Problem p01229 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef pair<P, P> Edge;
typedef long long ll;

const double PI = 4 * atan(1.0);

#define RED_FILE

#ifdef RED_FILE
ifstream ifs("input.txt");
ofstream ofs("output.txt");
#define cin ifs
#define cout ofs
#endif

int dp[2][321];

int main() {

  int t;
  cin >> t;
  while (t--) {
    memset(dp, 0, sizeof(dp));
    int n;
    cin >> n;
    int a[101];
    for (int i = 0; i < n; i++)
      cin >> a[i];
    if (n == 1) {
      cout << a[0] << endl;
      continue;
    }
    dp[0][a[0]] = a[1];
    for (int i = 1; i < n - 1; i++) {
      memset(dp[i % 2], 0, sizeof(dp[i % 2]));
      for (int j = 0; j < 321; j++) {
        // ツ遷ツ暗堋つウツつケツづゥ
        if (dp[(i - 1) % 2][j] - 1 >= 0)
          dp[i % 2][dp[(i - 1) % 2][j] - 1] =
              max(dp[i % 2][dp[(i - 1) % 2][j] - 1], j + a[i + 1]);
        // ツ遷ツ暗堋つウツつケツづ按つ「
        dp[i % 2][dp[(i - 1) % 2][j]] =
            max(dp[i % 2][dp[(i - 1) % 2][j]], a[i + 1]);
      }
    }
    int maxNum = 0;
    for (int i = 0; i < 321; i++)
      maxNum = max(dp[(n - 2) % 2][i], maxNum);
    cout << maxNum << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef pair<P, P> Edge;
typedef long long ll;

const double PI = 4 * atan(1.0);

// #define RED_FILE

#ifdef RED_FILE
ifstream ifs("input.txt");
ofstream ofs("output.txt");
#define cin ifs
#define cout ofs
#endif

int dp[2][321];

int main() {

  int t;
  cin >> t;
  while (t--) {
    memset(dp, 0, sizeof(dp));
    int n;
    cin >> n;
    int a[101];
    for (int i = 0; i < n; i++)
      cin >> a[i];
    if (n == 1) {
      cout << a[0] << endl;
      continue;
    }
    dp[0][a[0]] = a[1];
    for (int i = 1; i < n - 1; i++) {
      memset(dp[i % 2], 0, sizeof(dp[i % 2]));
      for (int j = 0; j < 321; j++) {
        // ツ遷ツ暗堋つウツつケツづゥ
        if (dp[(i - 1) % 2][j] - 1 >= 0)
          dp[i % 2][dp[(i - 1) % 2][j] - 1] =
              max(dp[i % 2][dp[(i - 1) % 2][j] - 1], j + a[i + 1]);
        // ツ遷ツ暗堋つウツつケツづ按つ「
        dp[i % 2][dp[(i - 1) % 2][j]] =
            max(dp[i % 2][dp[(i - 1) % 2][j]], a[i + 1]);
      }
    }
    int maxNum = 0;
    for (int i = 0; i < 321; i++)
      maxNum = max(dp[(n - 2) % 2][i], maxNum);
    cout << maxNum << endl;
  }
  return 0;
}