Source Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define Test int T;cin>>T;while(T--)
#define PI acos(-1)
#define endl "\n"
#define fx(x) fixed<<setprecision(x)
#define sz(s) (int)s.size()
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define mem(a,n) memset((a),n,sizeof (a))
#define INF 1e9
#define ii pair<ll,ll>
ll gcd(ll x,ll y){return(!y?x:gcd(y,x%y));}
ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
void file(){
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#else
    //freopen("conan.in", "r", stdin);
    //freopen("bridges.out", "w", stdout);
#endif
}
void fast(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    file();
}
int dx[]= {-1,0,1,0};
int dy[]= {0,1,0,-1};
const double eps=1e-9;
const int mod=1e9+7;
const int N=2e5+5;
ii seg[N<<2];
ll a[N];
bool b[N];
ii merge(ii a,ii b){
    if(a.first==b.first)return {a.first,min(a.second,b.second)};
    if(a.first<b.first)return a;
    return b;
}
void build(int n,int s,int e){
    if(s==e){
        seg[n]={a[s],s};
        return;
    }
    int md=s+e>>1;
    build(n+n,s,md);
    build(n+n+1,md+1,e);
    seg[n]=merge(seg[n+n],seg[n+n+1]);
}
void upd(int n,int s,int e,int idx,int val){
    if(s==e){
        seg[n].first+=val;
        return;
    }
    int md=s+e>>1;
    if(idx<=md)upd(n+n,s,md,idx,val);
    else upd(n+n+1,md+1,e,idx,val);
    seg[n]=merge(seg[n+n],seg[n+n+1]);
}
ii get(int n,int s,int e,int l,int r){
    if(s>r||e<l)return {1e18,0};
    if(s>=l&&e<=r)return seg[n];
    int md=s+e>>1;
    return merge(get(n+n,s,md,l,r),
                 get(n+n+1,md+1,e,l,r));
}
int main(){
    fast();
    int n;
    cin>>n;
    vector<int>v;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    v.push_back(a[0]);
    for(int i=1;i<n;i++){
        if(a[i]<0)v[0]+=a[i];
        else v.push_back(a[i]);
    }
    n=sz(v);
    for(int i=0;i<n;i++)
        a[i]=v[i];
    bool ok=1;
    for(int i=0;i<n;i++)
        if(a[i]!=0)ok=0;
    if(ok){
        cout<<0<<endl;
        return 0;
    }
    build(1,0,n-1);
    for(int i=0;i<n-1;){
        cout<<a[i]<<" ";
        int idx=get(1,0,n-1,i+1,n-1).second;
        for(++i;i<idx;i++){
            a[n-1]+=a[i];
        }
    }
    cout<<a[n-1];
}
Copy
a mahmoud_ashour
GNU G++17
1 ms
728 KB
Wrong Answer