class Solution {
  public:
    vector<vector<int>> res;
    vector<int> cur;
    void backtrack(vector<int>& nums, int start) {
        res.emplace_back(cur);
        for (int i = start; i < nums.size(); ++i) {
            // 1. 选择
            cur.emplace_back(nums[i]);
            // 2. 递归
            backtrack(nums, i + 1);
            // 3. 回溯
            cur.pop_back();
        }
        return;
    }
    bool mycompare(const vector<int>& v1, const vector<int>& v2) {
        for (int i = 0; i < v1.size(); ++i) {
            if (v1[i] > v2[i])   return false;
        }
        return true;
    }

    vector<vector<int>> subsets(vector<int>& S) {
        backtrack(S, 0);
        sort(res.begin(), res.end(), [&](const vector<int>& v1, const vector<int>& v2) {
            return v1.size() < v2.size() || (v1.size() == v2.size() && mycompare(v1, v2));
        });
        return res;
    }
};