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

#include <bits/stdc++.h>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \\
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << "\\n";
}
typedef long long ll;

int dp[100][2010];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> b;
  for (int i = 0; i < n; i++) {
    b.push_back(a[i]);
    for (int j = 0; j < n; j++) {
      if (a[i + j + 1] != a[i]) {
        i += j;
        break;
      }
    }
  }

  vector<int> c;
  c.push_back(INF);

  for (int i = 0; i < b.size(); i++) {
    c.push_back(b[i]);
  }
  if (b.back() > b[b.size() - 2])
    c.push_back(0);
  else
    c.push_back(INF);

  if (b.size() == 1) {
    cout << 1000 << endl;
    return 0;
  }
  int pos = 1000;
  int kab = 0;
  int ans = 0;
  // show(c);
  for (int i = 0; i < c.size() - 1; i++) {
    if (c[i - 1] < c[i] && c[i] > c[i + 1]) {
      pos += c[i] * kab;
      kab = 0;
    } else if (c[i - 1] > c[i] && c[i] < c[i + 1]) {
      kab += pos / c[i];
      pos %= c[i];
    }
    // cerr<<pos<<endl;
    chmax(ans, pos);
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \\
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << "\\n";
}
typedef long long ll;

int dp[100][200 * 80 + 10];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < 200 * 80 + 10; j++) {
      dp[i][j] = -1;
    }
  }
  dp[0][1000] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 200 * 80 + 10; j++) {
      if (dp[i][j] == -1)
        continue;
      // kauif
      int k = 0;
      while (1) {
        if (j - a[i] * (k) < 0) {
          break;
        }
        if (dp[i + 1][j - a[i] * (k)] < k + dp[i][j]) {
          dp[i + 1][j - a[i] * (k)] = k + dp[i][j];
        }
        k++;
      }
      // uru
      for (int l = 0; l <= dp[i][j]; l++) {
        if (dp[i + 1][j + a[i] * (l)] < dp[i][j] - l) {
          dp[i + 1][j + a[i] * (l)] = dp[i][j] - l;
        }
      }
    }
  }
  // cerr<<dp[6][1185]<<endl;
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < 200 * 80 + 10; j++) {
      /* if(i==1 && j<=1000){
           cerr<<dp[i][j]<<" ";
       }*/
      if (dp[i][j] >= 0) {
        chmax(ans, j);
      }
    }
  }
  cout << ans << endl;

  return 0;
}