alexjercan/bugnet huggingface dataset:
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <iostream>
#include <vector>

using namespace std;

int line;
vector<string> S;
enum Operator { add, mul };

int cnt(string s) {
  int ret = 0;
  for (auto c : s) {
    if (c == '.')
      ret++;
    else
      break;
  }
  return ret;
}

int dfs(int depth) {
  string s = S[line];
  if (isdigit(s[depth])) {
    return s[depth] - '0';
  } else {
    int ret;
    if (s[depth] == '+') {
      ret = 0;
      while (line + 1 < S.size() && cnt(S[line + 1]) == depth + 1) {
        line++;
        ret += dfs(depth + 1);
      }
    } else if (s[depth] == '*') {
      ret = 1;
      while (line + 1 < S.size() && cnt(S[line + 1]) == depth + 1) {
        line++;
        ret *= dfs(depth + 1);
      }
    } else {
      cout << "error" << endl;
      exit(1);
    }
    return ret;
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    S.clear();
    line = 0;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      S.push_back(s);
    }
    cout << dfs(0) << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <vector>

using namespace std;

int line;
vector<string> S;
enum Operator { add, mul };

int cnt(string s) {
  int ret = 0;
  for (auto c : s) {
    if (c == '.')
      ret++;
    else
      break;
  }
  return ret;
}

int dfs(int depth) {
  string s = S[line];
  if (isdigit(s[depth])) {
    return s[depth] - '0';
  } else {
    int ret;
    if (s[depth] == '+') {
      ret = 0;
      while (cnt(S[line + 1]) == depth + 1) {
        line++;
        ret += dfs(depth + 1);
      }
    } else if (s[depth] == '*') {
      ret = 1;
      while (cnt(S[line + 1]) == depth + 1) {
        line++;
        ret *= dfs(depth + 1);
      }
    } else {
      cout << "error" << endl;
      exit(1);
    }
    return ret;
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    S.clear();
    line = 0;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      S.push_back(s);
    }
    cout << dfs(0) << endl;
  }

  return 0;
}
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

int n;
std::vector<std::string> ss;

inline int getDots(std::string s) { return s.rfind('.', s.length() - 1); }

std::pair<long long, int> calc(int depth, int i) {
  int num = getDots(ss[i]) + 1;
  if (depth > num) {
    return {-1, i};
  }

  char c = ss[i][num];
  if (c >= '0' && c <= '9') {
    return {(long long)(c - '0'), i + 1};
  }

  long long ans = (c == '+' ? 0 : 1);
  int j = i + 1;
  while (j < n) {
    auto res = calc(depth + 1, j);
    if (res.first == -1) {
      break;
    }
    if (c == '+') {
      ans += res.first;
    } else {
      ans *= res.first;
    }
    j = res.second;
  }
  return {ans, j};
}

int main() {
  ss.resize(100);
  while (std::cin >> n, n != 0) {
    for (int i = 0; i < n; ++i) {
      std::cin >> ss[i];
    }
    std::cout << calc(0, 0).first << std::endl;
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

int n;
std::vector<std::string> ss;

inline int getDots(std::string s) { return s.rfind('.', s.length() - 1); }

std::pair<long long, int> calc(int depth, int i) {
  int num = getDots(ss[i]) + 1;
  if (depth > num) {
    return {-1, i};
  }

  char c = ss[i][num];
  if (c >= '0' && c <= '9') {
    return {(long long)(c - '0'), i + 1};
  }

  long long ans = (c == '+' ? 0 : 1);
  int j = i + 1;
  while (true) {
    auto res = calc(depth + 1, j);
    if (res.first == -1) {
      break;
    }
    if (c == '+') {
      ans += res.first;
    } else {
      ans *= res.first;
    }
    j = res.second;
  }
  return {ans, j};
}

int main() {
  while (std::cin >> n, n != 0) {
    if (ss.size() < n) {
      ss.resize(n);
    }
    for (int i = 0; i < n; ++i) {
      std::cin >> ss[i];
    }
    std::cout << calc(0, 0).first << std::endl;
  }
}
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>

#define pb push_back
#define ll long long
using namespace std;
vector<string> a;
int n, l;

int calc(int v) {
  char c = a[l][v];
  char cc;
  int r;
  l++;
  v++;
  // cout<<":"<<c<<endl;
  if (l < n - 1 && (a[l][v] == '+' || a[l][v] == '*'))
    r = calc(v);
  else {
    if (c == '*')
      r = 1;
    else if (c == '+')
      r = 0;
    else
      r = c - '0';
  }
  if (l >= n)
    return r;
  if (c == '+') {
    while (a[l].size() == v + 1) {
      cc = a[l][v];
      // cout<<cc<<endl;
      if (l + 1 < n && (cc == '+' || cc == '*'))
        r += calc(v);
      else {
        r += cc - '0';
        l++;
      }
      if (l == n)
        break;
    }
  } else if (c == '*') {
    while (a[l].size() == v + 1) {
      cc = a[l][v];
      if (l + 1 < n && (cc == '+' || cc == '*'))
        r *= calc(v);
      else {
        r *= cc - '0';
        l++;
      }
      if (l == n)
        break;
    }
  }
  // cout<<r<<endl;
  return r;
}

int main() {
  int i;
  while (cin >> n, n) {
    string s;
    a.clear();
    for (i = 0; i < n; i++) {
      cin >> s;
      a.pb(s);
    }
    l = 0;
    cout << calc(0) << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

#define pb push_back
#define ll long long
using namespace std;
vector<string> a;
int n, l;

int calc(int v) {
  char c = a[l][v];
  char cc;
  int r;
  l++;
  v++;
  // cout<<":"<<c<<endl;
  if (l < n && (a[l][v] == '+' || a[l][v] == '*'))
    r = calc(v);
  else {
    if (c == '*')
      r = 1;
    else if (c == '+')
      r = 0;
    else
      r = c - '0';
  }
  if (l >= n)
    return r;
  if (c == '+') {
    while (a[l].size() == v + 1) {
      cc = a[l][v];
      // cout<<cc<<endl;
      if (l + 1 < n && (cc == '+' || cc == '*'))
        r += calc(v);
      else {
        r += cc - '0';
        l++;
      }
    }
  } else if (c == '*') {
    while (a[l].size() == v + 1) {
      cc = a[l][v];
      if (l + 1 < n && (cc == '+' || cc == '*'))
        r *= calc(v);
      else {
        r *= cc - '0';
        l++;
      }
    }
  }
  // cout<<r<<endl;
  return r;
}

int main() {
  int i;
  while (cin >> n, n) {
    string s;
    a.clear();
    for (i = 0; i < n; i++) {
      cin >> s;
      a.pb(s);
    }
    l = 0;
    cout << calc(0) << endl;
  }
}
Problem p01088 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int M = 1000000007;

int main() {
  while (1) {
    int n;
    cin >> n;
    if (!n)
      return 0;

    int lim = n * 499 + 1;
    vector<P> dp(lim, P(-M, -M));
    dp[0] = P(0, 0);
    for (int i = 0; i < n; ++i) {
      int p;
      cin >> p;
      vector<P> nex = dp;
      int q = (500 - p % 500) % 500;
      if (p % 1000 > 0 && p % 1000 <= 500)
        for (int j = q; j < lim; ++j)
          nex[j] = max(nex[j], P(dp[j - q].first + 1, dp[j - q].second - p));

      else {
        int r = p % 500 == 0 ? 500 : p % 500;
        for (int j = q; j < lim; ++j)
          nex[j] = max(nex[j], P(dp[j - q].first, dp[j - q].second - p));
        for (int j = 0; j + r < lim; ++j)
          nex[j] = max(nex[j], P(dp[j + r].first + 1, dp[j + r].second - p));
      }
      dp = nex;
    }
    int ma = 0, mi = 0;
    for (auto &a : dp) {
      if (a.first > ma) {
        ma = a.first;
        mi = -a.second;
      } else if (a.first == ma && -a.second < mi)
        mi = -a.second;
    }
    cout << ma << " " << mi << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int M = 1000000007;

int main() {
  while (1) {
    int n;
    cin >> n;
    if (!n)
      return 0;

    vector<vector<int>> dp(n * 500, vector<int>(n + 1, M));
    dp[0][0] = 0;
    for (int i = 0; i < n; ++i) {
      int p;
      cin >> p;
      vector<vector<int>> nex = dp;
      if (p % 1000 > 0 && p % 1000 <= 500) {
        int q = (500 - p % 500) % 500;
        for (int j = q; j < (i + 1) * 500; ++j) {
          for (int k = 1; k <= i + 1; ++k) {
            nex[j][k] = min(nex[j][k], dp[j - q][k - 1] + p);
          }
        }
      } else {
        int q = p % 500;
        int r = 1000 - p % 1000;
        if (q == 0) {
          q = 500;
          r = 0;
        }
        for (int j = 0; j + q < (i + 1) * 500; ++j) {
          for (int k = 1; k <= i + 1; ++k) {
            nex[j][k] = min(nex[j][k], dp[j + q][k - 1] + p);
          }
        }
        for (int j = r; j + q < (i + 1) * 500; ++j) {
          for (int k = 0; k <= i + 1; ++k) {
            nex[j][k] = min(nex[j][k], dp[j - r][k] + p);
          }
        }
      }
      dp = nex;
    }
    for (int i = n; i >= 0; --i) {
      int mi = M;
      for (int j = 0; j < n * 500; ++j) {
        mi = min(mi, dp[j][i]);
      }
      if (mi < M) {
        cout << i << " " << mi << "\\n";
        break;
      }
    }
  }
}
Problem p01088 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < n; ++i)
#define rep(i, n) reps(i, 0, n)
#define maxl(a, b) a = max(a, b)

pair<int, int> dp[101][210 * 500];
const int MAX = 1001001001;

void solve(int n) {
  vector<int> p(n);

  rep(i, n) { cin >> p[i]; }

  rep(i, n + 1) {
    rep(c, 210 * 500) { dp[i][c] = {-MAX, 0}; }
  }
  dp[0][0] = {0, 0};

  rep(i, n) {
    rep(c, (n + 10) * 500) {
      auto r = dp[i][c];
      maxl(dp[i + 1][c], r);

      int pr = p[i] % 1000;
      if (pr == 0) {
        if (c >= 500)
          maxl(dp[i + 1][c - 500], make_pair(r.first + 1, r.second - p[i]));
      } else if (pr <= 500) {
        maxl(dp[i + 1][c + 500 - pr], make_pair(r.first + 1, r.second - p[i]));
      } else {
        maxl(dp[i + 1][c - pr + 1000], make_pair(r.first, r.second - p[i]));
        if (c >= pr - 500)
          maxl(dp[i + 1][c - pr + 500],
               make_pair(r.first + 1, r.second - p[i]));
      }
    }
  }

  auto ans = *max_element(dp[n], dp[n] + 210 * 500);
  cout << ans.first << ' ' << -ans.second << endl;
}

int main() {
  int n;

  while (cin >> n, n) {
    solve(n);
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < n; ++i)
#define rep(i, n) reps(i, 0, n)
#define maxl(a, b) a = max(a, b)

pair<int, int> dp[101][210 * 500];
const int MAX = 1001001001;

void solve(int n) {
  vector<int> p(n);

  rep(i, n) { cin >> p[i]; }

  rep(i, n + 1) {
    rep(c, 20 * 500) { dp[i][c] = {-MAX, 0}; }
  }
  dp[0][0] = {0, 0};

  rep(i, n) {
    rep(c, 210 * 500) {
      auto r = dp[i][c];
      maxl(dp[i + 1][c], r);

      int pr = p[i] % 1000;
      if (pr == 0) {
        if (c >= 500)
          maxl(dp[i + 1][c - 500], make_pair(r.first + 1, r.second - p[i]));
      } else if (pr <= 500) {
        maxl(dp[i + 1][c + 500 - pr], make_pair(r.first + 1, r.second - p[i]));
      } else {
        maxl(dp[i + 1][c - pr + 1000], make_pair(r.first, r.second - p[i]));
        if (c >= pr - 500)
          maxl(dp[i + 1][c - pr + 500],
               make_pair(r.first + 1, r.second - p[i]));
      }
    }
  }

  auto ans = *max_element(dp[n], dp[n] + 210 * 500);
  cout << ans.first << ' ' << -ans.second << endl;
}

int main() {
  int n;

  while (cin >> n, n) {
    solve(n);
  }
}
Problem p01088 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

#define IINF INT_MAX
int n, p[110], dp[110][100010];

int main() {
  while (cin >> n, n) {
    rep(i, n) cin >> p[i];
    int limit = (n * 999) + 1;
    rep(i, n + 1) rep(j, limit) dp[i][j] = IINF;
    dp[0][0] = 0;
    int maxi = -IINF, mini = IINF;
    rep(i, n) {
      for (int j = n; j >= 0; --j) {
        for (int k = min((i + 1) * 999 + 1, limit) - 1; k >= 0; --k) {
          if (dp[j][k] == IINF)
            continue;
          int rem = (1000 - p[i] % 1000) + k;
          if (p[i] % 1000 == 0)
            rem = k;

          if (rem >= 500) {
            assert(rem - 500 >= 0 && rem - 500 < limit);
            dp[j + 1][rem - 500] = min(dp[j + 1][rem - 500], dp[j][k] + p[i]);
            if (maxi < j + 1)
              maxi = j + 1, mini = dp[j + 1][rem - 500];
            else if (maxi == j + 1)
              mini = min(mini, dp[j + 1][rem - 500]);
          } else {
            assert(0 <= rem && rem < limit);
            dp[j][rem] = min(dp[j][rem], dp[j][k] + p[i]);
          }
          dp[j][k] = min(dp[j][k], dp[j][k]);
        }
      }
    }
    if (maxi == -IINF)
      maxi = mini = 0;
    cout << maxi << " " << mini << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef pair<int, int> ii;

#define IINF INT_MAX
#define MAX 110
int n, p[MAX], dp[2][210][100010];

int main() {
  while (cin >> n, n) {
    rep(i, n) cin >> p[i];
    int limit = (n * 999) + 1;
    rep(k, 2) rep(i, n + 1) rep(j, limit) dp[k][i][j] = IINF;
    dp[0][0][0] = 0;
    rep(i, n) {
      rep(j, n + 1) rep(k, limit) dp[(i + 1) & 1][j][k] = IINF;
      rep(j, n + 1) {
        rep(k, limit) {
          if (dp[i & 1][j][k] == IINF)
            continue;
          // use
          // cout << "dp[" << i << "][" << j << "][" << k << "] = " <<
          // dp[i][j][k] << endl;
          int rem = (1000 - p[i] % 1000) + k;
          if (p[i] % 1000 == 0)
            rem = k;

          if (rem >= 500) {
            assert(rem - 500 >= 0 && rem - 500 < limit);
            dp[(i + 1) & 1][j + 1][rem - 500] =
                min(dp[(i + 1) & 1][j + 1][rem - 500], dp[i & 1][j][k] + p[i]);
          } else {
            assert(0 <= rem && rem < limit);
            dp[(i + 1) & 1][j][rem] =
                min(dp[(i + 1) & 1][j][rem], dp[i & 1][j][k] + p[i]);
          }
          // dont use
          dp[(i + 1) & 1][j][k] = min(dp[(i + 1) & 1][j][k], dp[i & 1][j][k]);
        }
      }
    }
    for (int i = n; i >= 0; --i) {
      int mini = IINF;
      rep(j, limit) {
        if (dp[n & 1][i][j] != IINF) {
          mini = min(mini, dp[n & 1][i][j]);
        }
      }
      if (mini != IINF) {
        cout << i << " " << mini << endl;
        goto Skip;
      }
    }
    puts("0 0");
  Skip:;
  }
  return 0;
}
Problem p01088 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define M 25000

int dp[2][101][M];

int main() {
  int n;
  while (cin >> n, n) {
    int p[100], pp[100];
    for (int i = 0; i < n; i++)
      scanf("%d", &p[i]), pp[i] = (1000 - p[i] % 1000) % 1000;

    for (int i = 0; i < 2; i++)
      for (int j = 0; j <= n; j++)
        for (int k = 0; k < M; k++)
          dp[i][j][k] = 1e9;

    dp[0][0][0] = 0;

    for (int i = 0; i < n; i++) {
      int ni = (i + 1) % 2, I = i % 2, mm = min(M, 500 * i + 1);

      for (int j = 0; j <= i; j++)
        for (int k = 0; k < mm; k++)
          dp[ni][j][k] = 1e9;

      for (int j = 0; j <= i; j++)
        for (int k = 0; k < mm; k++) {
          int np = k + pp[i];
          int nj = j;
          if (np >= 500)
            nj++, np -= 500;
          np = min(M - 1, np);
          dp[ni][nj][np] = min(dp[ni][nj][np], dp[I][j][k] + p[i]);
          dp[ni][j][k] = min(dp[ni][j][k], dp[I][j][k]);
        }
    }

    int c = 0, s = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 0; j < M; j++)
        if (dp[n % 2][i][j] != 1e9 && i >= c) {
          if (c == i && dp[n % 2][i][j] < s)
            s = dp[n % 2][i][j];
          else if (c < i)
            c = i, s = dp[n % 2][i][j];
        }
    printf("%d %d\\n", c, s);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define M 25000

int dp[2][101][M];

int main() {
  int n;
  while (cin >> n, n) {
    int p[100], pp[100];
    for (int i = 0; i < n; i++)
      scanf("%d", &p[i]), pp[i] = (1000 - p[i] % 1000) % 1000;

    for (int i = 0; i < 2; i++)
      for (int j = 0; j <= n; j++)
        for (int k = 0; k < M; k++)
          dp[i][j][k] = 1e9;

    dp[0][0][0] = 0;

    for (int i = 0; i < n; i++) {
      int ni = (i + 1) % 2, I = i % 2, mm = min(M, 500 * i + 1);

      for (int j = 0; j <= i; j++)
        for (int k = 0; k < mm; k++)
          dp[ni][j][k] = 1e9;

      for (int j = 0; j <= i; j++)
        for (int k = 0; k < mm; k++) {
          int np = k + pp[i];
          int nj = j;
          if (np >= 500)
            nj++, np -= 500;
          np = min(M - 1, np);
          dp[ni][nj][np] = min(dp[ni][nj][np], dp[I][j][k] + p[i]);
          dp[ni][j][k] = min(dp[ni][j][k], dp[I][j][k]);
          dp[ni][nj][np] = min(dp[ni][nj][np], dp[I][j][k] + p[i]);
        }
    }

    int c = 0, s = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 0; j < M; j++)
        if (dp[n % 2][i][j] != 1e9 && i >= c) {
          if (c == i && dp[n % 2][i][j] < s)
            s = dp[n % 2][i][j];
          else if (c < i)
            c = i, s = dp[n % 2][i][j];
        }
    printf("%d %d\\n", c, s);
  }
  return 0;
}
Problem p01088 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

pii max(pii a, pii b) {
  if (a.first > b.first) {
    return a;
  } else if (a.first < b.first) {
    return b;
  } else {
    if (a.second > b.second) {
      return b;
    } else {
      return a;
    }
  }
}
pii dp[111][50000];

int main() {
  int n;
  while (std::cin >> n, n) {
    vector<int> shop(n);
    for (int i = 0; i < n; i++)
      std::cin >> shop[i];

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < 50000; j++) {
        dp[i][j] = pii(-1e9, 0);
      }
    }

    dp[0][0] = pii(0, 0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 50000; j++) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);

        int tf = dp[i][j].first;
        int tc = dp[i][j].second;
        int five_hundred;
        int diff = (5000 - shop[i]) % 1000;
        if (j + diff >= 500)
          dp[i + 1][j + diff - 500] =
              max(dp[i + 1][j + diff - 500], pii(tf + 1, tc + shop[i]));
        else
          dp[i + 1][j + diff] = max(dp[i + 1][j + diff], pii(tf, tc + shop[i]));
      }
    }
    pii ans = pii(0, 0);
    for (int i = 0; i < 50000; i++) {
      ans = max(ans, dp[n][i]);
    }
    std::cout << ans.first << " " << ans.second << std::endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

pii max(pii a, pii b) {
  if (a.first > b.first) {
    return a;
  } else if (a.first < b.first) {
    return b;
  } else {
    if (a.second > b.second) {
      return b;
    } else {
      return a;
    }
  }
}

int main() {
  int n;
  while (std::cin >> n, n) {
    pii dp[n + 1][50000];
    vector<int> shop(n);
    for (int i = 0; i < n; i++)
      std::cin >> shop[i];
    for (int i = 0; i <= n; i++) {

      for (int j = 0; j < 50000; j++) {
        dp[i][j] = pii(-1e9, 0);
      }
    }
    dp[0][0] = pii(0, 0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 50000; j++) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);

        int tf = dp[i][j].first;
        int tc = dp[i][j].second;
        int five_hundred;
        int diff = (5000 - shop[i]) % 1000;
        if (j + diff >= 500)
          dp[i + 1][j + diff - 500] =
              max(dp[i + 1][j + diff - 500], pii(tf + 1, tc + shop[i]));
        else
          dp[i + 1][j + diff] = max(dp[i + 1][j + diff], pii(tf, tc + shop[i]));
      }
    }
    pii ans = pii(0, 0);
    for (int i = 0; i < 50000; i++) {
      ans = max(ans, dp[n][i]);
    }
    std::cout << ans.first << " " << ans.second << std::endl;
  }
  return 0;
}
Problem p01088 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
P dp[105][50005];
int p[100];
void Max(P &a, P b) { a = max(a, b); }

int main() {
  int n;
  while (cin >> n && n) {
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; i++)
      cin >> p[i];
    dp[0][0] = P(0, 0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 50000; j++) {
        if (dp[i][j] == P(-1, -1))
          continue;
        int nf = dp[i][j].first, ns = dp[i][j].second - p[i];
        Max(dp[i + 1][j], dp[i][j]);
        if (p[i] % 1000 == 0) {
          if (j >= 500)
            Max(dp[i + 1][j - 500], P(nf + 1, ns));
        } else if (p[i] % 1000 < 500)
          Max(dp[i + 1][j + 500 - p[i] % 500], P(nf + 1, ns));
        else if (p[i] % 500 <= j)
          Max(dp[i + 1][j - p[i] % 500], P(nf + 1, ns));
        else
          Max(dp[i + 1][j + 500 - p[i] % 500], P(nf, ns));
      }
    }
    P ans(0, 0);
    for (int i = 0; i < 50000; i++)
      Max(ans, dp[n][i]);
    cout << ans.first << " " << -ans.second << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
P dp[100][50005];
int p[100];
int main() {
  int n;
  while (cin >> n && n) {
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; i++)
      cin >> p[i];
    dp[0][0] = P(0, 0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 50000; j++) {
        if (dp[i][j] == P(-1, -1))
          continue;
        int nf = dp[i][j].first, ns = dp[i][j].second;
        if (p[i] % 1000 == 0) {
          if (j >= 500)
            dp[i + 1][j - 500] = P(nf + 1, ns - p[i]);
          else
            dp[i + 1][j] = P(nf, ns);
        } else if (p[i] % 1000 < 500)
          dp[i + 1][j + 500 - p[i] % 500] = P(nf + 1, ns - p[i]);
        else if (p[i] % 500 <= j)
          dp[i + 1][j - p[i] % 500] = P(nf + 1, ns - p[i]);
        else {
          dp[i + 1][j + 1000 - p[i] % 1000] = P(nf, ns - p[i]);
          dp[i + 1][j] = P(nf, ns);
        }
      }
    }
    P ans(0, 0);
    for (int i = 0; i < 50000; i++)
      ans = max(ans, dp[n][i]);
    cout << ans.first << " " << -ans.second << endl;
  }
  return 0;
}