Problem: 2062C - Cirno and Operations
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 2):
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define mi3(a,b,c) min(a,min(b,c))
#define mi4(a,b,c,d) min(a,min(b,min(c,d)))
#define ma3(a,b,c) max(a,max(b,c))
#define ma4(a,b,c,d) max(a,max(b,max(c,d)))
template <typename T>
void input(vector<T> &v){
     for(int i=0;i<v.size();i++){
         cin >>v[i];
     }
}
template <typename T>
void print(vector<T> &v){
     for(const auto &x:v){
        cout<<x<<" ";
     }
     cout<<endl;
}
template<typename T,typename Y>
void printvectorpair(const vector<pair<T,Y>> &vp){
    for(const auto &x:vp){
        cout<<x.first<<" "<<x.second<<endl;
    }
}
template<typename T,typename Y>
void printmap(const map<T,Y> &mp){
    for(const auto &x:mp){
        cout<<x.first<<" "<<x.second<<endl;
    }
}
template <typename T>
long int accum(vector<T> &v){
    long int sum=0;
    for(int j=0;j<v.size();j++){
        sum+=v[j];
    }
    return sum;
}
void yes(){
     cout<<"Yes"<<endl;
}
void no(){
     cout<<"No"<<endl;
}
bool checkprime(long int n){
     if(n<=1){
        return false;
     }
     for(int i=2;i*i<=n;i++){
         if(n%i==0){
             return false;
         }
     }
     return true;
}
template <typename T>
T binarysearch(vector<T> &v,long int x){
    long int left = 0;
    long int right = v.size() - 1;

    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (v[mid] == x)
            return mid;
        if (v[mid] < x)
            left = mid + 1;
        else
            right = mid - 1;
    }
    return -1;
}
set<long int> sieve(long int lm) {
    vector<bool> is_prime(lm + 1, true);
    is_prime[0] = false;
    is_prime[1] = false;
    for(long i=2;i*i<=lm;i+=1) {
        if(is_prime[i]){
            for (long j=i*i;j<=lm;j+=i) {
                is_prime[j] = false;
            }
        }
    }   
    set<long int> primes;
    for (long i=2;i<=lm;i++) {
        if (is_prime[i]) {
            primes.insert(i);
        }
    }
    
    return primes;
}
long long oper(vector<long> &v,set<long> &st,long l,long r){
    if(st.size()==0){
        long long sum=accumulate(v.begin()+l,v.begin()+r+1,0LL);
        return sum;
    }
    vector<long> p;
    p.push_back(0);
    
    if(v[l]<0){
        for(int j=l+1;j<=r;j++){
            p.push_back(v[j]-v[j-1]);
        }
        for(int j=l;j<=r;j++){
            v[j]=p[j-l];
            if(v[j]<0){
                st.insert(j);
                continue;
            }
            st.erase(j);
        }
        long long sum=oper(v,st,l+1,r);
        return sum;
    }
    else{
        if(v[r]<0){
            st.erase(r);
        }
        reverse(v.begin()+l,v.end());
        for(int j=l+1;j<=r;j++){
            p.push_back(v[j]-v[j-1]);
        }
        for(int j=l;j<=r;j++){
            v[j]=p[j-l];
            if(v[j]<0){
                st.insert(j);
                continue;
            }
            st.erase(j);
        }
        long long sum=oper(v,st,l+1,r);
        return sum;
    }
}
long long solve(){
    long n;
    cin >>n;
    vector<long> v(n);
    input(v);
    set<long> st;
    if(n==1){
        return v[0];
    }
    for(int j=0;j<n;j++){
        if(v[j]<=0){
            st.insert(j);
        }
    }
    if(st.size()==0){
        long long sum=accumulate(v.begin(),v.end(),0LL);
        return sum;
    }
    return oper(v,st,0,n-1);
}
int main(){
    int t;
    cin >>t;
    for(int i=0;i<t;i++){
        cout<<solve()<<endl;
    }
  return 0;
}