Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// #define for(i,a,b) for (int i=(a);i<(b);++i)
typedef long long ll;
typedef pair<ll, ll> P;
#define REP(i, n) for (long long i = 0; i < (long long)(n); i++)
#define pb push_back // vectorに要素追加
#define INF (ll)1e18

ll mod = 1e9 + 7;

ll dp[200000][3];
ll A[200000];
void rec1(ll i) {
  if (i == 0) {
    dp[i][0] = A[i * 2];
    dp[i][1] = A[i * 2 + 1];
  }
  dp[i + 1][0] = dp[i][0] + A[i * 2 + 2];
  dp[i + 1][1] = max(dp[i][0] + A[i * 2 + 2 + 1], dp[i][1] + A[i * 2 + 2 + 1]);
}

void rec2(ll i) {
  if (i == 0) {
    dp[i][0] = A[i * 2];
    dp[i][1] = A[i * 2 + 1];
    dp[i][2] = A[i * 2 + 2];
  }
  dp[i + 1][0] = dp[i][0] + A[i * 2 + 2];
  dp[i + 1][1] = max(dp[i][0] + A[i * 2 + 2 + 1], dp[i][1] + A[i * 2 + 2 + 1]);
  dp[i + 1][2] =
      max(max(dp[i][0] + A[i * 2 + 2 + 2], dp[i][1] + A[i * 2 + 2 + 2]),
          dp[i][2] + A[i * 2 + 2 + 2]);
}

// 検索タグ
int main() {
  // 入力
  ll N;
  cin >> N;
  REP(i, N) cin >> A[i];

  // 解法
  ll ans;
  if (N % 2 == 0) {
    REP(i, N / 2) rec1(i);
    ans = max(dp[N / 2 - 1][0], dp[N / 2 - 1][1]);
  } else {
    REP(i, N / 2) rec2(i);
    ans = max(dp[N / 2 - 1][0], dp[N / 2 - 1][1]);
    ans = max(ans, dp[N / 2 - 1][2]);
  }
  // 出力
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// #define for(i,a,b) for (int i=(a);i<(b);++i)
typedef long long ll;
typedef pair<ll, ll> P;
#define REP(i, n) for (long long i = 0; i < (long long)(n); i++)
#define pb push_back // vectorに要素追加
#define INF (ll)1e18

ll mod = 1e9 + 7;

// 検索タグ
int main() {
  // 入力
  ll N;
  cin >> N;
  ll A[N];
  REP(i, N) cin >> A[i];

  // 解法
  ll ans;
  if (N % 2 == 0) {
    ll A1[N / 2 + 1];
    ll A2[N / 2 + 1];
    A1[0] = 0;
    A2[0] = 0;
    REP(i, N / 2) { A1[i + 1] = A1[i] + A[i * 2]; }
    REP(i, N / 2) { A2[i + 1] = A2[i] + A[N - 1 - i * 2]; }
    ans = A2[N / 2];
    REP(i, N / 2 + 1) {
      ll tmp = A1[i] + A2[N / 2 - i];
      // printf("tmp: %lld\\n", tmp);
      if (tmp > ans)
        ans = tmp;
    }
  } else {

    ll A1[N / 2 + 1];
    ll A3[N / 2 + 1];
    A1[0] = 0;
    A3[0] = 0;
    REP(i, N / 2) { A1[i + 1] = A1[i] + A[i * 2]; }
    REP(i, N / 2) { A3[i + 1] = A3[i] + A[N - 1 - i * 2]; }
    ll A2_f[N / 2 + 1];
    ll A2_b[N / 2 + 1];
    A2_f[0] = 0;
    A2_b[0] = 0;
    REP(i, N / 2) {
      A2_f[i + 1] = A2_f[i] + A[i * 2 + 1];
      A2_b[i + 1] = A2_b[i] + A[N - 1 - i * 2 - 1];
    }
    ans = A3[N / 2];
    for (int l = 0; l <= N / 2; ++l) {
      // REP(l,N/2+1){ // A1からlこ選ぶ場合のA2を考える
      for (int r = l; r <= N / 2; ++r) {
        ll tmp =
            A1[l] + A2_f[N / 2] - A2_f[l] - A2_b[N / 2 - r] + A3[N / 2 - r];
        // printf("A1[%lld]: %lld, ", l, A1[l]);
        // printf("A2[%lld, %lld]: %lld, ", l, r, A2_f[N/2] - A2_f[l] - A2_b[N/2
        // - r]); printf("A3[%lld]: %lld, ", r, A3[N/2-r]); printf("tmp:
        // %lld\\n", tmp);
        if (tmp > ans)
          ans = tmp;
      }
    }
  }
  // 出力
  printf("%lld\\n", ans);
}