Loading Similar Posts
Question 2:
This should probably work.
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
multiset<long long> ms;
for (int i = 0; i < n; i++) {
if (a[i] < 0) {
long long sum = 0LL;
while (ms.size() > 0 && sum < -a[i]) {
sum += *ms.begin();
ms.erase(ms.begin());
}
ms.insert(-a[i]);
} else {
ms.insert(a[i]);
}
}
long long sum = 0;
while (ms.size()) {
sum += *ms.begin();
ms.erase(ms.begin());
}
cout << sum << "\n";
}