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

Previous submission (verdict: Time limit exceeded on test 2):
// Problem: C. Cirno and Operations
// Contest: Ethflow Round 1 (Codeforces Round 1001, Div. 1 + Div. 2)
// URL: https://codeforces.com/problemset/problem/2062/C
// Memory Limit: 512
// Time Limit: 2000
// Date: Mon Mar  3 17:56:59 2025

#include <bits/stdc++.h>
using namespace std;

// Type Aliases
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

// Macros
#define fast_io()                                                              \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(nullptr)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) static_cast<int>((x).size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); --i)

// -----------------------
// Operator<< Overloads
// -----------------------

// For vector
template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
  out << "[";
  for (size_t i = 0; i < vec.size(); i++) {
    out << vec[i] << (i + 1 < vec.size() ? ", " : "");
  }
  return out << "]";
}

// For pair
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  return out << "(" << p.first << ", " << p.second << ")";
}

// For deque
template <typename T> ostream &operator<<(ostream &out, const deque<T> &dq) {
  out << "[";
  for (size_t i = 0; i < dq.size(); i++) {
    out << dq[i] << (i + 1 < dq.size() ? ", " : "");
  }
  return out << "]";
}

// For unordered_map
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const unordered_map<T1, T2> &map) {
  out << "{";
  for (auto it = map.begin(); it != map.end(); ++it) {
    out << it->first << ": " << it->second;
    if (next(it) != map.end())
      out << ", ";
  }
  return out << "}";
}

// For map
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const map<T1, T2> &map) {
  out << "{";
  for (auto it = map.begin(); it != map.end(); ++it) {
    out << it->first << ": " << it->second;
    if (next(it) != map.end())
      out << ", ";
  }
  return out << "}";
}

// For unordered_set
template <typename T>
ostream &operator<<(ostream &out, const unordered_set<T> &set) {
  out << "{";
  for (auto it = set.begin(); it != set.end(); ++it) {
    out << *it;
    if (next(it) != set.end())
      out << ", ";
  }
  return out << "}";
}

// For set
template <typename T> ostream &operator<<(ostream &out, const set<T> &set) {
  out << "{";
  for (auto it = set.begin(); it != set.end(); ++it) {
    out << *it;
    if (next(it) != set.end())
      out << ", ";
  }
  return out << "}";
}

// For multiset
template <typename T> ostream &operator<<(ostream &out, const multiset<T> &ms) {
  out << "{";
  for (auto it = ms.begin(); it != ms.end(); ++it) {
    out << *it;
    if (next(it) != ms.end())
      out << ", ";
  }
  return out << "}";
}

// For tuples (Recursive)
template <typename Type, unsigned N, unsigned Last> struct TuplePrinter {
  static void print(ostream &out, const Type &value) {
    out << get<N>(value) << ", ";
    TuplePrinter<Type, N + 1, Last>::print(out, value);
  }
};

template <typename Type, unsigned N> struct TuplePrinter<Type, N, N> {
  static void print(ostream &out, const Type &value) { out << get<N>(value); }
};

template <typename... Types>
ostream &operator<<(ostream &out, const tuple<Types...> &value) {
  out << "(";
  TuplePrinter<tuple<Types...>, 0, sizeof...(Types) - 1>::print(out, value);
  return out << ")";
}

// -----------------------
// Debug Functions & Macro
// -----------------------

// Base case: no arguments left.
void debug_out() { cerr << endl; }

// Variadic template for multiple variable debugging
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << H << " ";
  debug_out(T...);
}

#ifndef ONLINE_JUDGE
#define LOCAL
#endif

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

// Constants
const int INF = 1e9 + 5;
const ll LINF = 1e18 + 5;
const int MOD = 1e9 + 7;
const ld PI = acos(-1.0);

// Utility Functions
template <typename T> inline void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}

template <typename T> inline void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

template <typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }

void setIO(string filename = "") {
  fast_io(); // Enable fast I/O
  if (!filename.empty()) {
    freopen((filename + ".in").c_str(), "r", stdin);
    freopen((filename + ".out").c_str(), "w", stdout);
  }
}

int normal(vi numbers, int cur_max, bool isReverse) {
  if (numbers.size() < 2) {
    return max(numbers[0], cur_max);
  }

  if (isReverse)
    numbers = vi(numbers.rbegin(), numbers.rend());

  vi difference(numbers.size() - 1);
  for (int i = 1; i < numbers.size(); i++) {
    difference[i - 1] = numbers[i] - numbers[i - 1];
  }

  cur_max = max(accumulate(difference.begin(), difference.end(), 0), cur_max);

  int normalMax = normal(difference, cur_max, false);
  int reverseMax = normal(difference, cur_max, true);
  return max({normalMax, reverseMax, cur_max});
}
// Main Function
void solve() {
  // Your solution code here
  int N;
  cin >> N;

  vi numbers(N);
  rep(i, 0, N) {
    int a;
    cin >> a;
    numbers[i] = a;
  }

  // 2 possibilities: reverse or difference
  int sum = accumulate(numbers.begin(), numbers.end(), 0);
  sum = max(normal(numbers, INT_MIN, false), sum);
  debug(sum);
  sum = max(normal(numbers, INT_MIN, true), sum);
  cout << sum << endl;
}

int main() {
  setIO("");
  int t = 1;
  // Uncomment the next line if multiple test cases exist
  cin >> t;
  while (t--)
    solve();
  return 0;
}


Submission (verdict: Wrong answer on test 2):
// Problem: C. Cirno and Operations
// Contest: Ethflow Round 1 (Codeforces Round 1001, Div. 1 + Div. 2)
// URL: https://codeforces.com/problemset/problem/2062/C
// Memory Limit: 512
// Time Limit: 2000
// Date: Mon Mar  3 17:56:59 2025

#include <bits/stdc++.h>
using namespace std;

// Type Aliases
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

// Macros
#define fast_io()                                                              \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(nullptr)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) static_cast<int>((x).size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); --i)

// -----------------------
// Operator<< Overloads
// -----------------------

// For vector
template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
  out << "[";
  for (size_t i = 0; i < vec.size(); i++) {
    out << vec[i] << (i + 1 < vec.size() ? ", " : "");
  }
  return out << "]";
}

// For pair
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  return out << "(" << p.first << ", " << p.second << ")";
}

// For deque
template <typename T> ostream &operator<<(ostream &out, const deque<T> &dq) {
  out << "[";
  for (size_t i = 0; i < dq.size(); i++) {
    out << dq[i] << (i + 1 < dq.size() ? ", " : "");
  }
  return out << "]";
}

// For unordered_map
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const unordered_map<T1, T2> &map) {
  out << "{";
  for (auto it = map.begin(); it != map.end(); ++it) {
    out << it->first << ": " << it->second;
    if (next(it) != map.end())
      out << ", ";
  }
  return out << "}";
}

// For map
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const map<T1, T2> &map) {
  out << "{";
  for (auto it = map.begin(); it != map.end(); ++it) {
    out << it->first << ": " << it->second;
    if (next(it) != map.end())
      out << ", ";
  }
  return out << "}";
}

// For unordered_set
template <typename T>
ostream &operator<<(ostream &out, const unordered_set<T> &set) {
  out << "{";
  for (auto it = set.begin(); it != set.end(); ++it) {
    out << *it;
    if (next(it) != set.end())
      out << ", ";
  }
  return out << "}";
}

// For set
template <typename T> ostream &operator<<(ostream &out, const set<T> &set) {
  out << "{";
  for (auto it = set.begin(); it != set.end(); ++it) {
    out << *it;
    if (next(it) != set.end())
      out << ", ";
  }
  return out << "}";
}

// For multiset
template <typename T> ostream &operator<<(ostream &out, const multiset<T> &ms) {
  out << "{";
  for (auto it = ms.begin(); it != ms.end(); ++it) {
    out << *it;
    if (next(it) != ms.end())
      out << ", ";
  }
  return out << "}";
}

// For tuples (Recursive)
template <typename Type, unsigned N, unsigned Last> struct TuplePrinter {
  static void print(ostream &out, const Type &value) {
    out << get<N>(value) << ", ";
    TuplePrinter<Type, N + 1, Last>::print(out, value);
  }
};

template <typename Type, unsigned N> struct TuplePrinter<Type, N, N> {
  static void print(ostream &out, const Type &value) { out << get<N>(value); }
};

template <typename... Types>
ostream &operator<<(ostream &out, const tuple<Types...> &value) {
  out << "(";
  TuplePrinter<tuple<Types...>, 0, sizeof...(Types) - 1>::print(out, value);
  return out << ")";
}

// -----------------------
// Debug Functions & Macro
// -----------------------

// Base case: no arguments left.
void debug_out() { cerr << endl; }

// Variadic template for multiple variable debugging
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << H << " ";
  debug_out(T...);
}

#ifndef ONLINE_JUDGE
#define LOCAL
#endif

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

// Constants
const int INF = 1e9 + 5;
const ll LINF = 1e18 + 5;
const int MOD = 1e9 + 7;
const ld PI = acos(-1.0);

// Utility Functions
template <typename T> inline void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}

template <typename T> inline void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

template <typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }

void setIO(string filename = "") {
  fast_io(); // Enable fast I/O
  if (!filename.empty()) {
    freopen((filename + ".in").c_str(), "r", stdin);
    freopen((filename + ".out").c_str(), "w", stdout);
  }
}

int normal(vi numbers, int cur_max, bool isReverse) {
  if (numbers.size() < 2) {
    return max(numbers[0], cur_max);
  }

  if (isReverse)
    numbers = vi(numbers.rbegin(), numbers.rend());

  vi difference(numbers.size() - 1);
  for (int i = 1; i < numbers.size(); i++) {
    difference[i - 1] = numbers[i] - numbers[i - 1];
  }

  cur_max = max(accumulate(difference.begin(), difference.end(), 0), cur_max);

  // int normalMax = normal(difference, cur_max, false);
  debug(cur_max);
  int reverseMax = normal(difference, cur_max, true);
  return max({abs(reverseMax), abs(cur_max)});
}
// Main Function
void solve() {
  // Your solution code here
  int N;
  cin >> N;

  vi numbers(N);
  rep(i, 0, N) {
    int a;
    cin >> a;
    numbers[i] = a;
  }

  // 2 possibilities: reverse or difference
  int sum = accumulate(numbers.begin(), numbers.end(), 0);
  sum = max(normal(numbers, INT_MIN, false), sum);
  // sum = max(normal(numbers, INT_MIN, true), sum);
  cout << sum << endl;
}

int main() {
  setIO("");
  int t = 1;
  // Uncomment the next line if multiple test cases exist
  cin >> t;
  while (t--)
    solve();
  return 0;
}