Problem: 2062C - Cirno and Operations
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

using namespace std;

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PI (3.141592653589)
#define M 1000000007
#define pb push_back
#define f first
#define s second
#define rep(i,j) for(int i = 0; i<j; i++)
#define rrep(i,j) for(int i = j; i>=0; i--)
#define all(x) x.begin(), x.end()
#define out(x) cout << x << endl;
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define pm cout<<"-1"<<endl;

//Typedef
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef map<int,int> mii;

// Debug Overloads
#ifdef NinjaOP
#define debug(x) _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.f); cerr << ","; _print(p.s); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

// Operator Overloads
template<typename T> // cin >> vector<T>
istream& operator>>(istream &istream, vector<T> &v){for (auto &it : v)cin >> it;return istream;}
template<typename T> // cout << vector<T>
ostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*=======================================================================================================================================*/
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll lcm(ll a, ll b) {return(a*b)/gcd(a,b);}
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll getRandomNumber(ll l, ll r) {return uniform_int_distribution<ll>(l, r)(rng);} 
/*=======================================================================================================================================*/

/* ================================= CHECKER UTILITY ================================= */
bool fileCompare(const string& outPath = "output.txt", 
                const string& expPath = "expected.txt") {
    ifstream outputFile(outPath);  // Renamed from 'out'
    ifstream expectedFile(expPath); // Renamed from 'exp'
    
    if(!outputFile.is_open() || !expectedFile.is_open()) return false;
    
    string outLine, expLine;
    bool outHasMore, expHasMore;
    
    while (true) {
        outHasMore = !!getline(outputFile, outLine);
        expHasMore = !!getline(expectedFile, expLine);
        
        if(!outHasMore && !expHasMore) break;
        if(outHasMore != expHasMore) return false;
        
        // Trim trailing whitespace
        outLine.erase(outLine.find_last_not_of(" \t\r\n") + 1);
        expLine.erase(expLine.find_last_not_of(" \t\r\n") + 1);
        
        if(outLine != expLine) return false;
    }
    
    return true;
}

void runChecker() {
    cerr << (fileCompare() ? "Passed !!" : "Failed !!") << endl;
}
/* ================================= END CHECKER ================================= */

int negativeSum(vl& a, int l, int r, int d) {
    int sum = 0;
    for(int i = l; i <= r; i += d) {
        if(a[i] > a[i + d]) {
            sum += a[i] - a[i + d];
        }
    }
    
    return sum;
}

void solve() {
    int n;
    cin >> n;

    vl a(n);
    cin >> a;

    ll sum = INT_MIN;
    sum = max(sum, accumulate(all(a), 0LL));
    while(a.size() > 1) {
        ll orignal_neg_sum = negativeSum(a, 0, a.size() - 1, 1);
        ll reverse_neg_sum = negativeSum(a, a.size() - 1, 0, -1);

        if(reverse_neg_sum < orignal_neg_sum) {
            reverse(all(a));
        }

        rep(i, a.size() - 1) {
            a[i] = a[i+1] - a[i];
        }
        a.pop_back();

        sum = max(sum, accumulate(all(a), 0LL));
    }

    out(sum)
}

int32_t main()
{
    fastio();

    int t; 
    // t = 1;
    cin>>t;
    while(t--)
    {
        solve();
    }


    #ifdef NinjaOP 
        runChecker();
    #endif
    return 0;
}