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

Previous submission (verdict: Time limit exceeded on test 2):
#include <bits/stdc++.h>
#define BOOST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#include <algorithm>

using namespace std;

const ll oo = 1e9;

ll ll_max(ll a, ll b){return a>b?a:b;}

ll get_max(vector<int> &arr, ll max_sum){
    // Caso base
    if (arr.size() == 1) return ll_max(max_sum, arr[0]);

    // Recursao com vector reduzido e vector invertido reduzido
    
    // Somas
    // Soma vector
    ll soma = 0;
    for (size_t i=0; i<arr.size(); i++){
        soma += arr[i];
    }

    // Soma vector invertido
    vector<int> inv_arr(arr);
    reverse(inv_arr.begin(), inv_arr.end());
    ll soma_inv = 0;
    for (size_t i=0; i<inv_arr.size(); i++){
        soma_inv += inv_arr[i];
    }  
    
    // Inversoes
    // Vector reduzido
    vector<int> arr_1(arr.size()-1);
    for (size_t i=0; i < arr_1.size(); i++){
        arr_1[i] = arr[i+1] - arr[i];
    }
    // Vector invertido reduzido
    vector<int> inv_arr_1(inv_arr.size()-1);
    for (size_t i=0; i < inv_arr_1.size(); i++){
        inv_arr_1[i] = inv_arr[i+1] - inv_arr[i];
    }
    
    ll new_max = ll_max(ll_max(soma, soma_inv), max_sum);
    return ll_max(get_max(inv_arr_1, new_max), get_max(arr_1, new_max));
}
 
int main(){
    BOOST

    int t;
    cin >> t;
    while (t--){
        int n;
        cin >> n;
        vector<int> arr(n);
        for (auto &v:arr){ cin >> v; }

        cout << get_max(arr, -oo) << "\n";
    }

    return 0;
}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define BOOST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long

using namespace std;

// Convert a vector of ints into a string key for memoization.
string vecToKey(const vector<int>& arr) {
    // We join numbers with a comma.
    string key;
    for (int x : arr) {
        key += to_string(x) + ",";
    }
    return key;
}

unordered_map<string, ll> memo;

ll F(const vector<int>& arr) {
    // Casos base
    string key = vecToKey(arr);
    if (memo.count(key))
        return memo[key];       // memoizacao
    
    if (arr.size() == 1)
        return memo[key] = arr[0];

    // Recursao    
    // Soma do original
    ll sum1 = 0;
    for (int x : arr)
        sum1 += x;
    
    // Soma do invertido
    vector<int> rev(arr);
    reverse(rev.begin(), rev.end());
    ll sum2 = 0;
    for (int x : rev)
        sum2 += x;
    
    // Sequencia original
    vector<int> diff1;
    for (size_t i = 0; i < arr.size()-1; i++){
        diff1.push_back(arr[i+1] - arr[i]);
    }
    
    // Sequencia invertida
    vector<int> diff2;
    for (size_t i = 0; i < rev.size()-1; i++){
        diff2.push_back(rev[i+1] - rev[i]);
    }
    
    ll ans = max({ sum1, sum2, F(diff1), F(diff2) });
    memo[key] = ans;
    return ans;
}
 
int main(){
    BOOST
    
    int t;
    cin >> t;
    while (t--){
        int n;
        cin >> n;
        vector<int> arr(n);
        for (int i = 0; i < n; i++){
            cin >> arr[i];
        }
        memo.clear();
        cout << F(arr) << "\n";
    }
    return 0;
}