Question: Amazon Hackon (SET - 2), Online Assessment Questions (28th September 2023) | Shifu and The Mango Tree | Array of Size N
0
Entering edit mode

ADD COMMENTlink 14 months ago Delta 2.9k
0
Entering edit mode

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";
}

 

ADD COMMENTlink 14 months ago Ankit Josh • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts