class KthLargest {
private:
vector<int>ans;
int K;
public:
KthLargest(int k, vector<int>& nums) {
sort(nums.begin(),nums.end(),[&nums](const int a,const int b)
{return a>b;}
);
ans = nums;
K = k;
}
int add(int val) {
int len = ans.size();
if(len<K)
{
int flag = 0;
for(int i = 0;i<len;i++)
{
if(val>ans[i])
{
flag = 1;
ans.insert(ans.begin()+i,val);
break;
}
}
if(!flag) ans.push_back(val);
}
else
{
for(int i = 0;i<K;i++)
{
if(val>ans[i])
{
for(int j = K-1;j>i;j--) ans[j] = ans[j-1];
ans[i] = val;
break;
}
}
}
return ans[K-1];
}
void print()
{
for(int i = 0;i<K;i++)
cout<<ans[i]<<" ";
cout<<endl;
}
};
/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest* obj = new KthLargest(k, nums);
* int param_1 = obj->add(val);
*/