Problem p03070 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep3(i, a, b) for (int i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define queint queue<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pq priority_queue<int>                     // 大きい順
#define pqg priority_queue<int, vec, greater<int>> // 小さい順
#define pb push_back
#define vec vector<int>
#define vecvec vector<vector<int>>
#define vecll vector<ll>
#define vecvecll vector<vector<ll>>
#define bs binary_search
#define All(c) (c).begin(), (c).end()
#define mp make_pair
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
ll mod = 998244353;
ll modpow(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll d = modpow(a, b / 2);
    return d * d % mod;
  }
  return modpow(a, b - 1) * a % mod;
}
int main() {
  int n = in();
  ll sum = 0;
  vec a(n);
  rep(i, n) a[i + 1] = in();
  vector<vecll> dp(n + 1, vecll(90001, 0));
  dp[0][0] = 1;
  rep(i, n) {
    sum += a[i + 1];
    rep2(j, 0, sum) {
      dp[i + 1][j] = 2 * dp[i][j];
      if (j >= a[i + 1])
        dp[i + 1][j] += dp[i][j - a[i + 1]];
      dp[i + 1][j] %= mod;
    }
  }
  ll ans = modpow(3, n);
  rep2(i, (sum + 1) / 2, sum) ans = (ans - 3 * dp[n][i]) % mod;
  if (sum % 2 == 0) {
    vector<vecll> c(n + 1, vecll(90001, 0));
    c[0][0] = 1;
    rep(i, n) {
      rep2(j, 0, sum) {
        c[i + 1][j] = c[i][j];
        if (j >= a[i + 1])
          c[i + 1][j] += c[i][j - a[i + 1]];
        c[i + 1][j] %= mod;
      }
    }
    ans += 3 * c[n][sum / 2];
  }
  cout << (ans + mod) % mod << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep3(i, a, b) for (int i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define queint queue<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pq priority_queue<int>                     // 大きい順
#define pqg priority_queue<int, vec, greater<int>> // 小さい順
#define pb push_back
#define vec vector<int>
#define vecvec vector<vector<int>>
#define vecll vector<ll>
#define vecvecll vector<vector<ll>>
#define bs binary_search
#define All(c) (c).begin(), (c).end()
#define mp make_pair
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
ll mod = 998244353;
ll modpow(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll d = modpow(a, b / 2);
    return d * d % mod;
  }
  return modpow(a, b - 1) * a % mod;
}
int main() {
  int n = in();
  ll sum = 0;
  vec a(n + 1);
  rep(i, n) a[i + 1] = in();
  vector<vecll> dp(n + 1, vecll(90001, 0));
  dp[0][0] = 1;
  rep(i, n) {
    sum += a[i + 1];
    rep2(j, 0, sum) {
      dp[i + 1][j] = 2 * dp[i][j];
      if (j >= a[i + 1])
        dp[i + 1][j] += dp[i][j - a[i + 1]];
      dp[i + 1][j] %= mod;
    }
  }
  ll ans = modpow(3, n);
  rep2(i, (sum + 1) / 2, sum) ans = (ans - 3 * dp[n][i]) % mod;
  if (sum % 2 == 0) {
    vector<vecll> c(n + 1, vecll(90001, 0));
    c[0][0] = 1;
    rep(i, n) {
      rep2(j, 0, sum) {
        c[i + 1][j] = c[i][j];
        if (j >= a[i + 1])
          c[i + 1][j] += c[i][j - a[i + 1]];
        c[i + 1][j] %= mod;
      }
    }
    ans += 3 * c[n][sum / 2];
  }
  cout << (ans + mod) % mod << endl;
}