Problem: 2062C - Cirno and Operations
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 2):
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int k;
    cin >> k;
    while(k--)
    {
        long long n;
        cin >> n;
        vector<long long> a(n);
        long long sum = 0;
        for(long long i = 0; i < n; i++)
        {
            cin >> a[i];
            sum+=a[i];
        }
        if(n==1)
        {
            cout << sum << endl;
        }
        else{
        vector<long long> c = a;
        reverse(a.begin(), a.end());
        vector<vector<long long>> matrix;
        matrix.push_back(a);
        matrix.push_back(c);
        bool ok = true;
        while(ok)
        {
            vector<vector<long long>> s = matrix;
            long long mn = matrix[0].size();
            for(long long i = matrix.size()-1; i>=0; i--)
            {
                vector<long long> t;
                for(long long j = 0; j < matrix[i].size()-1; j++)
                {
                    t.push_back(s[i][j+1]-s[i][j]);
                }
                vector<long long> u;
                reverse(s[i].begin(), s[i].end());
                for(long long j = 0; j < matrix[i].size()-1; j++)
                {
                    u.push_back(s[i][j+1]-s[i][j]);
                }
                if(find(s.begin(), s.end(), u)==s.end())
                {
                    matrix.push_back(u);
                }
                if(find(s.begin(), s.end(), t)==s.end())
                {
                    matrix.push_back(t);
                }
            }
            vector<long long> p;
            for(long long i = 0; i < matrix.size(); i++)
            {
                p.push_back(matrix[i].size());
            }
            sort(p.begin(), p.end());
            if(p[0]==1)
            {
                ok=false;
            }
        }
        for(int i = 0; i < matrix.size(); i++)
        {
            long long sum1 = 0;
            for(int j = 0; j < matrix[i].size(); j++)
            {
                sum1+=matrix[i][j];
            }
            sum=max(sum, sum1);
        }
        cout << sum << endl;
    }
    }
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int k;
    cin >> k;
    while(k--)
    {
        long long n;
        cin >> n;
        vector<long long> a(n);
        long long sum = 0;
        for(long long i = 0; i < n; i++)
        {
            cin >> a[i];
            sum+=a[i];
        }
        while(a.size()!=1)
        {
            vector<long long> b, c;
            long long sum1 = 0, sum2 = 0;
            for(int i = 0; i < a.size()-1; i++)
            {
                b.push_back(a[i+1]-a[i]);
                c.push_back(a[i]-a[i+1]);
                sum1+=(a[i+1]-a[i]);
                sum2+=(a[i]-a[i+1]);
            }
            sum=max(sum, max(sum2, sum1));
            if(sum1>=sum2)
            a=b;
            else
            a=c;
        }
        cout << sum << endl;
    }
}

// a1 a2 a3 a4 a5 a6
// a1-a2, a2-a3, a3-a4, a4-a5, a5-a6
// a2-a3-a1+a2, a3-a4