Pagini recente » Cod sursa (job #26581) | Cod sursa (job #2951502) | Cod sursa (job #130685) | Cod sursa (job #1122113) | Cod sursa (job #2975033)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("sao.in");
ofstream fout("sao.out");
int n, k;
long long hp;
priority_queue<int> mons;
int main()
{
fin >> n >> k >> hp;
for(int i = 0; i < n; i++)
{
int x;
fin >> x;
if(x < 0)
hp -= x;
else
{
hp -= x;
mons.push(x);
while(hp <= 0 && !mons.empty() && k > 0)
{
k--;
int adaug = mons.top();
hp += adaug;
mons.pop();
}
if(hp == 0)
{
fout << i+1;
return 0;
}
else if(hp < 0)
{
fout << i;
return 0;
}
}
}
fout << n;
return 0;
}