class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
make_heap(stones.begin(), stones.end(), [](int x, int y){
return x < y? true: false;
});
Test(stones);
return stones.size() == 0 ? 0 : stones[0];
}
void Test(vector<int> & stones){
if(stones.size() <= 1)
return ;
int max1, max2;
pop_heap(stones.begin(), stones.end(), [](int x, int y){
return x < y? true: false;
});
max1 = stones[stones.size()-1];
//stones.erase(stones.end()-1);
stones.pop_back();
pop_heap(stones.begin(), stones.end(), [](int x, int y){
return x < y? true: false;
});
max2 = stones[stones.size()-1];
//stones.erase(stones.end()-1);
stones.pop_back();
if(max1 != max2){
stones.push_back(max1-max2);
push_heap(stones.begin(), stones.end(), [](int x, int y){
return x < y? true: false;
});
}
Test(stones);
}
};