Problem p03070 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int N = 301;
const int K = 101;
const int MOD = 998244353;

int add(int a, int b) { return (a + b) % MOD; }

int sub(int a, int b) { return (a - b + MOD) % MOD; }

int mul(int a, int b) { return (a * b) % MOD; }

int binpow(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = mul(res, a);
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

int divv(int a, int b) { return mul(a, binpow(b, MOD - 2)); }

int dp[N][K];
int dp2[N][K];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &t : a) {
    cin >> t;
  }
  dp[0][0] = 1;
  dp2[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < K; j++) {
      if (a[i] + j < K) {
        dp[i + 1][j + a[i]] = add(dp[i + 1][j + a[i]], dp[i][j]);
        dp2[i + 1][j + a[i]] = add(dp2[i + 1][j + a[i]], dp2[i][j]);
      }
      dp[i + 1][j] = add(dp[i + 1][j], mul(2, dp[i][j]));
      dp2[i + 1][j] = add(dp2[i + 1][j], mul(1, dp2[i][j]));
    }
  }
  int s = 0;
  for (auto t : a) {
    s += t;
  }
  int ans = 0;
  for (int tk = 0; tk < s; tk++) {
    if (tk >= s - tk) {
      int cnt = dp[n][tk];
      cnt = sub(cnt, mul(dp2[n][tk], 2));
      ans = add(ans, cnt);
    }
  }
  ans = mul(ans, 3);
  int all = binpow(3, n);
  all = sub(all, mul(3, sub(binpow(2, n), 2)));
  all = sub(all, 3);
  cout << sub(all, ans) << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int N = 301;
const int K = N * N;
const int MOD = 998244353;

int add(int a, int b) { return (a + b) % MOD; }

int sub(int a, int b) { return (a - b + MOD) % MOD; }

int mul(int a, int b) { return (a * b) % MOD; }

int binpow(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = mul(res, a);
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

int divv(int a, int b) { return mul(a, binpow(b, MOD - 2)); }

int dp[N][K];
int dp2[N][K];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &t : a) {
    cin >> t;
  }
  dp[0][0] = 1;
  dp2[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < K; j++) {
      if (a[i] + j < K) {
        dp[i + 1][j + a[i]] = add(dp[i + 1][j + a[i]], dp[i][j]);
        dp2[i + 1][j + a[i]] = add(dp2[i + 1][j + a[i]], dp2[i][j]);
      }
      dp[i + 1][j] = add(dp[i + 1][j], mul(2, dp[i][j]));
      dp2[i + 1][j] = add(dp2[i + 1][j], mul(1, dp2[i][j]));
    }
  }
  int s = 0;
  for (auto t : a) {
    s += t;
  }
  int ans = 0;
  for (int tk = 0; tk < s; tk++) {
    if (tk >= s - tk) {
      int cnt = dp[n][tk];
      cnt = sub(cnt, mul(dp2[n][tk], 2));
      ans = add(ans, cnt);
    }
  }
  ans = mul(ans, 3);
  int all = binpow(3, n);
  all = sub(all, mul(3, sub(binpow(2, n), 2)));
  all = sub(all, 3);
  cout << sub(all, ans) << endl;
}