Problem p00480 (C++) — buggy submission, verdict: Runtime Error


// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int DX[] = {0, 1, 0, -1};
const int DY[] = {-1, 0, 1, 0};

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

int main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, result;
  LL ans = 0;
  cin >> n;
  VI numbers(n - 1);
  REP(i, n - 1) { cin >> numbers[i]; }
  cin >> result;

  vector<vector<LL>> dp(n - 1, vector<LL>(21));
  dp[0][numbers[0]] = 1;
  REP(i, n - 2) {
    REP(j, 21) {
      LL value;
      if (20 >= j + numbers[i + 1]) {
        value = dp[i + 1][j + numbers[i + 1]] += dp[i][j];
      }
      if (j - numbers[i + 1] >= 0) {
        value = dp[i + 1][j - numbers[i + 1]] += dp[i][j];
      }
    }
  }
  cout << dp[n - 2][result] << endl;

  return 0;
}

Error: 0
i = 0
j = 0
value = 0
i = 0
j = 1
value = 0
i = 0
j = 2
value = 0
i = 0
j = 3
value = 0
i = 0
j = 3
value = 0
i = 0
j = 4
value = 0
i = 0
j = 4
value = 0
i = 0
j = 5
value = 0
i = 0
j = 5
value = 0
i = 0
j = 6
value = 0
i = 0
j = 6
value = 0
i = 0
j = 7
value = 0
i = 0
j = 7
value = 0
i = 0
j = 8
value = 1
i = 0
j = 8
value = 1
i = 0
j = 9
value = 0
i = 0
j = 9
value = 0
i = 0
j = 10
value = 0
i = 0
j = 10
value = 0
i = 0
j = 11
value = 0
i = 0
j = 11
value = 0
i = 0
j = 12
value = 0
i = 0
j = 12
value = 0
i = 0
j = 13
value = 0
i = 0
j = 13
value = 0
i = 0
j = 14
value = 0
i = 0
j = 14
value = 1
i = 0
j = 15
value = 0
i = 0
j = 15
value = 0
i = 0
j = 16
value = 0
i = 0
j = 16
value = 0
i = 0
j = 17
value = 0
i = 0
j = 17
value = 0
i = 0
j = 18
value = 0
i = 0
j = 19
value = 0
i = 0
j = 20
value = 0
i = 1
j = 0
value = 0
i = 1
j = 1
value = 0
i = 1
j = 2
value = 0
i = 1
j = 2
value = 0
i = 1
j = 3
value = 0
i = 1
j = 3
value = 0
i = 1
j = 4
value = 0
i = 1
j = 4
value = 0
i = 1
j = 5
value = 1
i = 1
j = 5
value = 1
i = 1
j = 6
value = 0
i = 1
j = 6
value = 0
i = 1
j = 7
value = 0
i = 1
j = 7
value = 0
i = 1
j = 8
value = 0
i = 1
j = 8
value = 0
i = 1
j = 9
value = 0
i = 1
j = 9
value = 1
i = 1
j = 10
value = 0
i = 1
j = 10
value = 0
i = 1
j = 11
value = 1
i = 1
j = 11
value = 1
i = 1
j = 12
value = 0
i = 1
j = 12
value = 0
i = 1
j = 13
value = 0
i = 1
j = 13
value = 0
i = 1
j = 14
value = 0
i = 1
j = 14
value = 0
i = 1
j = 15
value = 0
i = 1
j = 15
value = 1
i = 1
j = 16
value = 0
i = 1
j = 16
value = 0
i = 1
j = 17
value = 0
i = 1
j = 17
value = 0
i = 1
j = 18
value = 0
i = 1
j = 18
value = 0
i = 1
j = 19
value = 0
i = 1
j = 20
value = 0
i = 2
j = 0
value = 0
i = 2
j = 1
value = 0
i = 2
j = 2
value = 0
i = 2
j = 3
value = 1
i = 2
j = 4
value = 0
i = 2
j = 4
value = 0
i = 2
j = 5
value = 0
i = 2
j = 5
value = 0
i = 2
j = 6
value = 0
i = 2
j = 6
value = 0
i = 2
j = 7
value = 1
i = 2
j = 7
value = 1
i = 2
j = 8
value = 0
i = 2
j = 8
value = 0
i = 2
j = 9
value = 1
i = 2
j = 9
value = 1
i = 2
j = 10
value = 0
i = 2
j = 10
value = 0
i = 2
j = 11
value = 0
i = 2
j = 11
value = 1
i = 2
j = 12
value = 0
i = 2
j = 12
value = 0
i = 2
j = 13
value = 1
i = 2
j = 13
value = 1
i = 2
j = 14
value = 0
i = 2
j = 14
value = 0
i = 2
j = 15
value = 0
i = 2
j = 15
value = 1
i = 2
j = 16
value = 0
i = 2
j = 16
value = 0
i = 2
j = 17
value = 1
i = 2
j = 18
value = 0
i = 2
j = 19
value = 0
i = 2
j = 20
value = 0
i = 3
j = 0
value = 0
i = 3
j = 1
value = 0
i = 3
j = 2
value = 0
i = 3
j = 3
value = 1
i = 3
j = 4
value = 0
i = 3
j = 5
value = 1
i = 3
j = 6
value = 0
i = 3
j = 7
value = 1
i = 3
j = 8
value = 0
i = 3
j = 8
value = 0
i = 3
j = 9
value = 1
i = 3
j = 9
value = 1
i = 3
j = 10
value = 0
i = 3
j = 10
value = 0
i = 3
j = 11
value = 1
i = 3
j = 11
value = 1
i = 3
j = 12
value = 0
i = 3
j = 12
value = 0
i = 3
j = 13
value = 1
i = 3
j = 14
value = 0
i = 3
j = 15
value = 0
i = 3
j = 16
value = 0
i = 3
j = 17
value = 1
i = 3
j = 18
value = 0
i = 3
j = 19
value = 1
i = 3
j = 20
value = 0
i = 4
j = 0
value = 0
i = 4
j = 1
value = 1
i = 4
j = 2
value = 0
i = 4
j = 3
value = 1
i = 4
j = 4
value = 0
i = 4
j = 5
value = 1
i = 4
j = 6
value = 0
i = 4
j = 7
value = 0
i = 4
j = 7
value = 0
i = 4
j = 8
value = 0
i = 4
j = 8
value = 0
i = 4
j = 9
value = 1
i = 4
j = 9
value = 1
i = 4
j = 10
value = 0
i = 4
j = 10
value = 0
i = 4
j = 11
value = 1
i = 4
j = 11
value = 1
i = 4
j = 12
value = 0
i = 4
j = 12
value = 0
i = 4
j = 13
value = 1
i = 4
j = 13
value = 1
i = 4
j = 14
value = 0
i = 4
j = 15
value = 2
i = 4
j = 16
value = 0
i = 4
j = 17
value = 2
i = 4
j = 18
value = 0
i = 4
j = 19
value = 2
i = 4
j = 20
value = 0
i = 5
j = 0
value = 0
i = 5
j = 1
value = 0
i = 5
j = 2
value = 1
i = 5
j = 2
value = 1
i = 5
j = 3
value = 0
i = 5
j = 3
value = 0
i = 5
j = 4
value = 1
i = 5
j = 4
value = 1
i = 5
j = 5
value = 0
i = 5
j = 5
value = 0
i = 5
j = 6
value = 1
i = 5
j = 6
value = 2
i = 5
j = 7
value = 0
i = 5
j = 7
value = 0
i = 5
j = 8
value = 2
i = 5
j = 8
value = 3
i = 5
j = 9
value = 0
i = 5
j = 9
value = 0
i = 5
j = 10
value = 2
i = 5
j = 10
value = 3
i = 5
j = 11
value = 0
i = 5
j = 11
value = 0
i = 5
j = 12
value = 2
i = 5
j = 12
value = 4
i = 5
j = 13
value = 0
i = 5
j = 13
value = 0
i = 5
j = 14
value = 0
i = 5
j = 14
value = 2
i = 5
j = 15
value = 0
i = 5
j = 15
value = 0
i = 5
j = 16
value = 1
i = 5
j = 16
value = 3
i = 5
j = 17
value = 0
i = 5
j = 17
value = 0
i = 5
j = 18
value = 1
i = 5
j = 18
value = 1
i = 5
j = 19
value = 0
i = 5
j = 20
value = 2
i = 6
j = 0
value = 1
i = 6
j = 1
value = 0
i = 6
j = 2
value = 1
i = 6
j = 3
value = 0
i = 6
j = 4
value = 2
i = 6
j = 4
value = 2
i = 6
j = 5
value = 0
i = 6
j = 5
value = 0
i = 6
j = 6
value = 3
i = 6
j = 6
value = 3
i = 6
j = 7
value = 0
i = 6
j = 7
value = 0
i = 6
j = 8
value = 3
i = 6
j = 8
value = 4
i = 6
j = 9
value = 0
i = 6
j = 9
value = 0
i = 6
j = 10
value = 4
i = 6
j = 10
value = 5
i = 6
j = 11
value = 0
i = 6
j = 11
value = 0
i = 6
j = 12
value = 2
i = 6
j = 12
value = 4
i = 6
j = 13
value = 0
i = 6
j = 13
value = 0
i = 6
j = 14
value = 3
i = 6
j = 14
value = 6
i = 6
j = 15
value = 0
i = 6
j = 15
value = 0
i = 6
j = 16
value = 1
i = 6
j = 16
value = 4
i = 6
j = 17
value = 0
i = 6
j = 18
value = 6
i = 6
j = 19
value = 0
i = 6
j = 20
value = 3
i = 7
j = 0
value = 2
i = 7
j = 0
value = 4
i = 7
j = 1
value = 0
i = 7
j = 1
value = 0
i = 7
j = 2
value = 3
i = 7
j = 2
value = 6
i = 7
j = 3
value = 0
i = 7
j = 3
value = 0
i = 7
j = 4
value = 4
i = 7
j = 4
value = 8
i = 7
j = 5
value = 0
i = 7
j = 5
value = 0
i = 7
j = 6
value = 5
i = 7
j = 6
value = 10
i = 7
j = 7
value = 0
i = 7
j = 7
value = 0
i = 7
j = 8
value = 4
i = 7
j = 8
value = 8
i = 7
j = 9
value = 0
i = 7
j = 9
value = 0
i = 7
j = 10
value = 6
i = 7
j = 10
value = 12
i = 7
j = 11
value = 0
i = 7
j = 11
value = 0
i = 7
j = 12
value = 4
i = 7
j = 12
value = 8
i = 7
j = 13
value = 0
i = 7
j = 13
value = 0
i = 7
j = 14
value = 6
i = 7
j = 14
value = 12
i = 7
j = 15
value = 0
i = 7
j = 15
value = 0
i = 7
j = 16
value = 3
i = 7
j = 16
value = 6
i = 7
j = 17
value = 0
i = 7
j = 17
value = 0
i = 7
j = 18
value = 3
i = 7
j = 18
value = 6
i = 7
j = 19
value = 0
i = 7
j = 19
value = 0
i = 7
j = 20
value = 1
i = 7
j = 20
value = 2
i = 8
j = 0
value = 4
i = 8
j = 1
value = 0
i = 8
j = 2
value = 6
i = 8
j = 3
value = 0
i = 8
j = 4
value = 8
i = 8
j = 5
value = 0
i = 8
j = 6
value = 10
i = 8
j = 7
value = 0
i = 8
j = 8
value = 8
i = 8
j = 8
value = 8
i = 8
j = 9
value = 0
i = 8
j = 9
value = 0
i = 8
j = 10
value = 12
i = 8
j = 10
value = 12
i = 8
j = 11
value = 0
i = 8
j = 11
value = 0
i = 8
j = 12
value = 8
i = 8
j = 12
value = 8
i = 8
j = 13
value = 0
i = 8
j = 14
value = 12
i = 8
j = 15
value = 0
i = 8
j = 16
value = 10
i = 8
j = 17
value = 0
i = 8
j = 18
value = 12
i = 8
j = 19
value = 0
i = 8
j = 20
value = 10


Fixed submission:


// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int DX[] = {0, 1, 0, -1};
const int DY[] = {-1, 0, 1, 0};

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

int main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, result;
  LL ans = 0;
  cin >> n;
  VI numbers(n - 1);
  REP(i, n - 1) { cin >> numbers[i]; }
  cin >> result;

  vector<vector<LL>> dp(n - 1, vector<LL>(21));
  dp[0][numbers[0]] = 1;
  REP(i, n - 2) {
    REP(j, 21) {
      LL value;
      if (20 >= j + numbers[i + 1]) {
        value = dp[i + 1][j + numbers[i + 1]] += dp[i][j];
        dump(i);
        dump(j);
        dump(value);
      }
      if (j - numbers[i + 1] >= 0) {
        value = dp[i + 1][j - numbers[i + 1]] += dp[i][j];
        dump(i);
        dump(j);
        dump(value);
      }
    }
  }
  cout << dp[n - 2][result] << endl;

  return 0;
}