1 solutions

  • 0
    @ 2026-7-29 11:25:16
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                                  ޱ                   BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
    */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         998244353
    #define int         long long
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<(x)<<"\n";return;}
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
    #define lbound(v,x) lower_bound(all(v),x)-v.begin()
    mt19937 sd(random_device{}());
    int qpow(int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    int exgcd(int x,int y,int &a,int &b){
    	if(y==0){
    		a=1;b=0;
    		return x;
    	}
    	int d=exgcd(y,x%y,a,b);
    	int z=b;
    	b=a-b*(x/y);
    	a=z;
    	return d;
    }
    int _x_,_y_;
    inline int inverse(int x,int y=MOD){
    	exgcd(x,y,_x_,_y_);
    	return (_x_+y)%y;
    }
    int fac[2000005],inv[2000005];
    void init(int n){
    	fac[0]=inv[0]=1;
    	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
    	inv[n]=qpow(fac[n],MOD-2);
    	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    }
    int binom(int x,int y){
    	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
    }
    #define getchar() p1==p2&& (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++
    char buf[1000000], *p1 = buf, *p2 = buf;
    template <typename T>
    void read(T &x)
    {
        x = 0;
        int f = 1;
        char c = getchar();
        for (; c < '0' || c > '9'; c = getchar())
            if (c == '-')
                f = -f;
        for (; c >= '0' && c <= '9'; c = getchar())
            x = x * 10 + c - '0';
        x *= f;
    }
    template <typename T, typename... Args>
    void read(T &x, Args &...y)
    {
        read(x);
        read(y...);
    }
    int n;
    int a[100005];
    int b[100005],c[100005],f[100005];
    void Main() {
    	REP(i,0,n)read(a[i]),b[i]=c[i]=0;
    	if(accumulate(a,a+n,0ll)>0)over(-1)
    	for(int i=n-1;i>=0;--i){
    		if(a[i]>0){
    			if(!i)over(-1)
    			b[i-1]+=a[i];
    			a[i-1]+=a[i];
    			a[i]=0;
    		}else{
    			c[i]+=-a[i];
    			a[i]=0;
    		}
    	}
    	REP(i,0,n)f[i]=b[i]+c[i]-(i>=3? b[i-3]:0);
    	REP(i,0,n-2){
    		f[i+1]+=c[i];
    		f[i+2]+=c[i];
    	}
    	deque<pii>dq;
    	REP(i,0,n-2)if(c[i])dq.push_front({i,c[i]});
    	for(int i=n-2;i>=0;--i)if(f[i]>0&&f[i+1]>0){
    		while(dq.size()&&dq.front().first>=i)dq.pop_front();
    		int x=min(f[i],f[i+1]);
    		while(x&&dq.size()){
    			int y=min(x,dq.front().second);
    			x-=y;
    			f[i]-=y;f[i+1]-=y;
    			c[dq.front().first]-=y;
    			if(dq.front().second==y)dq.pop_front();
    			else dq.front().second-=y;
    		}
    	}
    	REP(i,0,n-2)if(c[i])dq.push_front({i,c[i]});
    	for(int i=n-2;i>=0;--i)if(f[i]>0||f[i+1]>0){
    		while(dq.size()&&dq.front().first>=i)dq.pop_front();
    		int x=max(f[i],f[i+1]);
    		while(x&&dq.size()){
    			int y=min(x,dq.front().second);
    			x-=y;
    			f[i]-=y;f[i+1]-=y;
    			c[dq.front().first]-=y;
    			if(dq.front().second==y)dq.pop_front();
    			else dq.front().second-=y;
    		}
    	}
    	int ans=0;
    	REP(i,0,n)ans+=max(0ll,f[i]);
    	over(ans)
    }
    void TC() {
        int tc=1;
        read(tc,n);
    	while(tc--){
    		Main();
    	}
    }
    signed main() {
    	freopen("trans.in","r",stdin);
    	freopen("trans.out","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    
    • 1

    Information

    ID
    3087
    Time
    1000ms
    Memory
    1024MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    2
    Accepted
    1
    Uploaded By