prithivMLmods/Coder-Stat huggingface dataset:
Problem p03599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = 1000000000;
const double PI = acos(-1);

int a, b, c, d, e, f;

bool calc(int i, int j) {
  bool isok = false;
  for (int l = 0; l * a <= j; l++) {
    if ((j - l * a) % b == 0) {
      isok = true;
      break;
    }
  }
  if (!isok) {
    return false;
  }
  isok = false;
  for (int l = 0; l * c <= i; l++) {
    if ((i - l * c) % d == 0) {
      isok = true;
      break;
    }
  }
  return isok;
}

int main() {
  double cc = -1;
  int cci = -1, ccj = -1;
  for (int j = a; j <= f / 100; j++) {
    for (int i = 0; i <= e * j; i++)
      if (calc(i, j) && i + j * 100 <= f) {
        if (cc < 1.0 * i / j)
          cc = 1.0 * i / j, cci = i, ccj = j;
      }
  }
  cout << (cci + ccj * 100) << " " << cci << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = 1000000000;
const double PI = acos(-1);

int a, b, c, d, e, f;

bool calc(int i, int j) {
  bool isok = false;
  for (int l = 0; l * a <= j; l++) {
    if ((j - l * a) % b == 0) {
      isok = true;
      break;
    }
  }
  if (!isok) {
    return false;
  }
  isok = false;
  for (int l = 0; l * c <= i; l++) {
    if ((i - l * c) % d == 0) {
      isok = true;
      break;
    }
  }
  return isok;
}

int main() {
  cin >> a >> b >> c >> d >> e >> f;
  double cc = -1;
  int cci = -1, ccj = -1;
  for (int j = a; j <= f / 100; j++) {
    for (int i = 0; i <= e * j; i++)
      if (calc(i, j) && i + j * 100 <= f) {
        if (cc < 1.0 * i / j)
          cc = 1.0 * i / j, cci = i, ccj = j;
      }
  }
  cout << (cci + ccj * 100) << " " << cci << endl;
}
Problem p03599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAX 1000000007
typedef long long ll;

int W[40], S[31000];

int main() {
  int A, B, C, D, E, F;
  cin >> A >> B >> C >> D >> E >> F;

  for (int i = 0; i <= F / 100 / A; i++) {
    for (int j = 0; j <= F / 100 - A * i; j++) {
      W[A * i + B * j]++;
    }
  }
  S[0] = 1;
  for (int i = 0; i <= ((F / 100) * E) / C; i++) {
    for (int j = 0; j <= ((F / 100) * E - C * i) / D; j++) {
      S[i * C + j * D]++;
    }
  }
  int ans1, ans2;
  ll temp, ans = 0;
  for (ll i = 1; i <= F / 100; i++) {
    for (ll j = 0; j <= (F / 100) * E; j++) {
      if (W[i] == 0 || S[j] == 0)
        continue;
      if (i * 100 + j > F)
        break;
      if (i * E < j)
        break;
      temp = (j * 10000000) / (i * 100 + j);
      if (temp >= ans) {
        ans = temp;
        ans1 = j;
        ans2 = i * 100 + j;
      }
    }
  }
  cout << ans2 << " " << ans1 << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAX 1000000007
typedef long long ll;

int W[40], S[31000];

int main() {
  int A, B, C, D, E, F;
  cin >> A >> B >> C >> D >> E >> F;

  for (int i = 0; i <= F / 100 / A; i++) {
    for (int j = 0; j <= (F / 100 - A * i) / B; j++) {
      W[A * i + B * j]++;
    }
  }
  S[0] = 1;
  for (int i = 0; i <= ((F / 100) * E) / C; i++) {
    for (int j = 0; j <= ((F / 100) * E - C * i) / D; j++) {
      S[i * C + j * D]++;
    }
  }
  int ans1, ans2;
  ll temp, ans = 0;
  for (ll i = 1; i <= F / 100; i++) {
    for (ll j = 0; j <= (F / 100) * E; j++) {
      if (W[i] == 0 || S[j] == 0)
        continue;
      if (i * 100 + j > F)
        break;
      if (i * E < j)
        break;
      temp = (j * 10000000) / (i * 100 + j);
      if (temp >= ans) {
        ans = temp;
        ans1 = j;
        ans2 = i * 100 + j;
      }
    }
  }
  cout << ans2 << " " << ans1 << endl;
}
Problem p03599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

int main() {
  int A, B, C, D, E, F;
  cin >> A >> B >> C >> D >> E >> F;

  double con = 0.0;
  int sumw = 100 * A, sums = 0;
  for (int a = 0; a <= 30; a++) {
    for (int b = 0; b <= 30; b++) {
      for (int c = 0; c <= 3000; c++) {
        for (int d = 0; d <= 3000; d++) {
          int tmpw = 100 * A * a + 100 * B * b;
          int tmps = C * c + D * d;
          if (tmpw + tmps <= F && tmps * 100 <= tmpw * E &&
              con < (double)tmps / (tmpw + tmps)) {
            con = (double)tmps / (tmpw + tmps);
            sumw = tmpw;
            sums = tmps;
          }
        }
      }
    }
  }

  cout << sumw + sums << " " << sums << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

int main() {
  int A, B, C, D, E, F;
  cin >> A >> B >> C >> D >> E >> F;

  double con = 0.0;
  int sumw = 100 * A, sums = 0;
  for (int a = 0; a <= 30; a++) {
    for (int b = 0; b <= 30; b++) {
      for (int c = 0; c <= ceil((double)F / C); c++) {
        for (int d = 0; d <= ceil((double)F / D); d++) {
          int tmpw = 100 * A * a + 100 * B * b;
          int tmps = C * c + D * d;
          if (tmpw + tmps <= F && tmps * 100 <= tmpw * E &&
              con < (double)tmps / (tmpw + tmps)) {
            con = (double)tmps / (tmpw + tmps);
            sumw = tmpw;
            sums = tmps;
          }
        }
      }
    }
  }

  cout << sumw + sums << " " << sums << endl;
}
Problem p03600 (C++)

Buggy submission (status: Runtime Error):

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;

  ll a[N][N];
  REP(i, N) REP(j, N) cin >> a[i][j];

  ll r[N][N];
  REP(i, N) REP(j, N) r[i][j] = a[i][j];

  bool no = false;
  REP(k, N) {
    REP(i, N) {
      REP(j, N) {
        if (a[i][j] == 0 || a[i][k] == 0 || a[k][j] == 0)
          continue;
        if (a[i][j] > a[i][k] + a[k][j]) {
          no = true;
          break;
        } else if (a[i][j] == a[i][k] + a[k][j]) {
          r[i][j] = 0;
        }
      }
      if (no)
        break;
    }
    if (no)
      break;
  }

  if (no) {
    cout << -1 << endl;
    return 1;
  } else {
    ll ans = 0;
    REP(i, N) REP(j, N) { ans += r[i][j]; }

    cout << ans / 2 << endl;
  }
}


Fixed submission:

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;

  ll a[N][N];
  REP(i, N) REP(j, N) cin >> a[i][j];

  ll r[N][N];
  REP(i, N) REP(j, N) r[i][j] = a[i][j];

  bool no = false;
  REP(k, N) {
    REP(i, N) {
      REP(j, N) {
        if (a[i][j] == 0 || a[i][k] == 0 || a[k][j] == 0)
          continue;
        if (a[i][j] > a[i][k] + a[k][j]) {
          no = true;
          break;
        } else if (a[i][j] == a[i][k] + a[k][j]) {
          r[i][j] = 0;
        }
      }
      if (no)
        break;
    }
    if (no)
      break;
  }

  if (no) {
    cout << -1 << endl;
    return 0;
  } else {
    ll ans = 0;
    REP(i, N) REP(j, N) { ans += r[i][j]; }

    cout << ans / 2 << endl;
  }
}
Problem p03600 (C++)

Buggy submission (status: Runtime Error):
// Rafael Saddatimov
#include <bits/stdc++.h>
#define MAX 205
using namespace std;

int a[MAX][MAX], no[MAX][MAX];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      scanf("%d", &a[i][j]);
    }
  }
  int ok = 1;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      for (int k = 1; k <= n; ++k) {
        if (a[i][k] + a[k][j] < a[i][j]) {
          ok = 0;
          break;
        }
        if (k != i && k != j && a[i][k] + a[k][j] == a[i][j]) {
          no[i][j] = 1;
        }
      }
    }
  }
  if (ok == 0) {
    return 0 * puts("-1");
  }
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      res += !no[i][j] * a[i][j];
    }
  }
  printf("%lld\n", res);
}


Fixed submission:
// Rafael Saddatimov
#include <bits/stdc++.h>
#define MAX 305
using namespace std;

int a[MAX][MAX], no[MAX][MAX];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      scanf("%d", &a[i][j]);
    }
  }
  int ok = 1;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      for (int k = 1; k <= n; ++k) {
        if (a[i][k] + a[k][j] < a[i][j]) {
          ok = 0;
          break;
        }
        if (k != i && k != j && a[i][k] + a[k][j] == a[i][j]) {
          no[i][j] = 1;
        }
      }
    }
  }
  if (ok == 0) {
    return 0 * puts("-1");
  }
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      res += !no[i][j] * a[i][j];
    }
  }
  printf("%lld\n", res);
}
Problem p03600 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using lli = long long int;

int main() {
  int n;
  std::cin >> n;
  vector<vector<lli>> adj(n, vector<lli>(n, 0));
  vector<vector<int>> mini;
  for (int i = 0; i < n; i++) {
    vector<int> tmp(3);
    for (int j = 0; j < n; j++) {
      std::cin >> adj[i][j];
      tmp[0] = adj[i][j], tmp[1] = i, tmp[2] = j;
      if (i < j)
        mini.push_back(tmp);
    }
  }
  sort(mini.begin(), mini.end());
  vector<vector<lli>> dist = adj;
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
  }

  if (dist != adj) {
    std::cout << -1 << std::endl;
    return 0;
  }
  dist = vector<vector<lli>>(n, vector<lli>(n, 2e9));
  for (int i = 0; i < n; i++)
    dist[i][i] = 0;
  lli ans = 0;
  for (int i = 0; i < mini.size(); i++) {
    int u = mini[i][1], v = mini[i][2];
    bool flag = false;
    if (dist[u][v] < adj[u][v])
      continue;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        lli md = dist[j][u] + adj[u][v] + dist[v][k];
        if (md < dist[j][k]) {
          dist[j][k] = dist[k][j] = md;
          flag = true;
        }
      }
    }
    if (flag)
      ans += adj[u][v];
  }
  std::cout << ans << std::endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using lli = long long int;

int main() {
  int n;
  std::cin >> n;
  vector<vector<lli>> adj(n, vector<lli>(n, 0));
  vector<vector<int>> mini;
  for (int i = 0; i < n; i++) {
    vector<int> tmp(3);
    for (int j = 0; j < n; j++) {
      std::cin >> adj[i][j];
      tmp[0] = adj[i][j], tmp[1] = i, tmp[2] = j;
      if (i < j)
        mini.push_back(tmp);
    }
  }
  sort(mini.begin(), mini.end());
  vector<vector<lli>> dist = adj;
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
  }

  if (dist != adj) {
    std::cout << -1 << std::endl;
    return 0;
  }
  dist = vector<vector<lli>>(n, vector<lli>(n, 2e9));
  for (int i = 0; i < n; i++)
    dist[i][i] = 0;
  lli ans = 0;
  for (int i = 0; i < mini.size(); i++) {
    int u = mini[i][1], v = mini[i][2];
    bool flag = false;
    if (dist[u][v] <= adj[u][v])
      continue;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        lli md = dist[j][u] + adj[u][v] + dist[v][k];
        if (md < dist[j][k]) {
          dist[j][k] = dist[k][j] = md;
          flag = true;
        }
      }
    }
    if (flag)
      ans += adj[u][v];
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p03600 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

const long long int INF = 1e16;

int main() {
  int N;
  cin >> N;
  vector<vector<long long int>> og(N, vector<long long int>(N));
  priority_queue<pair<long long int, pair<int, int>>> que;
  for (int n = 0; n < N; n++) {
    for (int m = 0; m < N; m++) {
      cin >> og[n][m];
      if (n >= m)
        continue;
      que.push(make_pair(-og[n][m], make_pair(n, m)));
    }
  }
  vector<vector<long long int>> field(N, vector<long long int>(N, INF));
  long long int ans = 0;
  while (!que.empty()) {
    pair<long long int, pair<int, int>> t = que.top();
    que.pop();
    long long int cost = -t.first;
    int n = t.second.first, m = t.second.second;
    if (field[n][m] > cost) {
      field[n][m] = cost;
      field[m][n] = cost;
      ans += cost;

      for (int k = 0; k < N; k++) {
        for (int i = 0; i < N; i++) {
          for (int j = 0; j < N; j++) {
            field[i][j] = min(field[i][j], field[i][k] + field[k][j]);
            if (og[i][j] > field[i][j]) {
              ans = -1;
              break;
            }
          }
        }
      }
    }
    if (ans == -1) {
      break;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

const long long int INF = 1e16;

int main() {
  int N;
  cin >> N;
  vector<vector<long long int>> og(N, vector<long long int>(N));
  priority_queue<pair<long long int, pair<int, int>>> que;
  for (int n = 0; n < N; n++) {
    for (int m = 0; m < N; m++) {
      cin >> og[n][m];
      if (n >= m)
        continue;
      que.push(make_pair(-og[n][m], make_pair(n, m)));
    }
  }
  vector<vector<long long int>> field(N, vector<long long int>(N, INF));
  long long int ans = 0;
  while (!que.empty()) {
    pair<long long int, pair<int, int>> t = que.top();
    que.pop();
    long long int cost = -t.first;
    int n = t.second.first, m = t.second.second;
    if (field[n][m] > cost) {
      field[n][m] = cost;
      field[m][n] = cost;
      ans += cost;

      int k = n;
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          field[i][j] = min(field[i][j], field[i][k] + field[k][j]);
          if (og[i][j] > field[i][j]) {
            ans = -1;
            break;
          }
        }
      }
      k = m;
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          field[i][j] = min(field[i][j], field[i][k] + field[k][j]);
          if (og[i][j] > field[i][j]) {
            ans = -1;
            break;
          }
        }
      }
    }
    if (ans == -1) {
      break;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03600 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
const int maxn = 110, inf = 1e9;
ll n, m, x, y, z, tot, ans;
ll f[maxn][maxn], v[maxn][maxn];
void read(ll &k) {
  int f = 1;
  k = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c == '-' && (f = -1), c = getchar();
  while (c <= '9' && c >= '0')
    k = k * 10 + c - '0', c = getchar();
  k *= f;
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      read(f[i][j]), ans += f[i][j];
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        if (i == j || i == k || j == k)
          continue;
        if (f[i][k] + f[k][j] < f[i][j])
          return puts("-1"), 0;
        if (f[i][k] + f[k][j] == f[i][j])
          v[i][j] = v[j][i] = 1;
      }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (v[i][j])
        ans -= f[i][j];
  printf("%lld\n", ans >> 1);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
const int maxn = 510, inf = 1e9;
ll n, m, x, y, z, tot, ans;
ll f[maxn][maxn], v[maxn][maxn];
void read(ll &k) {
  int f = 1;
  k = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c == '-' && (f = -1), c = getchar();
  while (c <= '9' && c >= '0')
    k = k * 10 + c - '0', c = getchar();
  k *= f;
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      read(f[i][j]), ans += f[i][j];
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        if (i == j || i == k || j == k)
          continue;
        if (f[i][k] + f[k][j] < f[i][j])
          return puts("-1"), 0;
        if (f[i][k] + f[k][j] == f[i][j])
          v[i][j] = v[j][i] = 1;
      }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (v[i][j])
        ans -= f[i][j];
  printf("%lld\n", ans >> 1);
  return 0;
}
Problem p03600 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << setprecision(32);

  int n;
  cin >> n;
  int a[n][n];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      int mn = 2e9;
      for (int k = 1; k <= n; k++) {
        if (i != k && j != k)
          mn = min(mn, a[i][k] + a[k][j]);
      }
      if (mn < a[i][j]) {
        cout << -1 << endl;
        exit(0);
      }
      if (mn > a[i][j])
        ans += a[i][j];
    }
  }
  cout << ans << endl;

  return 0;
}

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

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << setprecision(32);

  int n;
  cin >> n;
  int a[n + 1][n + 1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      int mn = 2e9;
      for (int k = 1; k <= n; k++) {
        if (i != k && j != k)
          mn = min(mn, a[i][k] + a[k][j]);
      }
      if (mn < a[i][j]) {
        cout << -1 << endl;
        exit(0);
      }
      if (mn > a[i][j])
        ans += a[i][j];
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03600 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll INF = 1e15;
int V;
vector<vector<ll>> d(30, vector<ll>(30, INF));

void warshall_floyd() {
  for (int k = 0; k < V; k++) {
    for (int i = 0; i < V; i++) {
      for (int j = 0; j < V; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  return;
}

int main() {
  cin >> V;
  vector<vector<ll>> G(V, vector<ll>(V));
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      cin >> G[i][j];
      d[i][j] = G[i][j];
    }
  }
  warshall_floyd();
  ll ans = 0;
  for (int i = 0; i < V; i++) {
    for (int j = i + 1; j < V; j++) {
      if (d[i][j] != G[i][j]) {
        cout << -1 << endl;
        return 0;
      }
      ans += d[i][j];
    }
  }
  for (int i = 0; i < V; i++) {
    for (int j = i + 1; j < V; j++) {
      for (int k = 0; k < V; k++) {
        if (i == k || j == k)
          continue;
        if (d[i][j] == d[i][k] + d[k][j]) {
          ans -= d[i][j];
          break;
        }
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll INF = 1e15;
int V;
vector<vector<ll>> d(300, vector<ll>(300, INF));

void warshall_floyd() {
  for (int k = 0; k < V; k++) {
    for (int i = 0; i < V; i++) {
      for (int j = 0; j < V; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  return;
}

int main() {
  cin >> V;
  vector<vector<ll>> G(V, vector<ll>(V));
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      cin >> G[i][j];
      d[i][j] = G[i][j];
    }
  }
  warshall_floyd();
  ll ans = 0;
  for (int i = 0; i < V; i++) {
    for (int j = i + 1; j < V; j++) {
      if (d[i][j] != G[i][j]) {
        cout << -1 << endl;
        return 0;
      }
      ans += d[i][j];
    }
  }
  for (int i = 0; i < V; i++) {
    for (int j = i + 1; j < V; j++) {
      for (int k = 0; k < V; k++) {
        if (i == k || j == k)
          continue;
        if (d[i][j] == d[i][k] + d[k][j]) {
          ans -= d[i][j];
          break;
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p03600 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>
#include <utility>
#include <vector>
#define LL long long
#define TT std::tuple<LL, int, int>
#define PP std::pair<int, int>
LL D[310];
LL ans;
int N;
std::vector<TT> A;
struct edge {
  int to;
  LL cost;
};
std::vector<edge> list[310];

void dset(int N) {
  for (int i = 0; i < N; i++) {
    D[i] = 1 << 30;
  }
}
int main() {
  std::cin >> N;
  dset(N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      LL tmp;
      std::cin >> tmp;
      if (i == j)
        continue;
      A.push_back(TT{tmp, i, j});
    }
  }
  std::sort(A.begin(), A.end());
  std::unique(A.begin(), A.end());
  for (int jj = 0; jj < (int)A.size(); jj++) {
    dset(N);
    std::priority_queue<PP, std::vector<PP>, std::greater<PP>> pq;
    LL a;
    int b, c;
    std::tie(a, b, c) = A[jj];
    D[b] = 0;
    pq.push({D[b], b});
    while (!pq.empty()) {
      PP p = pq.top();
      pq.pop();
      if (D[p.second] < p.first)
        continue;
      for (int k = 0; k < (int)list[p.second].size(); k++) {
        edge e = list[p.second][k];
        if (D[e.to] <= D[p.second] + e.cost)
          continue;
        D[e.to] = D[p.second] + e.cost;
        pq.push({D[e.to], e.to});
      }
    }
    if (D[c] < a) {
      std::cout << -1 << std::endl;
      return 0;
    }
    if (D[c] > a) {
      list[b].push_back(edge{c, a});
      list[c].push_back(edge{b, a});
      ans += a;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>
#include <utility>
#include <vector>
#define LL long long
#define TT std::tuple<LL, int, int>
#define PP std::pair<int, int>
LL D[310];
LL ans;
int N;
std::vector<TT> A;
struct edge {
  int to;
  LL cost;
};
std::vector<edge> list[310];

void dset(int N) {
  for (int i = 0; i < N; i++) {
    D[i] = 1 << 30;
  }
}
int main() {
  std::cin >> N;
  dset(N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      LL tmp;
      std::cin >> tmp;
      if (i >= j)
        continue;
      A.push_back(TT{tmp, i, j});
    }
  }
  std::sort(A.begin(), A.end());
  std::unique(A.begin(), A.end());
  for (int jj = 0; jj < (int)A.size(); jj++) {
    dset(N);
    std::priority_queue<PP, std::vector<PP>, std::greater<PP>> pq;
    LL a;
    int b, c;
    std::tie(a, b, c) = A[jj];
    D[b] = 0;
    pq.push({D[b], b});
    while (!pq.empty()) {
      PP p = pq.top();
      pq.pop();
      if (D[p.second] < p.first)
        continue;
      for (int k = 0; k < (int)list[p.second].size(); k++) {
        edge e = list[p.second][k];
        if (D[e.to] <= D[p.second] + e.cost)
          continue;
        D[e.to] = D[p.second] + e.cost;
        pq.push({D[e.to], e.to});
      }
    }
    if (D[c] < a) {
      std::cout << -1 << std::endl;
      return 0;
    }
    if (D[c] > a) {
      list[b].push_back(edge{c, a});
      list[c].push_back(edge{b, a});
      ans += a;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p03600 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

struct dat {
  ll d, from, to;
  bool operator<(const dat &another) const { return d < another.d; }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  vector<P> G[100]; // to, dist
  vector<dat> B;
  ll A[305][305];

  ll ans = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> A[i][j];
      if (i < j) {
        B.push_back({A[i][j], i, j});
      }
    }
  }

  sort(B.begin(), B.end());

  for (int i = 0; i < B.size(); i++) {
    dat t = B[i];
    ll dist[500];
    fill(dist, dist + N, LLONG_MAX);
    priority_queue<P, vector<P>, greater<P>> que;
    dist[t.from] = 0;
    que.push(P(0, t.from));
    while (que.size()) {
      P p = que.top();
      que.pop();
      ll now = p.second;
      ll nowdist = p.first;
      if (dist[now] < nowdist)
        continue;
      for (int i = 0; i < G[now].size(); i++) {
        ll to = G[now][i].first, len = G[now][i].second;
        if (dist[to] > nowdist + len) {
          dist[to] = nowdist + len;
          que.push(P(dist[to], to));
        }
      }
    }
    if (dist[t.to] < t.d) {
      cout << -1 << endl;
      return 0;
    } else if (dist[t.to] > t.d) {
      G[t.from].push_back(P(t.to, t.d));
      G[t.to].push_back(P(t.from, t.d));
      ans += t.d;
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

struct dat {
  ll d, from, to;
  bool operator<(const dat &another) const { return d < another.d; }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  vector<P> G[500]; // to, dist
  vector<dat> B;
  ll A[305][305];

  ll ans = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> A[i][j];
      if (i < j) {
        B.push_back({A[i][j], i, j});
      }
    }
  }

  sort(B.begin(), B.end());

  for (int i = 0; i < B.size(); i++) {
    dat t = B[i];
    ll dist[500];
    fill(dist, dist + N, LLONG_MAX);
    priority_queue<P, vector<P>, greater<P>> que;
    dist[t.from] = 0;
    que.push(P(0, t.from));
    while (que.size()) {
      P p = que.top();
      que.pop();
      ll now = p.second;
      ll nowdist = p.first;
      if (dist[now] < nowdist)
        continue;
      for (int i = 0; i < G[now].size(); i++) {
        ll to = G[now][i].first, len = G[now][i].second;
        if (dist[to] > nowdist + len) {
          dist[to] = nowdist + len;
          que.push(P(dist[to], to));
        }
      }
    }
    if (dist[t.to] < t.d) {
      cout << -1 << endl;
      return 0;
    } else if (dist[t.to] > t.d) {
      G[t.from].push_back(P(t.to, t.d));
      G[t.to].push_back(P(t.from, t.d));
      ans += t.d;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03600 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define uniq(x) (x).erase(unique(all(x)), (x).end())

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef std::priority_queue<int> IntPrioQueue; // Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int>>
    IntReversePrioQueue; // A->Z

const int MAX = 256;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1 << 29;
const int MOD = 1000000007;

int path[MAX][MAX];
bool check[MAX][MAX];

void solve(void) {
  int N;
  cin >> N;
  ll ans = 0LL;
  rep(N, i) rep(N, j) {
    scanf("%d", &path[i][j]);
    ans += path[i][j];
  }
  ans /= 2;
  bool f = true;
  rep(N, k) rep(N, i) rep(N, j) {
    if (path[i][j] > path[i][k] + path[k][j])
      f = false;
    else if (i != k && j != k && path[i][j] == path[i][k] + path[k][j] &&
             !check[i][j]) {
      ans -= path[i][j];
      check[i][j] = check[j][i] = true;
    }
  }
  cout << (f ? ans : -1) << endl;
}

int main(void) {
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define uniq(x) (x).erase(unique(all(x)), (x).end())

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef std::priority_queue<int> IntPrioQueue; // Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int>>
    IntReversePrioQueue; // A->Z

const int MAX = 512;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1 << 29;
const int MOD = 1000000007;

int path[MAX][MAX];
bool check[MAX][MAX];

void solve(void) {
  int N;
  cin >> N;
  ll ans = 0LL;
  rep(N, i) rep(N, j) {
    scanf("%d", &path[i][j]);
    ans += path[i][j];
  }
  ans /= 2;
  bool f = true;
  rep(N, k) rep(N, i) rep(N, j) {
    if (path[i][j] > path[i][k] + path[k][j])
      f = false;
    else if (i != k && j != k && path[i][j] == path[i][k] + path[k][j] &&
             !check[i][j]) {
      ans -= path[i][j];
      check[i][j] = check[j][i] = true;
    }
  }
  cout << (f ? ans : -1) << endl;
}

int main(void) {
  solve();
  return 0;
}
Problem p03600 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pp;

#define sz(x) (int)x.size()
#define PI acos(-1)
int const N = 3e5;
int oo = 1e9;
int mod = 998244353;
ll OO = 3e18;

int n, aa[302][302];

void solve() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> aa[i][j];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (i == j || i == k || j == k)
          continue;
        if (aa[i][j] > aa[i][k] + aa[k][j]) {
          cout << -1;
          return;
        }
      }
    }
  }
  ll an = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      bool no = 0;
      for (int k = 0; k < n; k++) {
        if (i == k || j == k)
          continue;
        if (aa[i][j] == aa[i][k] + aa[k][j]) {
          no = 1;
          break;
        }
      }
      if (!no) {
        an += aa[i][j];
      }
    }
  }
  cout << an / 2 << '\n';
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}


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

typedef long long ll;
typedef pair<int, int> pp;

#define sz(x) (int)x.size()
#define PI acos(-1)
int const N = 3e5;
int oo = 1e9;
int mod = 998244353;
ll OO = 3e18;

int n, aa[302][302];

void solve() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> aa[i][j];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (i == j || i == k || j == k)
          continue;
        if (aa[i][j] > aa[i][k] + aa[k][j]) {
          cout << -1;
          return;
        }
      }
    }
  }
  ll an = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      bool no = 0;
      for (int k = 0; k < n; k++) {
        if (i == k || j == k)
          continue;
        if (aa[i][j] == aa[i][k] + aa[k][j]) {
          no = 1;
          break;
        }
      }
      if (!no) {
        an += aa[i][j];
      }
    }
  }
  cout << an / 2 << '\n';
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}
Problem p03600 (Python)

Buggy submission (status: Runtime Error):
inf = float("inf")
N = int(input())
A = []
for _ in range(N):
    a = [int(s) for s in input().split()]
    A.append(a)
d = [a[:] for a in A]
ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        dmin = min([d[i][k] + d[k][j] for k in range(N) if k not in (i, j)])
        if d[i][j] > dmin:
            print(-1)
            exit()
        if d[i][j] != dmin:
            ans += d[i][j]
print(ans)


Fixed submission:
inf = float("inf")
N = int(input())
A = []
for _ in range(N):
    a = [int(s) for s in input().split()]
    A.append(a)
d = [a[:] for a in A]
ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        dmin = min([inf] + [d[i][k] + d[k][j] for k in range(N) if k not in (i, j)])
        if d[i][j] > dmin:
            print(-1)
            exit()
        if d[i][j] != dmin:
            ans += d[i][j]
print(ans)
Problem p03600 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

typedef long long ll;
typedef pair<int, int> P;

ll n;
ll d[100][100];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  REP(i, n) REP(j, n) cin >> d[i][j];
  ll ans = 0;
  REP(i, n) {
    FOR(j, i + 1, n) {
      bool flg = false;
      REP(k, n) {
        if (d[i][j] > d[i][k] + d[k][j]) {
          cout << "-1" << endl;
          return 0;
        }
        if (i != k && j != k && d[i][j] == d[i][k] + d[k][j])
          flg = true;
      }
      if (!flg)
        ans += d[i][j];
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

typedef long long ll;
typedef pair<int, int> P;

ll n;
ll d[300][300];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  REP(i, n) REP(j, n) cin >> d[i][j];
  ll ans = 0;
  REP(i, n) {
    FOR(j, i + 1, n) {
      bool flg = false;
      REP(k, n) {
        if (d[i][j] > d[i][k] + d[k][j]) {
          cout << "-1" << endl;
          return 0;
        }
        if (i != k && j != k && d[i][j] == d[i][k] + d[k][j])
          flg = true;
      }
      if (!flg)
        ans += d[i][j];
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03600 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>

using namespace std;

typedef long long ll;

const ll INF = 1e9;
ll n, ans;
ll a[105][105];
bool bo[105][105];

int main() {
  scanf("%lld", &n);
  //    for(ll i = 1; i <= n; i++) for(ll j = 1; j <= n; j++) a[i][j] = INF;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      scanf("%lld", &a[i][j]);
    }
  }
  for (ll k = 1; k <= n; k++) {
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= n; j++) {
        if (i == k || j == k || i == j)
          continue;
        if (a[i][j] > a[i][k] + a[k][j]) {
          printf("-1");
          return 0;
        }
        if (a[i][j] == a[i][k] + a[k][j]) {
          bo[i][j] = true;
        }
      }
    }
  }
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      ans += (1 - bo[i][j]) * a[i][j];
    }
  }
  printf("%lld", ans / 2);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>

using namespace std;

typedef long long ll;

const ll INF = 1e9;
ll n, ans;
ll a[305][305];
bool bo[305][305];

int main() {
  scanf("%lld", &n);
  //    for(ll i = 1; i <= n; i++) for(ll j = 1; j <= n; j++) a[i][j] = INF;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      scanf("%lld", &a[i][j]);
    }
  }
  for (ll k = 1; k <= n; k++) {
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= n; j++) {
        if (i == k || j == k || i == j)
          continue;
        if (a[i][j] > a[i][k] + a[k][j]) {
          printf("-1");
          return 0;
        }
        if (a[i][j] == a[i][k] + a[k][j]) {
          bo[i][j] = true;
        }
      }
    }
  }
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      ans += (1 - bo[i][j]) * a[i][j];
    }
  }
  printf("%lld", ans / 2);
  return 0;
}
Problem p03601 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int dp[4010][4010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef LOCAL
  std::ifstream in("in");
  std::cin.rdbuf(in.rdbuf());
#endif

  int A, B, C, D, E, F;
  cin >> A >> B >> C >> D >> E >> F;
  dp[0][0] = 1;
  for (int i = 0; i <= F; i++) {
    for (int j = 0; j <= F; j++) {
      if (!dp[i][j])
        continue;
      dp[i + 100 * A][j] = 1;
      dp[i + 100 * B][j] = 1;
      dp[i + C][j + C] = 1;
      dp[i + D][j + D] = 1;
    }
  }
  double maxD = -1;
  int mi = 0, mj = 0;
  for (int i = 1; i <= F; i++) {
    for (int j = 0; j <= F; j++) {
      if (!dp[i][j])
        continue;
      if (E * (i - j) / 100 < j)
        continue;
      double d = (double)j / i;
      if (maxD < d) {
        maxD = d;
        mi = i;
        mj = j;
      }
    }
  }

  cout << mi << " " << mj << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int dp[4010][4010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef LOCAL
  std::ifstream in("in");
  std::cin.rdbuf(in.rdbuf());
#endif

  int A, B, C, D, E, F;
  cin >> A >> B >> C >> D >> E >> F;
  dp[0][0] = 1;
  for (int i = 0; i <= F; i++) {
    for (int j = 0; j <= F; j++) {
      if (!dp[i][j])
        continue;
      if (i + 100 * A <= F)
        dp[i + 100 * A][j] = 1;
      if (i + 100 * B <= F)
        dp[i + 100 * B][j] = 1;
      if (i + C <= F)
        dp[i + C][j + C] = 1;
      if (i + D <= F)
        dp[i + D][j + D] = 1;
    }
  }
  double maxD = -1;
  int mi = 0, mj = 0;
  for (int i = 1; i <= F; i++) {
    for (int j = 0; j <= F; j++) {
      if (!dp[i][j])
        continue;
      if (E * (i - j) / 100 < j)
        continue;
      double d = (double)j / i;
      if (maxD < d) {
        maxD = d;
        mi = i;
        mj = j;
      }
    }
  }

  cout << mi << " " << mj << endl;
}
Problem p03601 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int A, B, C, D, E, F;
bool dp1[1009], dp2[1009];
int main() {
  cin >> A >> B >> C >> D >> E >> F;
  dp1[0] = true;
  for (int i = 1; i <= F; i++) {
    if (i >= A * 100 && dp1[i - A * 100])
      dp1[i] = true;
    if (i >= B * 100 && dp1[i - B * 100])
      dp1[i] = true;
  }
  dp2[0] = true;
  for (int i = 1; i <= F; i++) {
    if (i >= C && dp2[i - C])
      dp2[i] = true;
    if (i >= D && dp2[i - D])
      dp2[i] = true;
  }
  int rp = 0, rq = A * 100;
  for (int i = 0; i <= F; i++) {
    for (int j = 0; j <= F - i; j++) {
      if (dp1[i] && dp2[j] && (100 + E) * j <= E * (i + j) &&
          rp * (i + j) < rq * j) {
        rp = j;
        rq = i + j;
      }
    }
  }
  cout << rq << ' ' << rp << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int A, B, C, D, E, F;
bool dp1[3009], dp2[3009];
int main() {
  cin >> A >> B >> C >> D >> E >> F;
  dp1[0] = true;
  for (int i = 1; i <= F; i++) {
    if (i >= A * 100 && dp1[i - A * 100])
      dp1[i] = true;
    if (i >= B * 100 && dp1[i - B * 100])
      dp1[i] = true;
  }
  dp2[0] = true;
  for (int i = 1; i <= F; i++) {
    if (i >= C && dp2[i - C])
      dp2[i] = true;
    if (i >= D && dp2[i - D])
      dp2[i] = true;
  }
  int rp = 0, rq = A * 100;
  for (int i = 0; i <= F; i++) {
    for (int j = 0; j <= F - i; j++) {
      if (dp1[i] && dp2[j] && (100 + E) * j <= E * (i + j) &&
          rp * (i + j) < rq * j) {
        rp = j;
        rq = i + j;
      }
    }
  }
  cout << rq << ' ' << rp << endl;
  return 0;
}
Problem p03601 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int a, b, c, d, e, f;
int dp[3501];
int dp2[3501];

int main(void) {
  scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
  dp[0] = 1;
  dp2[0] = 1;
  for (int i = 0; i <= 3000; i++) {
    if (dp[i] == 1) {
      dp[i + a * 100] = 1;
      dp[i + b * 100] = 1;
    }
  }
  for (int i = 0; i <= 3000; i++) {
    if (dp2[i] == 1) {
      dp2[i + c] = 1;
      dp2[i + d] = 1;
    }
  }
  int res = -1, res2 = -1;
  for (int i = 1; i <= f; i++) {
    for (int j = 0; j <= i; j++) {
      int wa = i - j;
      if ((i - j) * e >= j * 100 && dp[i - j] == 1 && dp2[j] == 1) {
        if (res == -1) {
          res = i;
          res2 = j;
        }
        if (res2 * (i) < j * (res)) {
          res = i;
          res2 = j;
        }
      }
    }
  }
  printf("%d %d\n", res, res2);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int a, b, c, d, e, f;
int dp[6001];
int dp2[6001];

int main(void) {
  scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
  dp[0] = 1;
  dp2[0] = 1;
  for (int i = 0; i <= 3000; i++) {
    if (dp[i] == 1) {
      dp[i + a * 100] = 1;
      dp[i + b * 100] = 1;
    }
  }
  for (int i = 0; i <= 3000; i++) {
    if (dp2[i] == 1) {
      dp2[i + c] = 1;
      dp2[i + d] = 1;
    }
  }
  int res = -1, res2 = -1;
  for (int i = 1; i <= f; i++) {
    for (int j = 0; j <= i; j++) {
      int wa = i - j;
      if ((i - j) * e >= j * 100 && dp[i - j] == 1 && dp2[j] == 1) {
        if (res == -1) {
          res = i;
          res2 = j;
        }
        if (res2 * (i) < j * (res)) {
          res = i;
          res2 = j;
        }
      }
    }
  }
  printf("%d %d\n", res, res2);
  return 0;
}
Problem p03601 (Python)

Buggy submission (status: Runtime Error):
from itertools import product

a, b, c, d, e, f = map(int, input().split())

lima = [100 * a * i for i in range(f // (100 * a) + 1)]
limb = [100 * b * i for i in range(f // (100 * b) + 1)]
setw = set([i + j for i, j in product(lima, limb) if 0 < i + j <= f])
limc = [c * i for i in range(f // c + 1)]
limd = [d * i for i in range(f // d + 1)]
sets = set([i + j for i, j in product(limc, limd) if 0 < i + j <= f])

cnt = 0
for w in setw:
    for s in sets:
        if (cnt <= 100 * s / w <= e) and (s + w <= f):
            cnt = 100 * s / w
            ans = s + w, s
else:
    print(*ans)


Fixed submission:
from itertools import product

a, b, c, d, e, f = map(int, input().split())

lima = [100 * a * i for i in range(f // (100 * a) + 1)]
limb = [100 * b * i for i in range(f // (100 * b) + 1)]
setw = set([i + j for i, j in product(lima, limb) if 0 < i + j <= f])
limc = [c * i for i in range(f // c + 1)]
limd = [d * i for i in range(f // d + 1)]
sets = set([i + j for i, j in product(limc, limd) if 0 < i + j <= f])

cnt, ans = 0, (max(setw), 0)
for w in setw:
    for s in sets:
        if (cnt <= 100 * s / w <= e) and (s + w <= f):
            cnt = 100 * s / w
            ans = s + w, s
else:
    print(*ans)
Problem p03601 (Python)

Buggy submission (status: Time Limit Exceeded):
A, B, C, D, E, F = map(int, input().split())

tmp = 0
ans_A = 0
ans_B = 0
for a in range(31):
    for b in range(31):
        x = 100 * (a * A + b * B)
        for c in range((x // 100 * E) // C + 1):
            for d in range(((x // 100 * E) - c) // D + 1):
                y = c * C + d * D
                if x + y > F or y > x * E // 100:
                    break
                if x + y != 0:
                    if y / (x + y) > tmp:
                        ans_A = x + y
                        ans_B = y
                        tmp = y / (x + y)

if ans_A == 0:
    print(100 * A, 0)
else:
    print(ans_A, ans_B)


Fixed submission:
A, B, C, D, E, F = map(int, input().split())

tmp = 0
ans_A = 0
ans_B = 0
for a in range(31):
    for b in range(31):
        x = 100 * (a * A + b * B)
        if x > F:
            break
        for c in range((x // 100 * E) // C + 1):
            for d in range(((x // 100 * E) - c) // D + 1):
                y = c * C + d * D
                if x + y > F or y > x * E // 100:
                    break
                if x + y != 0:
                    if y / (x + y) > tmp:
                        ans_A = x + y
                        ans_B = y
                        tmp = y / (x + y)

if ans_A == 0:
    print(100 * A, 0)
else:
    print(ans_A, ans_B)