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

#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef pair<double, double> pdd;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
const int mod = 998244353;

ll sums[305][305];
ll sum1[305][305];

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  string s;
  int n, k;
  cin >> s >> k;
  s = '0' + s;
  n = s.size();
  vi a;
  for (int i = 0; i < n; ++i)
    if (s[i] == '0') {
      int c = 0;
      for (int j = i + 1; j < n && s[j] == '1'; ++j)
        ++c;
      a.push_back(c);
    }
  vi sa(a.size() + 1);
  for (int i = 0; i < a.size(); ++i) {
    sa[i + 1] = sa[i] + a[i];
    //    cerr << sa[i + 1] << endl;
  }
  vvl d(sa.back() + 1, vl(min(n, k) + 1));
  d[0][0] = 1;
  for (int i = 1; i < sa.size(); ++i) {
    vvl nd(d.size(), vl(d[0].size()));
    for (int j = 0; j < d[0].size(); ++j) {
      for (int t = 0; t < d.size(); ++t) {
        sums[j][t + 1] = (sums[j][t] + d[t][j]) % mod;
        if (t - sa[i - 1] <= j) {
          int x = j - t + sa[i - 1];
          sum1[x][t + 1] = (sum1[x][t] + d[t][j]) % mod;
        }
      }
    }
    for (int t = sa[i]; t <= sa.back(); ++t) {
      int ds = t - sa[i];
      for (int j = ds; j < d[t].size(); ++j) {
        nd[t][j] = (sums[j][t + 1] - sums[j][sa[i - 1] + ds] +
                    sum1[j - ds][sa[i - 1] + ds] - sum1[j - ds][sa[i - 1]]) %
                   mod;
        //        cerr << t << ' ' << j << ' ' << (mod + nd[t][j]) % mod << ' '
        //        << sums[j][t + 1] - sums[j][sa[i - 1] + ds] << endl;
      }
    }
    d.swap(nd);
  }
  ll res = 0;
  for (int i = 0; i < min(k + 1, (int)d.back().size()); ++i) {
    //    cerr << i << ' ' << d.back()[i] << endl;
    res = (res + d.back()[i]) % mod;
  }
  cout << (res + mod) % mod << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef pair<double, double> pdd;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
// const int mod = ;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  cin >> n;
  ++n;
  vl a(n), l(n), r(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    l[i] = a[i];
    if (i < 60)
      r[i] = 1LL << i;
    else
      r[i] = 1LL << 60;
  }
  l[0] = max(l[0], 1LL);
  r[n - 1] = min(r[n - 1], a[n - 1]);
  bool fail = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (l[i] > r[i])
      fail = 1;
    else if (i) {
      l[i - 1] = max(l[i - 1], a[i - 1] + (l[i] + 1) / 2);
      r[i - 1] = min(r[i - 1], a[i - 1] + r[i]);
    }
  }
  ll ma = 0;
  for (int i = 0; i < n; ++i) {
    //    cerr << l[i] << ' ' << r[i] << endl;
    if (l[i] > r[i])
      fail = 1;
    ma += r[i];
    if (i + 1 < n)
      r[i + 1] = min(r[i + 1], 2 * (r[i] - a[i]));
  }
  if (fail)
    cout << -1 << endl;
  else
    cout << ma << endl;
  return 0;
}