Problem p03064 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define x first
#define y second
#define all(aaa) (aaa).begin(), (aaa).end()
#define REP(i, sss, eee) for (int i = sss; i <= eee; ++i)
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define N 998244353
#define inf 1000000000000000000
using namespace __gnu_pbds;
using namespace std;
template <class T> T MAX(T a, T b) { return ((a > b) ? a : b); }
template <class T> T MIN(T a, T b) { return ((a < b) ? a : b); }
template <class T> T MOD(T a) { return ((a >= 0) ? a : (-1 * a)); }
template <class T> void SWAP(T &a, T &b) {
  T temp = a;
  a = b;
  b = temp;
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set; // for multiset use less_equal above

inline int ex(int a, int b) {
  if (b == 0 || a == 1) {
    return 1;
  }
  if (a == 0 || b == 1) {
    return a;
  }
  int tmp = ex(a, (b / 2));
  if (b % 2) {
    return ((((tmp * tmp) % N) * a) % N);
  } else {
    return ((tmp * tmp) % N);
  }
}
inline int inv(int k) {
  if (k == 1) {
    return 1;
  } else {
    return ex(k, N - 2);
  }
}
/*****************default********************/
int solve() {
  int n;
  cin >> n;
  int ans = ex(3, n);
  vi inp(n + 1, 0);
  int sum = 0;
  REP(i, 1, n) {
    cin >> inp[i];
    sum += inp[i];
  }
  std::vector<std::vector<int>> dp(n + 1, std::vector<int>(sum + 1, 0));
  dp[0][0] = 1;
  REP(i, 1, n) {
    REP(red, 0, sum) {
      int prev = red - inp[i];
      dp[i][red] = ((dp[i - 1][red] * 2) + ((prev >= 0) ? dp[i - 1][prev] : 0));
      dp[i][red] %= N;
    }
  }
  if (!(sum % 2)) {
    int st = sum / 2;
    REP(i, st, sum) {
      ans -= (dp[n][i] * 3);
      ans %= N;
    }
    std::vector<std::vector<int>> dp2(n + 1, std::vector<int>(sum, 0));
    dp2[0][0] = 1;
    REP(i, 1, n) {
      REP(red, 0, sum) {
        int prev = red - inp[i];
        dp2[i][red] = (dp2[i - 1][red] + ((prev >= 0) ? dp2[i - 1][prev] : 0));
        dp2[i][red] %= N;
      }
    }
    ans += (3 * dp2[n][sum / 2]);
    ans %= N;
  } else {
    int st = ((sum / 2) + 1);
    REP(i, st, sum) {
      ans -= (dp[n][i] * 3);
      ans %= N;
    }
  }
  ans += N;
  ans %= N;
  cout << ans << "\n";
}
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  speed int tt = 1;
  // cin>>tt;
  while (tt--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define x first
#define y second
#define all(aaa) (aaa).begin(), (aaa).end()
#define REP(i, sss, eee) for (int i = sss; i <= eee; ++i)
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define N 998244353
#define inf 1000000000000000000
using namespace __gnu_pbds;
using namespace std;
template <class T> T MAX(T a, T b) { return ((a > b) ? a : b); }
template <class T> T MIN(T a, T b) { return ((a < b) ? a : b); }
template <class T> T MOD(T a) { return ((a >= 0) ? a : (-1 * a)); }
template <class T> void SWAP(T &a, T &b) {
  T temp = a;
  a = b;
  b = temp;
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set; // for multiset use less_equal above

inline int ex(int a, int b) {
  if (b == 0 || a == 1) {
    return 1;
  }
  if (a == 0 || b == 1) {
    return a;
  }
  int tmp = ex(a, (b / 2));
  if (b % 2) {
    return ((((tmp * tmp) % N) * a) % N);
  } else {
    return ((tmp * tmp) % N);
  }
}
inline int inv(int k) {
  if (k == 1) {
    return 1;
  } else {
    return ex(k, N - 2);
  }
}
/*****************default********************/
int solve() {
  int n;
  cin >> n;
  int ans = ex(3, n);
  vi inp(n + 1, 0);
  int sum = 0;
  REP(i, 1, n) {
    cin >> inp[i];
    sum += inp[i];
  }
  std::vector<std::vector<int>> dp(n + 1, std::vector<int>(sum + 1, 0));
  dp[0][0] = 1;
  REP(i, 1, n) {
    REP(red, 0, sum) {
      int prev = red - inp[i];
      dp[i][red] = ((dp[i - 1][red] * 2) + ((prev >= 0) ? dp[i - 1][prev] : 0));
      dp[i][red] %= N;
    }
  }
  if (!(sum % 2)) {
    int st = sum / 2;
    REP(i, st, sum) {
      ans -= (dp[n][i] * 3);
      ans %= N;
    }
    std::vector<std::vector<int>> dp2(n + 1, std::vector<int>(sum, 0));
    dp2[0][0] = 1;
    REP(i, 1, n) {
      REP(red, 0, sum) {
        int prev = red - inp[i];
        dp2[i][red] = (dp2[i - 1][red] + ((prev >= 0) ? dp2[i - 1][prev] : 0));
        dp2[i][red] %= N;
      }
    }
    ans += (3 * dp2[n][sum / 2]);
    ans %= N;
  } else {
    int st = ((sum / 2) + 1);
    REP(i, st, sum) {
      ans -= (dp[n][i] * 3);
      ans %= N;
    }
  }
  ans += N;
  ans %= N;
  cout << ans << "\n";
}
signed main() {
  speed int tt = 1;
  // cin>>tt;
  while (tt--) {
    solve();
  }
  return 0;
}