alexjercan/bugnet huggingface dataset:
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF 5000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  vector<vector<ll>> dp(N / 2 + 1, vector<ll>(2, -INF));
  dp.at(0).at(0) = 0;
  for (ll i = 0; i < N; ++i) {
    ll least, most;
    if (N % 2 == 0) {
      most = min(N / 2 - 1, (i + 1) / 2);
      least = min(N / 2 - 1, i / 2);
    } else {
      most = min(N / 2 - 1, (i + 1) / 2);
      least = max(0ll, (i - 1) / 2);
    }
    vector<vector<ll>> update(most - least + 2, vector<ll>(2, -INF));
    for (ll j = least; j <= most; ++j) {
      update.at(j - least + 1).at(1) =
          max(update.at(j - least + 1).at(1),
              max(dp.at(j + 1).at(1), dp.at(j).at(0) + A.at(i)));
      update.at(j - least).at(0) =
          max(update.at(j - least).at(0), max(dp.at(j).at(0), dp.at(j).at(1)));
    }
    for (ll j = 0; j < update.size(); ++j) {
      for (ll k = 0; k < 2; ++k) {
        dp.at(j + least).at(k) =
            max(update.at(j).at(k), dp.at(j + least).at(k));
      }
    }
  }
  cout << max(dp.at(N / 2).at(0), dp.at(N / 2).at(1)) << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 5000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  vector<vector<ll>> dp(N / 2 + 1, vector<ll>(2, -INF));
  dp.at(0).at(0) = 0;
  for (ll i = 0; i < N; ++i) {
    vector<vector<ll>> temp = dp;
    ll least, most;
    if (N % 2 == 0) {
      most = min(N / 2 - 1, (i + 1) / 2);
      least = min(N / 2 - 1, i / 2);
    } else {
      most = min(N / 2 - 1, (i + 1) / 2);
      least = max(0ll, (i - 1) / 2);
    }
    for (ll j = least; j <= most; ++j) {
      temp.at(j).at(0) =
          max(temp.at(j).at(0), max(dp.at(j).at(0), dp.at(j).at(1)));
      temp.at(j + 1).at(1) =
          max(temp.at(j + 1).at(1), dp.at(j).at(0) + A.at(i));
    }
    temp.at(N / 2) = max(temp.at(N / 2), dp.at(N / 2));
    dp = temp;
  }
  cout << max(dp.at(N / 2).at(0), dp.at(N / 2).at(1)) << "\\n";
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int n;
vector<int> a;
vector<map<int, long long>> dp;
long long ref(int i, int k) {
  auto itr = dp[i].find(k);
  if (itr == dp[i].end())
    return -1e18;
  return itr->second;
}
int main(void) {
  cin >> n;
  a.assign(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp.assign(n + 1, map<int, long long>());
  dp[0][0] = 0;
  dp[1][0] = 0;
  dp[1][1] = a[1];
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < 5; j++) {
      int k = (i + 1) / 2 - j;
      if (k < 0)
        continue;
      long long res = ref(i - 1, k);
      int cnt = 0;
      for (int l = i - 2; l >= 0; l--) {
        long long tmp = ref(l, k - 1) + a[i];
        if (tmp > res)
          res = tmp;
        if (++cnt > 5)
          break;
      }
      dp[i][k] = res;
      // cout << i << " " << k << " " << res << endl;
    }
  }
  cout << dp[n][n / 2] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int n;
vector<int> a;
vector<map<int, long long>> dp;
long long ref(int i, int k) {
  auto itr = dp[i].find(k);
  if (itr == dp[i].end())
    return -1e18;
  return itr->second;
}
int main(void) {
  cin >> n;
  a.assign(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp.assign(n + 1, map<int, long long>());
  dp[0][0] = 0;
  dp[1][0] = 0;
  dp[1][1] = a[1];
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < 5; j++) {
      int k = (i + 1) / 2 - j;
      if (k < 0)
        continue;
      long long res = ref(i - 1, k);
      for (int l = i - 2; l >= 0; l--) {
        long long tmp = ref(l, k - 1) + a[i];
        if (tmp > res)
          res = tmp;
      }
      dp[i][k] = res;
      // cout << i << " " << k << " " << res << endl;
    }
  }
  cout << dp[n][n / 2] << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define pb push_back
#define fi first
#define se second
const int N = 1e6 + 10;
const int M = 1e6;
const int mod7 = 1e9 + 7;
const int mod = 1e9 + 7;
void read(int &a) {
  a = 0;
  int d = 1;
  char ch;
  while (ch = getchar(), ch > '9' || ch < '0')
    if (ch == '-')
      d = -1;
  a = ch ^ 48;
  while (ch = getchar(), ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + (ch ^ 48);
  a *= d;
}
void read(ll &a) {
  a = 0;
  int d = 1;
  char ch;
  while (ch = getchar(), ch > '9' || ch < '0')
    if (ch == '-')
      d = -1;
  a = ch ^ 48;
  while (ch = getchar(), ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + (ch ^ 48);
  a *= d;
}
ll f1[N], f2[N], f3[N];
int a[N];
int main() {
  int n;
  read(n);
  for (re int i = 1; i <= n; i++)
    read(a[i]);
  if (n % 2 == 0) {
    f1[1] = a[1];
    f1[2] = a[2];
    for (re int i = 3; i <= n; i++) {
      if (i & 1)
        f1[i] = f1[i - 2] + a[i];
      else
        f1[i] = max(f1[i - 3], f1[i - 2]) + a[i];
    }
    printf("%lld\\n", max(f1[n - 1], f1[n]));
  } else {
    f1[1] = a[1];
    f2[1] = a[2];
    f3[1] = a[3];
    for (re int i = 2; i <= n / 2; i++) {
      f1[i] = f1[i - 1] + a[(i << 1) - 1];
      f2[i] = max(f1[i - 1], f2[i - 1]) + a[(i << 1)];
      f3[i] = max(f1[i - 1], max(f2[i - 1], f3[i - 1])) + a[(i << 1) + 1];
    }
    printf("%lld\\n", max(f1[n / 2], max(f2[n / 2], f3[n / 2])));
  }
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define pb push_back
#define fi first
#define se second
const int N = 1e5 + 10;
const int M = 1e6;
const int mod7 = 1e9 + 7;
const int mod = 1e9 + 7;
void read(int &a) {
  a = 0;
  int d = 1;
  char ch;
  while (ch = getchar(), ch > '9' || ch < '0')
    if (ch == '-')
      d = -1;
  a = ch ^ 48;
  while (ch = getchar(), ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + (ch ^ 48);
  a *= d;
}
void read(ll &a) {
  a = 0;
  int d = 1;
  char ch;
  while (ch = getchar(), ch > '9' || ch < '0')
    if (ch == '-')
      d = -1;
  a = ch ^ 48;
  while (ch = getchar(), ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + (ch ^ 48);
  a *= d;
}
ll f1[N], f2[N], f3[N];
int a[N];
int main() {
  int n;
  read(n);
  for (re int i = 1; i <= n; i++)
    read(a[i]);
  if (n % 2 == 0) {
    f1[1] = a[1];
    f1[2] = a[2];
    for (re int i = 3; i <= n; i++) {
      if (i & 1)
        f1[i] = f1[i - 2] + a[i];
      else
        f1[i] = max(f1[i - 3], f1[i - 2]) + a[i];
    }
    printf("%lld\\n", max(f1[n - 1], f1[n]));
  } else {
    f1[1] = a[1];
    f2[1] = a[2];
    f3[1] = a[3];
    for (re int i = 2; i <= n / 2; i++) {
      f1[i] = f1[i - 1] + a[(i << 1) - 1];
      f2[i] = max(f1[i - 1], f2[i - 1]) + a[(i << 1)];
      f3[i] = max(f1[i - 1], max(f2[i - 1], f3[i - 1])) + a[(i << 1) + 1];
    }
    printf("%lld\\n", max(f1[n / 2], max(f2[n / 2], f3[n / 2])));
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long int;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  vector<ll> a(n);
  for (auto &e : a)
    cin >> e;
  vector<vector<ll>> dp(n, vector<ll>(50, -1e18));
  dp[0][0 - 1 / 2 + 25] = 0;
  dp[0][1 - 1 / 2 + 25] = a[0];
  dp[1][0 - 2 / 2 + 25] = 0;
  dp[1][1 - 2 / 2 + 25] = a[1];
  for (ll i = 1; i < n; i++) {
    for (ll j = max(0ll, (i + 1) / 2 - 2); j <= min(n - 1, (i + 1) / 2 + 2);
         j++) {
      dp[i][j - (i + 1) / 2 + 25] =
          max(dp[i][j - (i + 1) / 2 + 25], dp[i - 1][j - i / 2 + 25]);
      if (i - 2 >= 0 && j - 1 >= 0)
        dp[i][j - (i + 1) / 2 + 25] =
            max(dp[i][j - (i + 1) / 2 + 25],
                dp[i - 2][j - 1 - (i - 1) / 2 + 25] + a[i]);
    }
  }
  cout << dp[n - 1][n / 2 - n / 2 + 25] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long int;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  vector<ll> a(n);
  for (auto &e : a)
    cin >> e;
  vector<vector<ll>> dp(n, vector<ll>(n, -1e18));
  dp[0][0] = 0;
  dp[0][1] = a[0];
  dp[1][0] = 0;
  dp[1][1] = a[1];
  for (ll i = 1; i < n; i++) {
    for (ll j = max(0ll, (i + 1) / 2 - 2); j <= min(n - 1, (i + 1) / 2 + 2);
         j++) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      if (i - 2 >= 0 && j - 1 >= 0)
        dp[i][j] = max(dp[i][j], dp[i - 2][j - 1] + a[i]);
    }
  }
  cout << dp[n - 1][n / 2] << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define Sort(v, n) sort(v, v + n);
#define VSort(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define cY cout << "Yes";
#define cN cout << "No";
#define cA(a) cout << a;
const ll MOD = 1000000007;

int main() {
  ll n, ans = 0;
  cin >> n;
  ll a[n];
  rep(i, n) cin >> a[i];
  // assert(n%2 == 0);
  if (n % 2 == 0) {
    ll dp[n];
    dp[0] = a[0];
    dp[1] = a[1];
    for (int i = 2; i < n; i++) {
      if (i % 2 == 0) {
        dp[i] = dp[i - 2] + a[i];
      } else {
        dp[i] = max(dp[i - 3], dp[i - 2]) + a[i];
      }
    }
    ans = max(dp[n - 1], dp[n - 2]);
  } else {
    vector<ll> l(n / 2), m(n / 2), r(n / 2);
    l[0] = a[0];
    m[0] = a[1];
    r[0] = a[2];
    for (int i = 1; i < n / 2; i++) {
      l[i] = l[i - 1] + a[2 * i];
      m[i] = max(l[i - 1], m[i - 1]) + a[2 * i + 1];
      r[i] = max(l[i - 1], max(m[i - 1], r[i - 1])) + a[i * 2 + 2];
    }
    ans = max(l[n / 2 - 1], max(m[n / 2 - 1], r[n / 2 - 1]));
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define Sort(v, n) sort(v, v + n);
#define VSort(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define cY cout << "Yes";
#define cN cout << "No";
#define cA(a) cout << a;
const ll MOD = 1000000007;

int main() {
  ll n, ans = 0;
  cin >> n;
  ll a[n];
  rep(i, n) cin >> a[i];
  assert(n % 2 == 0);
  if (n % 2 == 0) {
    ll dp[n];
    dp[0] = a[0];
    dp[1] = a[1];
    for (int i = 2; i < n; i++) {
      if (i % 2 == 0) {
        dp[i] = dp[i - 2] + a[i];
      } else {
        dp[i] = max(dp[i - 3], dp[i - 2]) + a[i];
      }
    }
    ans = max(dp[n - 1], dp[n - 2]);
  } else {
  }
  cout << ans;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef vector<ll> VI;
typedef vector<VI> VVI;

int main() {
  int N;
  cin >> N;
  VI a(N);
  rep(i, N) { cin >> a.at(i); }
  if (N == 2) {
    cout << max(a.at(0), a.at(1)) << endl;
    return 0;
  }
  ll maxi = max(max(a.at(0), a.at(1)), a.at(2));
  ll left = a.at(0);
  ll emax = max(a.at(0), a.at(1));
  for (int i = 4; i < N + 1; i++) {
    if (i % 2 == 0) {
      left += a.at(i - 2);
      emax = max(emax + a.at(i - 1), left);
    } else {
      maxi = max(emax, maxi + a.at(i - 1));
    }
  }
  if (N % 2 == 0) {
    cout << emax << endl;
  } else {
    cout << maxi << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef vector<ll> VI;
typedef vector<VI> VVI;

int main() {
  int N;
  cin >> N;
  VI a(N);
  rep(i, N) { cin >> a.at(i); }
  ll maxi = max(max(a.at(0), a.at(1)), a.at(2));
  ll left = a.at(0);
  ll emax = max(a.at(0), a.at(1));
  for (int i = 4; i < N + 1; i++) {
    if (i % 2 == 0) {
      left += a.at(i - 2);
      emax = max(emax + a.at(i - 1), left);
    } else {
      maxi = max(emax, maxi + a.at(i - 1));
    }
  }
  if (N % 2 == 0) {
    cout << emax << endl;
  } else {
    cout << maxi << endl;
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using state = pair<int, int>;

int64_t dp(state stat, const vector<int64_t> &data, map<state, int64_t> *memo) {
  if (memo->count(stat)) {
    return memo->at(stat);
  } else {
    const int N = data.size();
    int rem, id;
    tie(rem, id) = stat;

    if (rem == 0) {
      return memo->operator[](stat) = 0;
    } else if (2 * rem - 1 > N - id) {
      return memo->operator[](stat) = INT64_MIN;
    } else {
      state pick = make_pair(rem - 1, id + 2);
      state nopick = make_pair(rem, id + 1);
      return memo->operator[](stat) = max(data.at(id) + dp(pick, data, memo),
                                          dp(nopick, data, memo));
    }
  }
}

int main() {
  int N;
  cin >> N;

  vector<int64_t> data(N);
  for (int i = 0; i < N; i++) {
    cin >> data.at(i);
  }
  map<state, int64_t> memo;

  cout << dp(make_pair(N / 2, 0), data, &memo) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using state = pair<int, int>;

int64_t dp(state stat, const vector<int64_t> &data, map<state, int64_t> *memo) {
  if (memo->count(stat)) {
    return memo->at(stat);
  } else {
    const int N = data.size();
    int rem, id;
    tie(rem, id) = stat;

    if (rem == 0) {
      return memo->operator[](stat) = 0;
    } else if (2 * rem - 1 == N - id) {
      int64_t sum_odd = 0;
      for (int i = id; i < N; i += 2) {
        sum_odd += data.at(i);
      }

      return memo->operator[](stat) = sum_odd;
    } else if (2 * rem - 1 > N - id) {
      return memo->operator[](stat) = INT64_MIN;
    } else {
      state pick = make_pair(rem - 1, id + 2);
      state nopick = make_pair(rem, id + 1);
      return memo->operator[](stat) = max(data.at(id) + dp(pick, data, memo),
                                          dp(nopick, data, memo));
    }
  }
}

int main() {
  int N;
  cin >> N;

  vector<int64_t> data(N);
  for (int i = 0; i < N; i++) {
    cin >> data.at(i);
  }
  map<state, int64_t> memo;

  cout << dp(make_pair(N / 2, 0), data, &memo) << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
累積和でとってTLE
DPでとる?
1 2 3 4 5 6 7
DP[3]=3
DP[4]=6 =
DP[5]=8 = max(DP[3]+A[4],DP[4])
DP[6]=12 = max(DP[4]+A[5]
DP[n]=max(DP[n-1],DP[1]+A[3])
みたいなかんじ
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, ans = -1e15, tmp = -1e15;
  cin >> N;
  vector<ll> A(N, 0), O(N + 1, 0), DP(N + 1, -1e15);
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    A[i] = a;
    if (i % 2 == 0)
      O[i + 1] = a;
    if (i <= 2)
      DP[i + 1] = max(DP[i], a);
    // DP[4]、2個とる。DP[2]+aかO[1]+O[3]
    // DP[5]、2個とる。DP[4]かDP[3]+a
    // DP[6]、3個とる。DP[4]+aかO[1]+O[3]+O[5](aを使わないとなると奇数番目をとるだけ）
    // DP[7]、3個とる。DP[6]かDP[5]+a
  }
  // 奇数累積和
  for (int i = 1; i <= N; i++)
    O[i] = O[i - 1] + O[i];
  // DPつくる
  for (int i = 4; i <= N; i++) {
    if (i % 2 == 0)
      DP[i] = max(DP[i - 2] + A[i - 1], O[i - 1]);
    else
      DP[i] = max(DP[i - 1], DP[i - 2] + A[i - 1]);
  }
  cout << DP[N] << endl;
}


Error: TLE

Fixed submission:

// -10 0 10 20 4
// 0 20
// 0 100 -190 1 20000 40000
//

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, ans = -1e15, tmp = -1e15;
  cin >> N;
  vector<ll> A(N, 0), E((N + 1) / 2 + 1, 0), O((N + 1) / 2 + 1, 0);
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    A[i] = a;
    if (i % 2 == 1)
      E[i / 2 + 1] = a;
    else if (i % 2 == 0)
      O[i / 2 + 1] = a;
  }
  for (int i = 0; i <= N / 2; i++) {
    E[i + 1] = E[i] + E[i + 1];
    O[i + 1] = O[i] + O[i + 1];
    //    cout<<"i:"<<i<<" E:"<<E[i+1]<<" O:"<<O[i+1]<<endl;
  }
  if (N % 2 == 0) {
    for (int i = N / 2; i >= 0; i--) {
      tmp = O[i] + E[N / 2] - E[i];
      ans = max(tmp, ans);
    }
  } else {
    // 奇数→偶数→奇数
    for (int i = N / 2; i >= 0; i--) {
      for (int j = N / 2 - i; j >= 0; j--) {
        tmp = O[i] + E[i + j] - E[i] + O[N / 2 + 1] - O[i + j + 1];
        ans = max(ans, tmp);
        //        cout<<"i:"<<i<<" j:"<<j<<" t:"<<tmp<<endl;
      }
    }
  }
  cout << ans << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#ifndef LOCAL
#define endl '\\n'
#endif

const int mod = 1e9 + 7;

int pwr(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * 1LL * a) % mod;
    a = (a * 1LL * a) % mod;
    b >>= 1;
  }
  return ans;
}

const int N = 2e5 + 5;
int a[N];
ll pre_e[N], pre_o[N];
ll dp_even[N], dp_odd[N];

void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pre_e[i] = pre_e[i - 1];
    pre_o[i] = pre_o[i - 1];
    if (i & 1)
      pre_o[i] += a[i];
    else
      pre_e[i] += a[i];
  }
  ll maxi = -1e18;
  dp_even[2] = max(a[1], a[2]);
  for (int i = 4; i <= n; i += 2) {
    maxi = max(maxi, pre_o[i - 3] - pre_e[i - 1]);
    dp_even[i] = pre_e[i];
    dp_even[i] = max(dp_even[i], pre_e[i] + maxi);
  }
  if (n % 2 == 0) {
    cout << dp_even[n];
    return;
  }
  for (int i = 3; i <= n; i += 2) {
    dp_odd[i] = -1e18;
    dp_odd[i] = max(dp_odd[i], pre_o[i]);
    dp_odd[i] = max(dp_odd[i], dp_odd[i - 2] + a[i]);
    dp_odd[i] = max(dp_odd[i], dp_even[i - 3] + a[i - 1]);
  }
  cout << dp_odd[n];
}

int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  // cin >> t;
  t = 1;
  for (int tt = 1; tt <= t; tt++) {
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#ifndef LOCAL
#define endl '\\n'
#endif

const int mod = 1e9 + 7;

int pwr(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * 1LL * a) % mod;
    a = (a * 1LL * a) % mod;
    b >>= 1;
  }
  return ans;
}

const int N = 1e5 + 5;
int a[N];
ll pre_e[N], pre_o[N];
ll dp_even[N], dp_odd[N];

void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pre_e[i] = pre_e[i - 1];
    pre_o[i] = pre_o[i - 1];
    if (i & 1)
      pre_o[i] += a[i];
    else
      pre_e[i] += a[i];
  }
  ll maxi = -1e18;
  for (int i = 2; i <= n; i += 2) {
    if (i >= 4) {
      maxi = max(maxi, pre_o[i - 3] - pre_e[i - 1]);
    }
    dp_even[i] = pre_e[i];
    dp_even[i] = max(dp_even[i], pre_e[i] + maxi);
  }
  if (n % 2 == 0) {
    cout << dp_even[n];
    return;
  }
  for (int i = 3; i <= n; i += 2) {
    dp_odd[i] = -1e18;
    dp_odd[i] = max(dp_odd[i], pre_o[i]);
    dp_odd[i] = max(dp_odd[i], dp_odd[i - 2] + a[i]);
    dp_odd[i] = max(dp_odd[i], dp_even[i - 3] + a[i - 1]);
  }
  cout << dp_odd[n];
}

int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  // cin >> t;
  t = 1;
  for (int tt = 1; tt <= t; tt++) {
    solve();
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long int ll;
#define mod 1000000007
#define sz 200009
ll n, m, i, j, k, l, t, arr[sz], dp[sz], brr[sz], crr[sz], drr[sz], err[sz];
int main() {
  scanf("%lld", &n);
  for (i = 1; i <= n; i++)
    scanf("%lld", &arr[i]);
  for (i = 1; i <= n; i++) {
    if (i % 2 == 1) {
      dp[i] = dp[i - 2] + arr[i];
      brr[i] =
          dp[i - 1] > brr[i - 2] + arr[i] ? dp[i - 1] : brr[i - 2] + arr[i];
      crr[i] =
          brr[i - 1] > crr[i - 2] + arr[i] ? brr[i - 1] : crr[i - 2] + arr[i];
    } else {
      dp[i] = dp[i - 2] + arr[i] > dp[i - 1] ? dp[i - 2] + arr[i] : dp[i - 1];
      brr[i] =
          brr[i - 1] > brr[i - 2] + arr[i] ? brr[i - 1] : brr[i - 2] + arr[i];
      crr[i] =
          crr[i - 1] > crr[i - 2] + arr[i] ? crr[i - 1] : crr[i - 2] + arr[i];
    }
  }
  if (n % 2 == 1)
    k = brr[n];
  else
    k = dp[n];
  printf("%lld\\n", k);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define sz 200009
ll n, i, k, l, arr[sz], dp[sz][2][4], mx, dp1[sz][2][4];
ll dpdp(ll node, ll val, ll ind) {
  if (node > n) {
    if (val == 0)
      return 0;
    else
      return mx;
  }
  ll t = (n - node + 2) / 2;
  ll ret = dp[node][ind][t - val];
  ll ret1 = dp1[node][ind][t - val];
  if (ret1 != -1)
    return ret;
  ret1 = 1;
  if (ind == 1)
    return ret = dpdp(node + 1, val, 0);
  ll j = n - node + 2;
  j /= 2;
  if (n % 2 == node % 2 && j == val)
    return ret = dpdp(node + 1, val - 1, 1) + arr[node];
  return ret = max(arr[node] + dpdp(node + 1, val - 1, 1),
                   dpdp(node + 1, val, 0));
}
int main() {
  scanf("%lld", &n);
  for (i = 1; i <= n; i++)
    scanf("%lld", &arr[i]), mx = min(mx, arr[i]);
  memset(dp1, -1, sizeof dp1);
  l = n / 2;
  k = dpdp(1, l, 0);
  printf("%lld\\n", k);
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

// https://atcoder.jp/contests/abc159/submissions/14840254
const long long OO = (long long)1e18;

const int N = 200008;
int n;
long long a[N];

long long dp[N][3];
// dp[i][j] is maximum possible sum choosing (i+1-j)/2 elements from first i
// elements. This is NOT a floor! So half the dp elements will be impossible
// solely because it's impossible to choose half an element.

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

  dp[0][0] = -OO;
  dp[0][1] = 0;
  dp[0][2] = -OO;
  dp[1][0] = a[0];
  dp[1][1] = -OO;
  dp[1][2] = 0;

  for (int i = 2; i <= n; i++) {
    // note that we are computing dp[i] using a[i-1] and dp[i-1]
    for (int j = 0; j < 3; j++) {
      // take a[i-1]
      dp[i][j] = dp[i - 2][j] + a[i - 1];

      if (j) {
        // we have slack; don't take a[i-1]
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]);
      }
    }
  }

  cout << dp[n][n % 2 == 0 ? 1 : 2];
}


Error: WA

Fixed submission:

#include <iostream>
using namespace std;

// https://atcoder.jp/contests/abc159/submissions/14840254
const long long OO = (long long)1e18;

const int N = 100008;
int n;
long long a[N];

long long dp[N][3];
// dp[i][j] is maximum possible sum choosing (i+1-j)/2 elements from first i
// elements. This is NOT a floor! So half the dp elements will be impossible
// solely because it's impossible to choose half an element.

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

  dp[0][0] = -OO;
  dp[0][1] = 0;
  dp[0][2] = -OO;
  dp[1][0] = a[0];
  dp[1][1] = -OO;
  dp[1][2] = 0;

  for (int i = 2; i <= n; i++) {
    // note that we are computing dp[i] using a[i-1] and dp[i-1]
    for (int j = 0; j < 3; j++) {
      // take a[i-1]
      dp[i][j] = dp[i - 2][j] + a[i - 1];

      if (j) {
        // we have slack; don't take a[i-1]
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]);
      }
    }
  }

  cout << dp[n][n % 2 == 0 ? 1 : 2];
}