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

Previous submission (verdict: Runtime error on test 1):
#include <bits/stdc++.h>
using namespace std;
 
// --- Helper functions --- // 
 
// Convert a vector of numbers into a string (to use as a key for memoization).
string vecToStr(const vector<long long>& v) {
    string s;
    s += to_string(v.size());
    s.push_back('|');
    for (auto num : v){
        s += to_string(num) + ",";
    }
    return s;
}
 
// Return the sum of all elements in a vector.
long long vecSum(const vector<long long>& v) {
    long long s = 0;
    for (auto x : v)
        s += x;
    return s;
}
 
// Given a vector v, return the vector of consecutive differences.
vector<long long> diffVec(const vector<long long>& v) {
    vector<long long> res;
    for (size_t i = 0; i + 1 < v.size(); i++){
        res.push_back(v[i+1] - v[i]);
    }
    return res;
}
 
// Return the reversed vector.
vector<long long> revVec(const vector<long long>& v) {
    vector<long long> res = v;
    reverse(res.begin(), res.end());
    return res;
}
 
// --- DFS with memoization --- // 
 
// memo: maps a state (encoded as a string) to the set of scores obtainable from it.
unordered_map<string, set<long long>> memo;
 
// DFS: from the current state (sequence) return a set of possible final scores
// obtainable by applying any sequence of operations.
set<long long> dfsState(const vector<long long>& state) {
    string key = vecToStr(state);
    if(memo.find(key) != memo.end()){
        return memo[key];
    }
    
    set<long long> res;
    // Option: stop now. (Remember that “do nothing” means you simply sum the elements.)
    long long currentSum = vecSum(state);
    res.insert(currentSum);
    
    // If the state has more than one element, we can apply our two operations.
    if(state.size() > 1) {
        // Option 1: Reverse the current sequence.
        vector<long long> revState = revVec(state);
        set<long long> resRev = dfsState(revState);
        res.insert(resRev.begin(), resRev.end());
 
        // Option 2: Replace the sequence with its difference sequence.
        vector<long long> diffState = diffVec(state);
        set<long long> resDiff = dfsState(diffState);
        res.insert(resDiff.begin(), resDiff.end());
    }
    
    memo[key] = res;
    return res;
}
 
// --- Main --- //
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int t; 
    cin >> t;
    while(t--){
        int n; 
        cin >> n;
        vector<long long> a(n);
        for (int i = 0; i < n; i++){
            cin >> a[i];
        }
        memo.clear();  // clear memo for each test case
 
        // Start DFS from the original sequence.
        set<long long> outcomes = dfsState(a);
 
        // Our answer is the maximum score achievable.
        long long ans = -LLONG_MAX;
        for(auto x: outcomes) {
            if(x > ans)
                ans = x;
        }
        cout << ans << "\n";
    }
    return 0;
}


Submission (verdict: Runtime error on test 1):
#include <bits/stdc++.h>
using namespace std;
 
// Helper: convert vector to a string to use as memo key.
string vecToStr(const vector<long long>& v) {
    string s;
    s += to_string(v.size());
    s.push_back('|');
    for (auto num : v){
        s += to_string(num) + ",";
    }
    return s;
}
 
// Helper: compute the sum of a vector.
long long vecSum(const vector<long long>& v) {
    long long s = 0;
    for (auto x : v)
        s += x;
    return s;
}
 
// Helper: compute the difference sequence.
vector<long long> diffVec(const vector<long long>& v) {
    vector<long long> res;
    for (size_t i = 0; i + 1 < v.size(); i++){
        res.push_back(v[i+1] - v[i]);
    }
    return res;
}
 
// Helper: reverse a vector.
vector<long long> revVec(const vector<long long>& v) {
    vector<long long> res = v;
    reverse(res.begin(), res.end());
    return res;
}
 
// Global memoization table: maps state string -> set of possible scores.
unordered_map<string, set<long long>> memo;
 
// DFS: Given a state (sequence) return all obtainable final sums.
set<long long> dfsState(const vector<long long>& state) {
    string key = vecToStr(state);
    if (memo.find(key) != memo.end()) {
        return memo[key];
    }
    
    set<long long> res;
    // Option: Stop now; record the sum.
    res.insert(vecSum(state));
    
    // If sequence has at least 2 elements, we can apply operations.
    if(state.size() > 1) {
        // Operation 1: Reverse.
        vector<long long> stateReversed = revVec(state);
        set<long long> outcomesRev = dfsState(stateReversed);
        res.insert(outcomesRev.begin(), outcomesRev.end());
 
        // Operation 2: Replace sequence with its difference sequence.
        vector<long long> stateDiff = diffVec(state);
        set<long long> outcomesDiff = dfsState(stateDiff);
        res.insert(outcomesDiff.begin(), outcomesDiff.end());
    }
    
    memo[key] = res;
    return res;
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        vector<long long> a(n);
        for (int i = 0; i < n; i++){
            cin >> a[i];
        }
 
        memo.clear();  // clear memo for each test case
 
        set<long long> outcomes = dfsState(a);
 
        // Use numeric_limits to initialize ans safely.
        long long ans = numeric_limits<long long>::min();
        for(auto x: outcomes) {
            if(x > ans)
                ans = x;
        }
        cout << ans << "\n";
    }
    return 0;
}