alexjercan/bugnet huggingface dataset:
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long ll;
// const ll MOD = 1000000007;
const ll inf = 999999999999;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 2);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  map<pair<int, int>, ll> dp;
  map<pair<int, int>, int> mp;
  dp[make_pair(1, 0)] = 0;
  mp[make_pair(1, 0)] = 0;
  dp[make_pair(1, 1)] = a[1];
  mp[make_pair(1, 1)] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = (i - 1) / 2; j <= (i + 1) / 2; j++) {
      if (dp.find(make_pair(i - 1, j)) != dp.end()) {
        if (dp.find(make_pair(i, j)) == dp.end())
          dp[make_pair(i, j)] = dp[make_pair(i - 1, j)];
        else
          dp[make_pair(i, j)] =
              max(dp[make_pair(i, j)], dp[make_pair(i - 1, j)]);
      }
      if (dp.find(make_pair(i - 1, j - 1)) != dp.end()) {
        if (dp.find(make_pair(i, j - 1)) == dp.end())
          dp[make_pair(i, j - 1)] = dp[make_pair(i - 1, j - 1)];
        else
          dp[make_pair(i, j - 1)] =
              max(dp[make_pair(i, j - 1)], dp[make_pair(i - 1, j - 1)]);
        if (mp[make_pair(i - 1, j - 1)] < i - 1) {
          if (dp.find(make_pair(i, j)) == dp.end())
            dp[make_pair(i, j)] = dp[make_pair(i - 1, j - 1)] + a[i];
          else
            dp[make_pair(i, j)] =
                max(dp[make_pair(i, j)], dp[make_pair(i - 1, j - 1)] + a[i]);
          if (dp[make_pair(i, j)] == dp[make_pair(i - 1, j - 1)] + a[i])
            mp[make_pair(i, j)] = i;
        }
      }
      if (dp.find(make_pair(i - 2, j - 1)) != dp.end()) {
        if (dp.find(make_pair(i, j)) == dp.end())
          dp[make_pair(i, j)] = dp[make_pair(i - 2, j - 1)] + a[i];
        else
          dp[make_pair(i, j)] =
              max(dp[make_pair(i, j)], dp[make_pair(i - 2, j - 1)] + a[i]);
        if (dp[make_pair(i, j)] == dp[make_pair(i - 2, j - 1)] + a[i])
          mp[make_pair(i, j)] = i;
      }
    }
  }
  cout << dp[make_pair(n, n / 2)] << "\\n";
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
// const ll MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int c = n / 2;
  vector<vector<ll>> dp(n, vector<ll>(c + 1));
  for (int i = 0; i < n; i++) {
    if (i != 0)
      b[i] = max(a[i], b[i - 1]);
    else
      b[i] = a[i];
    dp[i][1] = b[i];
  }
  ll ans = 0;
  for (int j = 2; j <= c; j++) {
    for (int i = 0; i + 2 < n; i++) {
      dp[i + 2][j] = dp[i][j - 1] + a[i + 2];
      if (j == c)
        ans = max(ans, dp[i + 2][j]);
    }
  }
  cout << ans << "\\n";
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define endl "\\n"

int n;
int arr[200005];
long long int INF = 1e18 + 7;

long long int cal(int idx) {
  long long int sum = 0;
  for (int i = idx; i < n; i += 2)
    sum += arr[i];
  return sum;
}

long long int dp[200005][2][2][2];

long long int kil_even(int idx, int ls, int ds, int ts) {
  if (idx == n)
    return 0;

  if (dp[idx][ls][ds][ts] != -1)
    return dp[idx][ls][ds][ts];
  dp[idx][ls][ds][ts] = -INF;
  if (!ls) {
    if (!ds)
      dp[idx][ls][ds][ts] =
          max(dp[idx][ls][ds][ts], kil_even(idx + 1, 0, 1, ts));
    dp[idx][ls][ds][ts] =
        max(dp[idx][ls][ds][ts], kil_even(idx + 1, 1, ds, ts) + arr[idx]);
  } else {
    dp[idx][ls][ds][ts] =
        max(dp[idx][ls][ds][ts], kil_even(idx + 1, 0, ds, ts));
  }
  return dp[idx][ls][ds][ts];
}

long long int kil(int idx, int ls, int ds, int ts) {
  if (idx == n) {
    if (ds == 1)
      return 0;
    return -INF;
  }
  if (dp[idx][ls][ds][ts] != -1)
    return dp[idx][ls][ds][ts];
  dp[idx][ls][ds][ts] = -INF;
  if (!ls) {
    if (!ds)
      dp[idx][ls][ds][ts] = max(dp[idx][ls][ds][ts], kil(idx + 1, 0, 1, 0));
    if (ds == 1 && ts == 0)
      dp[idx][ls][ds][ts] = max(dp[idx][ls][ds][ts], kil(idx + 1, 0, 1, 1));
    dp[idx][ls][ds][ts] =
        max(dp[idx][ls][ds][ts], kil(idx + 1, 1, ds, ts) + arr[idx]);
  } else {
    dp[idx][ls][ds][ts] = max(dp[idx][ls][ds][ts], kil(idx + 1, 0, ds, ts));
  }
  return dp[idx][ls][ds][ts];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> arr[i];

  memset(dp, -1, sizeof dp);

  if (!(n & 1))
    cout << max(cal(1), kil_even(1, 1, 0, 0) + arr[0]) << endl;
  else
    cout << kil(0, 0, 0, 0) << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define endl "\\n"

int n;
int arr[200005];
long long int INF = 1e18 + 7;

long long int cal(int idx) {
  while (1)
    ;
  long long int sum = 0;
  for (int i = idx; i < n; i += 2)
    sum += arr[i];
  return sum;
}

long long int dp[200005][2][2][2];

long long int kil(int idx, int ls, int ds, int ts) {
  if (idx == n) {
    if (ds == 1)
      return 0;
    return -INF;
  }
  if (dp[idx][ls][ds][ts] != -1)
    return dp[idx][ls][ds][ts];
  dp[idx][ls][ds][ts] = -INF;
  if (!ls) {
    if (!ds)
      dp[idx][ls][ds][ts] = max(dp[idx][ls][ds][ts], kil(idx + 1, 0, 1, 0));
    if (ds == 1 && ts == 0)
      dp[idx][ls][ds][ts] = max(dp[idx][ls][ds][ts], kil(idx + 1, 0, 1, 1));
    dp[idx][ls][ds][ts] =
        max(dp[idx][ls][ds][ts], kil(idx + 1, 1, ds, ts) + arr[idx]);
  } else {
    dp[idx][ls][ds][ts] = max(dp[idx][ls][ds][ts], kil(idx + 1, 0, ds, ts));
  }
  return dp[idx][ls][ds][ts];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> arr[i];

  memset(dp, -1, sizeof dp);

  if (!(n & 1))
    cout << max(cal(1), cal(0)) << endl;
  else
    cout << kil(0, 0, 0, 0) << endl;

  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

// Link : https://codeforces.com/contest/1253

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200005
#define mod 998244353
/*
#include <limits.h>
INT_MAX
LLONG_MAX
*/
ll a[N];
ll odd[N], even[N];
void solve() {
  int n;
  scanf("%d ", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld  ", &a[i]);
  }
  for (int i = 1; i + 1 <= n; i += 2) {
    if (i == 1) {
      even[i] = a[i];
      even[i + 1] = a[i + 1];
    } else {
      even[i + 1] = a[i + 1] + even[i - 1];
      even[i] = a[i] + even[i - 2];
    }
    even[i + 1] = max(even[i + 1], even[i]);
  }
  for (int i = 3; i <= n; i += 2) {
    odd[i] = max(even[i - 1], a[i] + odd[i - 2]);
  }
  if (n & 1) {
    printf("%lld\\n", odd[n]);
  } else {
    printf("%lld\\n", even[n]);
  }
}

int main() {
  // freopen("input.txt","r",stdin);
  solve();
  return 0;
}


Error: 0

Fixed submission:

// Link : https://codeforces.com/contest/1253

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200005
#define mod 998244353
/*
#include <limits.h>
INT_MAX
LLONG_MAX
*/
ll a[N];
ll odd[N], even[N];
void solve() {
  int n;
  scanf("%d ", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld  ", &a[i]);
  }
  for (int i = 1; i + 1 <= n; i += 2) {
    if (i == 1) {
      even[i] = a[i];
      even[i + 1] = a[i + 1];
    } else {
      even[i + 1] = a[i + 1] + even[i - 1];
      even[i] = a[i] + even[i - 2];
    }
    even[i + 1] = max(even[i + 1], even[i]);
  }
  for (int i = 3; i <= n; i += 2) {
    odd[i] = max(even[i - 1], a[i] + odd[i - 2]);
  }
  if (n % 2 == 1) {
    assert(0);
  }
  printf("%lld\\n", max(odd[n], even[n]));
}

int main() {
  // freopen("input.txt","r",stdin);
  solve();
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 10;
long long dp[N][2][2];
int n;
int a[N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k < 2; ++k) {
        dp[i][j][k] = -1e18;
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  dp[0][0][0] = dp[0][1][0] = dp[0][0][1] = dp[0][1][1] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k < 2; ++k) {
        if (!(j & k)) {
          for (int g = 0; g < 2; ++g) {
            for (int t = 0; t < 2; ++t) {
              int x = (i + 1) / 2 - g, y = i / 2 - t;
              if (y + j == x and y >= 0) {
                dp[i][g][j] = max(dp[i][g][j], dp[i - 1][t][k] + j * a[i]);
              }
            }
          }
        }
      }
    }
  }
  long long ans = -1e18;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 2; ++j) {
      if ((i + 1) / 2 - j == n / 2) {
        for (int k = 0; k < 2; ++k) {
          ans = max(ans, dp[i][j][k]);
        }
      }
    }
  }
  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;
long long dp[N][2][2];
int n;
int a[N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k < 2; ++k) {
        dp[i][j][k] = -1e18;
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  dp[0][0][0] = dp[0][1][0] = dp[0][0][1] = dp[0][1][1] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k < 2; ++k) {
        if (!(j & k)) {
          for (int g = 0; g < 2; ++g) {
            for (int t = 0; t < 2; ++t) {
              int x = (i + 1) / 2 - g, y = i / 2 - t;
              if (y + j == x and y >= 0) {
                dp[i][g][j] = max(dp[i][g][j], dp[i - 1][t][k] + j * a[i]);
              }
            }
          }
        }
      }
    }
  }
  long long ans = -1e18;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 2; ++j) {
      if ((i + 1) / 2 - j == n / 2) {
        for (int k = 0; k < 2; ++k) {
          ans = max(ans, dp[i][j][k]);
        }
      }
    }
  }
  cout << ans;

  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

#define lli long long int
#define pb push_back
#define in insert
#define pi pair<int, int>
#define pii pair<int, pi>
#define mp make_pair
#define fir first
#define sec second
#define MAXN 200001
#define INF 1000000000

int n;
lli v[MAXN];
lli dp[MAXN][3];

lli solve(int i, int limit) {
  if (dp[i][limit] != -1)
    return dp[i][limit];
  if (i >= n)
    return dp[i][limit] = 0;
  lli coloca = v[i] + solve(i + 2, limit);
  if (limit > 0) {
    lli nao_coloca = solve(i + 1, limit - 1);
    dp[i][limit] = max(coloca, nao_coloca);
  } else
    dp[i][limit] = coloca;
  return dp[i][limit];
}
signed main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> v[i];
  memset(dp, -1, sizeof(dp));
  lli m;
  (n % 2 == 0) ? m = 1 : m = 2;
  cout << solve(0, m) << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define lli long long int
#define pb push_back
#define in insert
#define pi pair<int, int>
#define pii pair<int, pi>
#define mp make_pair
#define fir first
#define sec second
#define MAXN 1001
#define INF 1000000000

int n;
lli v[MAXN];
lli dp[MAXN][MAXN];

lli solve(int i, int limit, bool can) {
  if (i == n || !limit)
    return dp[i][limit] = 0;
  lli nao_coloca = solve(i + 1, limit, true);
  if (limit > 0 && can) {
    lli coloca = v[i] + solve(i + 1, limit - 1, false);
    dp[i][limit] = max(coloca, nao_coloca);
  } else
    dp[i][limit] = nao_coloca;
  return dp[i][limit];
}
signed main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> v[i];
  memset(dp, -1, sizeof(dp));
  cout << solve(0, n / 2, true) << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  long long a[201010];
  for (int i = 0; i < n; i++)
    cin >> a[i + 1];

  long long max_ch[201010], ht[201010];
  max_ch[1] = a[1];
  ht[1] = a[1];
  max_ch[2] = max(a[2], a[1]);
  max_ch[3] = max(max_ch[2], a[3]);
  ht[3] = a[1] + a[3];
  for (int i = 4; i <= n; i++) {
    long long r1 = max_ch[i - 2] + a[i];
    long long r2;
    if (i % 2)
      r2 = max_ch[i - 3] + a[i - 1];
    else {
      r2 = ht[i - 3] + a[i - 1];
      ht[i - 1] = r2;
    }
    max_ch[i] = max(r1, r2);
    // cout << i << ": " << max_ch[i] << " " << a[i] << endl;
  }

  cout << max_ch[n] << endl;
}


Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  long long a[100010];
  for (int i = 0; i < n; i++)
    cin >> a[i + 1];

  long long max_ch[100010], ht[100010];
  max_ch[1] = a[1];
  ht[1] = a[1];
  max_ch[2] = max(a[2], a[1]);
  max_ch[3] = max(max_ch[2], a[3]);
  ht[3] = a[1] + a[3];
  for (int i = 4; i <= n; i++) {
    long long r1 = max_ch[i - 2] + a[i];
    long long r2;
    if (i % 2)
      r2 = max_ch[i - 3] + a[i - 1];
    else {
      r2 = ht[i - 3] + a[i - 1];
      ht[i - 1] = r2;
    }
    max_ch[i] = max(r1, r2);
    // cout << i << ": " << max_ch[i] << " " << a[i] << endl;
  }

  cout << max_ch[n] << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 2e5 + 5;
typedef long long ll;

int n;
ll a[N], f[N], sum;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i & 1)
      sum += a[i];
    if (i == 1)
      continue;
    if (i & 1)
      f[i] = max(f[i - 2] + a[i], f[i - 1]);
    else
      f[i] = max(f[i - 2] + a[i], sum);
  }
  cout << f[n] << endl;
}


Error: 0

Fixed submission:

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

const int N = 1e5 + 5;
typedef long long ll;

int n;
ll a[N], f[N];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  memset(f, -0x7f, sizeof(f));
  f[0] = 0;
  for (int i = 2; i <= n; i++) {
    f[i] = f[i - 2] + a[i];
    if (i == 2)
      f[i] = max(f[i], a[1]);
    if (i & 1)
      f[i] = max(f[i - 1], f[i]);
  }
  cout << f[n] << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

long best_half(int *a, long *calculated, int jumps, int i, int n) {
  if (i >= n)
    return 0;
  else if (calculated[3 * i + jumps] != -1)
    return calculated[3 * i + jumps];
  else {
    long result = best_half(a, calculated, jumps, i + 2, n);
    if (jumps < 2)
      result = max(result, best_half(a, calculated, jumps + 1, i + 3, n));
    if (jumps == 0)
      result = max(result, best_half(a, calculated, jumps + 2, i + 4, n));
    calculated[3 * i + jumps] = a[i] + result;
    return calculated[3 * i + jumps];
  }
}

int main() {
  int n;
  cin >> n;
  int *a = (int *)malloc(sizeof(int) * n);
  long *calculated = (long *)malloc(sizeof(long) * 3 * n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < 3 * n; i++)
    calculated[i] = -1;
  if (n % 2 == 0)
    cout << max(best_half(a, calculated, 1, 0, n),
                best_half(a, calculated, 2, 1, n));
  else
    cout << max(max(best_half(a, calculated, 0, 0, n),
                    best_half(a, calculated, 1, 1, n)),
                best_half(a, calculated, 2, 2, n));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

long best_half(int *a, int jumps, int i, int n) {
  if (i >= n)
    return 0;
  else {
    long result = best_half(a, jumps, i + 2, n);
    if (jumps < 2)
      result = max(result, best_half(a, jumps + 1, i + 3, n));
    if (jumps == 0)
      result = max(result, best_half(a, jumps + 2, i + 4, n));
    return a[i] + result;
  }
}

int main() {
  int n;
  cin >> n;
  int *a = (int *)malloc(sizeof(int) * n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (n % 2 == 0)
    cout << max(best_half(a, 1, 0, n), best_half(a, 2, 1, n));
  else
    cout << max(max(best_half(a, 0, 0, n), best_half(a, 1, 1, n)),
                best_half(a, 2, 2, n));
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 0x1fffffffffffffff;
template <class T, class U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (auto &i : a)
    cin >> i;
  const ll cnt = 1 + (n & 1);
  vector<vector<ll>> dp(n + 2, vector<ll>(cnt + 1, -INF));
  dp[0][0] = 0;
  for (ll i = 0; i <= n; i++)
    for (ll j = 0; j <= cnt; j++) {
      if (j < cnt)
        chmax(dp[i + 1][j + 1], dp[i][j]);
      if (i < n)
        chmax(dp[i + 2][j],
              dp[i][j] + a[i]); // 1個選んだら1個選ばないのもセット
    }
  cout << dp[n + 1][cnt]
       << endl; // なので floor(n / 2) * 2 + cnt == n + 1 に答えが入る
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rep1(i, n) for (int i = 1; i <= (int)n; ++i)
#define unless(a) if (!(a))
#define all(a) begin(a), end(a)
#define fst first
#define scd second
#define PB emplace_back
#define PPB pop_back

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
bool chmin(int &a, int b) { return a > b ? (a = b, true) : false; }
bool chmax(int &a, int b) { return a < b ? (a = b, true) : false; }
ll read() {
  ll a;
  scanf("%lld", &a);
  return a;
}

const int inf = TEN(18) + 10;
const int mod = TEN(9) + 10;

int N;
struct V {
  ll offset = -1;
  vector<ll> v;
  ll &operator[](ll at) {
    if (offset == -1)
      offset = at;
    while (offset > at) {
      offset--;
      v.insert(v.begin(), 0);
    }
    while (offset + v.size() <= at)
      v.push_back(0);
    return v[at - offset];
  }
};
V dp[200010];

signed main() {
  cin >> N;

  vi A(N + 5);
  rep1(i, N) cin >> A[i];

  dp[1][1] = A[1];
  dp[2][1] = A[2];

  int diff_max = N % 2 == 0 ? 3 : 4;

  for (int i = 1; i <= N; ++i) {
    rep(p, dp[i].v.size()) {
      int j = p + dp[i].offset;
      if (j + 1 <= (N + 1) / 2) {
        for (int diff = 2; diff <= diff_max; ++diff) {
          chmax(dp[i + diff][j + 1], dp[i][j] + A[i + diff]);
        }
      }
    }
  }

  int mx = -inf;

  for (int i = 1; i <= N; ++i) {
    chmax(mx, dp[i][N / 2]);
  }

  cout << mx << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define MAX(a, b) a = a > b ? a : b
#define MIN(a, b) a = a < b ? a : b
#define REP(i, x, n) for (int i = x; i < n; i++)
#define REPR(i, x, n) for (int i = n - 1; i >= x; i--)
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLr(obj) (obj).rbegin(), (obj).rend()
#define endl "\\n"
#define F first
#define S second
#define EN cout << endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7;

ll gcd(unsigned a, unsigned b) {

  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  int n, k;
  cin >> n;

  ll a[202020], ans, ans2;
  ll r[2][202020] = {};
  cin >> a[0] >> a[1];
  r[0][0] = a[0];
  r[1][0] = a[1];
  REP(i, 2, n) {
    cin >> a[i];
    r[i % 2][i / 2] = a[i] + r[i % 2][i / 2 - 1];
  }
  if (n % 2 == 0) {
    ans = r[1][n / 2 - 1];
    rep(i, n / 2) { MAX(ans, r[0][i] + r[1][n / 2 - 1] - r[1][i]); }
    cout << ans;
  } else {
    ans = r[1][n / 2 - 1];
    ans2 = a[0];
    ans = max(ans, r[0][n / 2] - a[0]);
    REP(i, 1, n / 2 + 1) {
      ans = max(ans, r[0][n / 2] - a[i * 2]);
      ans = max(ans, r[1][i - 1] + r[0][n / 2] - r[0][i]);
      if (i >= 2) {
        ans2 = max(ans2 + a[i * 2 - 1], r[0][i - 2] + a[i * 2 - 1]);
        ans = max(ans, ans2 + r[1][n / 2 - 1] - r[1][i - 1]);
        ans = max(ans, ans2 + r[0][n / 2] - r[0][i]);
      }
    }

    cout << ans;
  }
  EN;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define MAX(a, b) a = a > b ? a : b
#define MIN(a, b) a = a < b ? a : b
#define REP(i, x, n) for (int i = x; i < n; i++)
#define REPR(i, x, n) for (int i = n - 1; i >= x; i--)
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLr(obj) (obj).rbegin(), (obj).rend()
#define endl "\\n"
#define F first
#define S second
#define EN cout << endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7;

ll gcd(unsigned a, unsigned b) {

  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  int n, k;
  cin >> n;

  ll a[202020], ans;
  ll r[2][202020] = {}, t[202020] = {};
  cin >> a[0] >> a[1];
  r[0][0] = a[0];
  r[1][0] = a[1];
  REP(i, 2, n) {
    cin >> a[i];
    r[i % 2][i / 2] = a[i] + r[i % 2][i / 2 - 1];
  }
  if (n % 2 == 0) {
    ans = r[1][n / 2 - 1];
    rep(i, n / 2) { MAX(ans, r[0][i] + r[1][n / 2 - 1] - r[1][i]); }
    cout << ans;
  } else {
    ans = r[1][n / 2 - 1];
    rep(i, n / 2) {
      MAX(ans, r[0][n / 2] - a[i * 2]);
      // MAX(ans,r[0][i]+r[1][n/2 -1]-r[1][i]);
      MAX(ans, r[1][i] + r[0][n / 2] - r[0][i + 1]);
      REP(j, i + 1, n / 2) {
        MAX(ans, r[0][i] + r[1][n / 2 - 1] - r[1][i] -
                     (r[1][n / 2 - 1] - r[1][j]) + r[0][n / 2] - r[0][j + 1]);
      }
    }

    cout << ans;
  }
  EN;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
void solve(vector<int64> &A) {
  int n = A.size();
  int64 dp[n + 1][2];
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      dp[i][j] = 0;
    }
  }
  dp[2][1] = A[2];
  dp[2][0] = A[1];
  for (int i = 3; i < n; i++) {
    if (i % 2 == 1) { // odd.
      dp[i][0] =
          max(A[i - 1] + dp[i - 2][0],
              max(A[i - 1] + dp[i - 3][0],
                  max(A[i - 1] + dp[i - 3][1], A[i - 2] + dp[i - 3][0])));
      dp[i][1] = A[i] + max(dp[i - 2][0],
                            max(dp[i - 2][1], max(dp[i - 3][1], dp[i - 3][1])));
    } else {
      dp[i][0] = A[i - 1] + dp[i - 2][0];
      dp[i][1] = A[i] + max({dp[i - 1][0], dp[i - 2][0], dp[i - 2][1]});
    }
  }
  cout << max(dp[n - 1][0], dp[n - 1][1]) << endl;
  return;
}
int main() {
  int n;
  cin >> n;
  vector<int64> A(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  solve(A);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const int maxn = 2e5 + 10; //
map<int, map<int, int64>> dp;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int64 n;
  cin >> n;
  vector<int64> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  int64 ans = 0;
  dp[0][0] = 0;
  dp[0][1] = A[0];
  for (int i = 1; i < n; i++) {
    for (int taken = 1; taken <= ((i + 1) / 2); taken++) {
      dp[i][taken] =
          max(A[i] + ((i >= 2) ? dp[i - 2][taken - 1] : 0LL), dp[i - 1][taken]);
      // cout << "i = " << i << " taken = " << taken << " = " << dp[i][taken] <<
      // endl;
    }
  }
  cout << dp[n - 1][n / 2] << endl;
  return 0;
}