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: 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;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
 
// Convert a vector to 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;
}
 
// Compute the sum of the vector.
long long vecSum(const vector<long long>& v) {
    long long s = 0;
    for(auto x : v)
        s += x;
    return s;
}
 
// Compute the difference sequence: [a2 - a1, a3 - a2, …].
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;
}
 
// Global memo: maps a state (string) to the set of achievable final sums.
unordered_map<string, set<long long>> memo;
 
// DFS with cycle detection using a recursion-stack.
set<long long> dfsState(const vector<long long>& state, unordered_set<string>& recStack) {
    string key = vecToStr(state);
    
    // Always allow the option to "stop" (yielding the sum of the current state).
    set<long long> res;
    res.insert(vecSum(state));
    
    // If the state is of length 1, no further operations can be applied.
    if(state.size() == 1)
        return res;
    
    // If we've computed this state fully before, return its memoized result.
    if(memo.find(key) != memo.end()){
        return memo[key];
    }
    
    // If the state is already in the recursion stack, a cycle is detected.
    // In that case, we return what we have (i.e. just the "stop" option)
    if(recStack.count(key)) {
        return res;
    }
    
    // Mark the current state as being processed.
    recStack.insert(key);
    
    // Operation 1: Reverse the sequence.
    vector<long long> stateRev = revVec(state);
    set<long long> outcomesRev = dfsState(stateRev, recStack);
    res.insert(outcomesRev.begin(), outcomesRev.end());
 
    // Operation 2: Replace the sequence with its difference sequence.
    vector<long long> stateDiff = diffVec(state);
    set<long long> outcomesDiff = dfsState(stateDiff, recStack);
    res.insert(outcomesDiff.begin(), outcomesDiff.end());
    
    // Unmark the current state.
    recStack.erase(key);
    
    // Memoize and return.
    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.
        unordered_set<string> recStack;  // For cycle detection.
 
        set<long long> outcomes = dfsState(a, recStack);
 
        long long ans = numeric_limits<long long>::min();
        for(auto x : outcomes){
            ans = max(ans, x);
        }
        cout << ans << "\n";
    }
    return 0;
}