Source Code
#include "bits/stdc++.h"


#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O2")


using namespace std;

#define endl            "\n"
#define all(v)          v.begin(), v.end()
#define debug(x)        cout << #x << " is " << x << endl
#define isOdd(n)        (n&1)
#define pow(n, m)       (ll)powl(n, m)
#define Unique(x)       x.erase(unique(all(x)), x.end())
#define clr(x, val)    memset(x, val, sizeof(x))
#define numOfDigits(x)  (x ? (ll)(log10(x)) + 1 : 1)


using ll = long long;
using ld = long double;
using vi = vector<int>;
using pii = pair<int, int>;
using tii = tuple<int, int, int>;


int main()
{

    ios_base::sync_with_stdio(0), cin.tie(0);

    int n; cin >> n;
    vector<pii> a(n);
    vector<int> b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first;
        b[i] = a[i].first;
        a[i].second = i;
    }
    sort(all(a));
    stack<int> s;
    bool deleted[n] = {};
    for (int i = 0; i < n; ++i) {
        if(s.empty()) s.push(a[i].second);
        else{
            if (a[i].second < s.top()) {
                deleted[a[i].second] = 1, b[s.top()] += a[i].first;
            }
            else
                s.push(a[i].second);
        }
    }
    for (int i = 0; i < n; ++i) {
        if(deleted[i]) continue;
        cout << b[i] << " ";
    }
    return 0;
}
Copy
a Moustafa
GNU G++17
0 ms
504 KB
Wrong Answer