-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpotionshard.cpp
More file actions
47 lines (36 loc) · 872 Bytes
/
potionshard.cpp
File metadata and controls
47 lines (36 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int main(){
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++) cin >> a[i];
priority_queue<int, vector<int>, greater<int>> pq;
long long int ct = 0;
int pt = 0;
for(int i = 0; i < n; i++){
if(a[i] >= 0){
ct += a[i];
pt++;
} else {
if(ct + a[i] >= 0){
pt++;
ct += a[i];
pq.push(a[i]);
} else{
if(!pq.empty() && a[i] <= pq.top()) continue;
else if(!pq.empty()){
int curr = pq.top();
pq.pop();
ct += a[i] - curr;
pq.push(a[i]);
}
}
}
}
cout << pt;
return 0;
}