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

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int n;
vector<int> a;

ll dp[2003][2003][2];
ll solve(int left, int right, int player) {
  if (left == right)
    return player * a[left];

  ll maxi = -1;
  if ((!player && a[left] > a[right]) || player)
    maxi =
        max<ll>(maxi, solve((left + 1) % n, right, !player) + player * a[left]);
  if ((!player && a[left] < a[right]) || player)
    maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, !player) +
                             player * a[right]);

  return dp[left][right][player] = maxi;
}

int main() {
  rep(i, 2003) rep(j, 2003) rep(k, 2) dp[i][j][k] = -1;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];

  ll maxi = -1;
  rep(i, n) maxi = max(maxi, solve((i + 1) % n, (i - 1 + n) % n, 0) + a[i]);
  cout << maxi << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int n;
vector<int> a;

ll dp[2003][2003][2];
ll solve(int left, int right, int player) {
  if (left == right)
    return player * a[left];
  if (dp[left][right][player] != -1)
    return dp[left][right][player];

  ll maxi = -1;
  if ((!player && a[left] > a[right]) || player)
    maxi =
        max<ll>(maxi, solve((left + 1) % n, right, !player) + player * a[left]);
  if ((!player && a[left] < a[right]) || player)
    maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, !player) +
                             player * a[right]);

  return dp[left][right][player] = maxi;
}

int main() {
  rep(i, 2003) rep(j, 2003) rep(k, 2) dp[i][j][k] = -1;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];

  ll maxi = -1;
  rep(i, n) maxi = max(maxi, solve((i + 1) % n, (i - 1 + n) % n, 0) + a[i]);
  cout << maxi << endl;
}
Problem p00538 (C++)

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

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int n;
long long ans = 0, sum[2001], a[4001], b[4001][4001], x[4001][4001];

void read() {
  scanf("%d", &n);
  FOR(i, 1, n)
  scanf("%lld", &a[i]);
  FOR(i, 1, n)
  a[n + i] = a[i];
}

void solve() {
  FOR(i, 1, 2 * n)
  sum[i] = sum[i - 1] + a[i];
  FOR(j, 1, 2 * n)
  FORD(i, j, 1) {
    if (i == j) {
      b[i][j] = a[i];
      x[i][j] = a[i];
    } else {
      b[i][j] = (sum[j] - sum[i - 1]) - min(x[i + 1][j], x[i][j - 1]);
      if (a[i] > a[j])
        x[i][j] = a[i] + (sum[j] - sum[i] - b[i + 1][j]);
      else
        x[i][j] = a[j] + (sum[j - 1] - sum[i - 1] - b[i][j - 1]);
    }
  }
}

void process() {
  solve();
  FOR(i, 1, n) {
    ans = max(ans, a[i] + sum[i + n - 1] - sum[i] - x[i + 1][i + n - 1]);
  }
  cout << ans << '\n';
}

int main() {
  read();
  process();
}

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

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int n;
long long ans = 0, sum[4001], a[4001], b[4001][4001], x[4001][4001];

void read() {
  scanf("%d", &n);
  FOR(i, 1, n)
  scanf("%lld", &a[i]);
  FOR(i, 1, n)
  a[n + i] = a[i];
}

void solve() {
  FOR(i, 1, 2 * n)
  sum[i] = sum[i - 1] + a[i];
  FOR(j, 1, 2 * n)
  FORD(i, j, 1) {
    if (i == j) {
      b[i][j] = a[i];
      x[i][j] = a[i];
    } else {
      b[i][j] = (sum[j] - sum[i - 1]) - min(x[i + 1][j], x[i][j - 1]);
      if (a[i] > a[j])
        x[i][j] = a[i] + (sum[j] - sum[i] - b[i + 1][j]);
      else
        x[i][j] = a[j] + (sum[j - 1] - sum[i - 1] - b[i][j - 1]);
    }
  }
}

void process() {
  solve();
  FOR(i, 1, n) {
    ans = max(ans, a[i] + sum[i + n - 1] - sum[i] - x[i + 1][i + n - 1]);
  }
  cout << ans << '\n';
}

int main() {
  read();
  process();
}
Problem p00538 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

long long int DP[2000][2000];
long long int A[2000];

long long int INF = 100000000000000LL;

int main() {

  int N;
  cin >> N;

  for (int i = 0; i < 2100; i++) {
    for (int j = 0; j < 2100; j++) {
      DP[i][j] = -INF;
    }
  }

  for (int i = 0; i < N; i++) {
    cin >> A[i];
    DP[i][i] = A[i];
  }

  for (int i = 1; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i % 2 == 0) {
        DP[j][(j + i) % N] = max(DP[(j + 1) % N][(j + i) % N] + A[j],
                                 DP[j][(j + i - 1) % N] + A[(j + i) % N]);
      } else {
        if (A[j] >= A[(j + i + 1) % N]) {
          DP[j][(j + i) % N] =
              max(DP[j][(j + i) % N], DP[(j + 1) % N][(j + i) % N]);
        }
        if (A[(j - 1 + N) % N] <= A[(j + i) % N]) {
          DP[j][(j + i) % N] = max(DP[j][(j + i) % N], DP[j][(j + i - 1) % N]);
        }
      }
      // cout << DP[j][(j + i) % N] << " ";
    }
    // cout << endl;
  }

  long long int ans = 0;

  for (int i = 0; i < N; i++) {
    ans = max(ans, DP[i][(i - 1 + N) % N]);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

long long int DP[2000][2000];
long long int A[2000];

long long int INF = 100000000000000LL;

int main() {

  int N;
  cin >> N;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      DP[i][j] = -INF;
    }
  }

  for (int i = 0; i < N; i++) {
    cin >> A[i];
    DP[i][i] = A[i];
  }

  for (int i = 1; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i % 2 == 0) {
        DP[j][(j + i) % N] = max(DP[(j + 1) % N][(j + i) % N] + A[j],
                                 DP[j][(j + i - 1) % N] + A[(j + i) % N]);
      } else {
        if (A[j] >= A[(j + i + 1) % N]) {
          DP[j][(j + i) % N] =
              max(DP[j][(j + i) % N], DP[(j + 1) % N][(j + i) % N]);
        }
        if (A[(j - 1 + N) % N] <= A[(j + i) % N]) {
          DP[j][(j + i) % N] = max(DP[j][(j + i) % N], DP[j][(j + i - 1) % N]);
        }
      }
      // cout << DP[j][(j + i) % N] << " ";
    }
    // cout << endl;
  }

  long long int ans = 0;

  for (int i = 0; i < N; i++) {
    ans = max(ans, DP[i][(i - 1 + N) % N]);
  }

  cout << ans << endl;

  return 0;
}
Problem p00538 (C++)

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

#define int long long

#define PB push_back
#define PPB pop_back
#define MK make_pair
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<string, int> psi;
typedef pair<int, pii> pipii;

constexpr int INF = 1LL << 61;
constexpr int MOD = 1000000007;
constexpr int MAX_N = 2005;

int n, a[MAX_N], mem[MAX_N][MAX_N];

int Dfs(int s, int t) {
  cerr << s << " " << t << endl;
  if (mem[s][t] != -1)
    return mem[s][t];
  if (s == t)
    return 0;

  if (a[s] < a[t]) {
    t++;
    t %= n;
  } else {
    s += n;
    s--;
    s %= n;
  }

  if (mem[s][t] != -1)
    return mem[s][t];
  if (s == t)
    return a[s];

  return mem[s][t] =
             max(a[s] + Dfs((s + n - 1) % n, t), a[t] + Dfs(s, (t + 1) % n));
}

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

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  memset(mem, -1, sizeof(mem));

  int ans = -1;
  for (int i = 0; i < n; ++i) {
    ans = max(ans, a[i] + Dfs((i + n - 1) % n, (i + 1) % n));
  }

  cout << ans << endl;
}

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

#define int long long

#define PB push_back
#define PPB pop_back
#define MK make_pair
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<string, int> psi;
typedef pair<int, pii> pipii;

constexpr int INF = 1LL << 61;
constexpr int MOD = 1000000007;
constexpr int MAX_N = 2005;

int n, a[MAX_N], mem[MAX_N][MAX_N];

int Dfs(int s, int t) {
  if (mem[s][t] != -1)
    return mem[s][t];
  if (s == t)
    return 0;

  if (a[s] < a[t]) {
    t++;
    t %= n;
  } else {
    s += n;
    s--;
    s %= n;
  }

  if (mem[s][t] != -1)
    return mem[s][t];
  if (s == t)
    return a[s];

  return mem[s][t] =
             max(a[s] + Dfs((s + n - 1) % n, t), a[t] + Dfs(s, (t + 1) % n));
}

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

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  memset(mem, -1, sizeof(mem));

  int ans = -1;
  for (int i = 0; i < n; ++i) {
    ans = max(ans, a[i] + Dfs((i + n - 1) % n, (i + 1) % n));
  }

  cout << ans << endl;
}
Problem p00538 (C++)

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

#define int long long

typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

int N, A[114514];

int memo[6666][6666];

int dfs(int l, int r) {
  if (r - l - 1 == N)
    return 0;
  int &ret = memo[l][r];
  if (ret != -1)
    return ret;

  if ((r - l - 1) & 1) {
    if (A[l] > A[r])
      ret = dfs(l - 1, r);
    else
      ret = dfs(l, r + 1);
  } else {
    ret = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]);
  }
  return ret;
}

signed main() {
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    A[i + N + N] = A[i + N] = A[i];
  }
  memset(memo, -1, sizeof(memo));
  int ans = 0;
  rep(i, N) chmax(ans, A[i + N] + dfs(i + N - 1, i + N + 1));
  cout << ans << endl;
  return 0;
}

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

#define int long long

typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

int N, A[114514];

int memo[4000][6000];

int dfs(int l, int r) {
  if (r - l - 1 == N)
    return 0;
  int &ret = memo[l][r];
  if (ret != -1)
    return ret;

  if ((r - l - 1) & 1) {
    if (A[l] > A[r])
      ret = dfs(l - 1, r);
    else
      ret = dfs(l, r + 1);
  } else {
    ret = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]);
  }
  return ret;
}

signed main() {
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    A[i + N + N] = A[i + N] = A[i];
  }
  memset(memo, -1, sizeof(memo));
  int ans = 0;
  rep(i, N) chmax(ans, A[i + N] + dfs(i + N - 1, i + N + 1));
  cout << ans << endl;
  return 0;
}
Problem p00538 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
#define JOI 1
#define IOI 0

int n;
vector<int> a;

ll dp[2003][2003][2];
ll solve(int left, int right, int player) {
  //    cout<<left<<" "<<right<<" "<<player<<endl;
  if (left == right) {
    if (player == JOI)
      return a[left];
    else
      return 0;
  }
  //    if(dp[left][right][player]!=-1)return dp[left][right][player];

  ll maxi = -1;
  if (player == IOI) {
    if (a[left] > a[right])
      maxi = max<ll>(maxi, solve((left + 1) % n, right, JOI));
    if (a[left] < a[right])
      maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, JOI));
  } else {
    maxi = max<ll>(maxi, solve((left + 1) % n, right, IOI) + a[left]);
    maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, IOI) + a[right]);
  }
  //    cout<<left<<" "<<right<<" "<<player<<" "<<maxi<<endl;
  return dp[left][right][player] = maxi;
}

int main() {
  rep(i, 2003) rep(j, 2003) rep(k, 2) dp[i][j][k] = -1;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];

  ll maxi = -1;
  rep(i, n) {
    int left = (i + 1) % n;
    int right = (i - 1 + n) % n;

    ll res = solve(left, right, IOI);
    //        cout<<i<<" "<<res<<endl;
    maxi = max(maxi, res + a[i]);
  }
  cout << maxi << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
#define JOI 1
#define IOI 0

int n;
vector<int> a;

ll dp[2003][2003][2];
ll solve(int left, int right, int player) {
  //    cout<<left<<" "<<right<<" "<<player<<endl;
  if (left == right) {
    if (player == JOI)
      return a[left];
    else
      return 0;
  }
  if (dp[left][right][player] != -1)
    return dp[left][right][player];

  ll maxi = -1;
  if (player == IOI) {
    if (a[left] > a[right])
      maxi = max<ll>(maxi, solve((left + 1) % n, right, JOI));
    if (a[left] < a[right])
      maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, JOI));
  } else {
    maxi = max<ll>(maxi, solve((left + 1) % n, right, IOI) + a[left]);
    maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, IOI) + a[right]);
  }
  //    cout<<left<<" "<<right<<" "<<player<<" "<<maxi<<endl;
  return dp[left][right][player] = maxi;
}

int main() {
  rep(i, 2003) rep(j, 2003) rep(k, 2) dp[i][j][k] = -1;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];

  ll maxi = -1;
  rep(i, n) {
    int left = (i + 1) % n;
    int right = (i - 1 + n) % n;

    ll res = solve(left, right, IOI);
    //        cout<<i<<" "<<res<<endl;
    maxi = max(maxi, res + a[i]);
  }
  cout << maxi << endl;
}
Problem p00538 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

ll dp[4040][6060];
int main(void) {
  int N;
  ll piece[6060];
  memset(piece, 0, sizeof piece);
  memset(dp, 0, sizeof dp);
  cin >> N;
  REP(i, N) {
    ll a;
    cin >> a;
    piece[i] = a;
    piece[N * 1 + i] = a;
    piece[N * 2 + i] = a;
  }
  FOR(i, N, 2 * N) { dp[i][i + 1] = piece[i]; }
  FOR(k, 1, N + 1) {
    REP(i, min(4040, 3 * N - k)) {
      int l = i, r = i + k;
      if (k % 2 == 0) {
        if (piece[l] >= piece[r]) {
          dp[l][r] = max(dp[l][r], dp[l + 1][r]);
        }
        if (l > 0 && piece[l - 1] <= piece[r - 1]) {
          dp[l][r] = max(dp[l][r], dp[l][r - 1]);
        }
      } else {
        dp[l][r] = max(dp[l][r], dp[l + 1][r] + piece[l]);
        dp[l][r] = max(dp[l][r], dp[l][r - 1] + piece[r - 1]);
      }
    }
  }
  ll res = 0;
  REP(i, N) { res = max(res, dp[i][i + N]); }
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

ll dp[4040][6060];
int main(void) {
  int N;
  ll piece[6060];
  memset(piece, 0, sizeof piece);
  memset(dp, 0, sizeof dp);
  cin >> N;
  REP(i, N) {
    ll a;
    cin >> a;
    piece[i] = a;
    piece[N * 1 + i] = a;
    piece[N * 2 + i] = a;
  }
  FOR(i, N, 2 * N) { dp[i][i + 1] = piece[i]; }
  FOR(k, 1, N + 1) {
    REP(i, 2 * N) {
      int l = i, r = i + k;
      if (k % 2 == 0) {
        if (piece[l] >= piece[r]) {
          dp[l][r] = max(dp[l][r], dp[l + 1][r]);
        }
        if (l > 0 && piece[l - 1] <= piece[r - 1]) {
          dp[l][r] = max(dp[l][r], dp[l][r - 1]);
        }
      } else {
        dp[l][r] = max(dp[l][r], dp[l + 1][r] + piece[l]);
        dp[l][r] = max(dp[l][r], dp[l][r - 1] + piece[r - 1]);
      }
    }
  }
  ll res = 0;
  REP(i, N) { res = max(res, dp[i][i + N]); }
  cout << res << endl;
}
Problem p00539 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long int LL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VP;
const LL INF = 3e18;

LL n, m, c;
LL a[200010], b[200010], d[200010];

VP road[100010];
LL dist[100010];
PLL dist_road[100010];
priority_queue<PLL, VP, greater<PLL>> Q;

LL last = 0, ans = INF;
LL last_dist = 0;

void dijk() {
  memset(dist, -1, sizeof dist);
  Q.push(MP(0, 1));

  while (!Q.empty()) {
    PLL now = Q.top();
    Q.pop();

    if (dist[now.second] == -1) {
      dist[now.second] = now.first;

      for (LL i = 0; i < road[now.second].size(); i++)
        Q.push(MP(now.first + road[now.second][i].first,
                  road[now.second][i].second));
    }
  }
  return;
}

int main() {
  scanf("%lld%lld%lld", &n, &m, &c);
  for (LL i = 0; i < m; i++) {
    scanf("%lld%lld%lld", &a[i], &b[i], &d[i]);
    road[a[i]].PB(MP(d[i], b[i]));
    road[b[i]].PB(MP(d[i], a[i]));
  }

  dijk();

  for (LL i = 0; i < m; i++)
    dist_road[i] = MP(max(dist[a[i]], dist[b[i]]), i);

  sort(dist_road, dist_road + m);

  for (LL i = 0; i < m; i++)
    last += d[i];
  ans = last;

  for (LL i = 0; i < m; i++) {
    last += c * (dist_road[i].first - last_dist);
    last_dist = dist_road[i].first;
    last -= d[dist_road[i].second];

    ans = min(ans, last);
  }

  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long int LL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VP;
const LL INF = 3e18;

LL n, m, c;
LL a[200010], b[200010], d[200010];

VP road[100010];
LL dist[100010];
PLL dist_road[200010];
priority_queue<PLL, VP, greater<PLL>> Q;

LL last = 0, ans = INF;
LL last_dist = 0;

void dijk() {
  memset(dist, -1, sizeof dist);
  Q.push(MP(0, 1));

  while (!Q.empty()) {
    PLL now = Q.top();
    Q.pop();

    if (dist[now.second] == -1) {
      dist[now.second] = now.first;

      for (LL i = 0; i < road[now.second].size(); i++)
        Q.push(MP(now.first + road[now.second][i].first,
                  road[now.second][i].second));
    }
  }
  return;
}

int main() {
  scanf("%lld%lld%lld", &n, &m, &c);
  for (LL i = 0; i < m; i++) {
    scanf("%lld%lld%lld", &a[i], &b[i], &d[i]);
    road[a[i]].PB(MP(d[i], b[i]));
    road[b[i]].PB(MP(d[i], a[i]));
  }

  dijk();

  for (LL i = 0; i < m; i++)
    dist_road[i] = MP(max(dist[a[i]], dist[b[i]]), i);

  sort(dist_road, dist_road + m);

  for (LL i = 0; i < m; i++)
    last += d[i];
  ans = last;

  for (LL i = 0; i < m; i++) {
    last += c * (dist_road[i].first - last_dist);
    last_dist = dist_road[i].first;
    last -= d[dist_road[i].second];

    ans = min(ans, last);
  }

  printf("%lld\n", ans);
  return 0;
}
Problem p00539 (C++)

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

map<int, int> S;
int mincost[100000];
vector<P> rinsetu[100000];
signed main() {
  int sum = 0;
  int a, b, c;
  cin >> a >> b >> c;
  for (int d = 0; d < b; d++) {
    int e, f, g;
    scanf("%lld%lld%lld", &e, &f, &g);
    sum += g;
    e--;
    f--;
    rinsetu[e].push_back(P(g, f));
    rinsetu[f].push_back(P(g, e));
  }
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, 0));
  fill(mincost, mincost + a, LLONG_MAX / 3);
  mincost[0] = 0;
  while (Q.size()) {
    P o = Q.top();
    Q.pop();
    if (o.first > mincost[o.second])
      continue;
    for (P t : rinsetu[o.second]) {
      if (mincost[t.second] > o.first + t.first) {
        mincost[t.second] = o.first + t.first;
        Q.push(P(mincost[t.second], t.second));
      }
    }
  }
  for (int i = 0; i < b; i++) {
    for (P j : rinsetu[i]) {
      if (i < j.second)
        S[max(mincost[i], mincost[j.second])] += j.first;
    }
  }
  int MIN = sum;
  for (auto t = S.begin(), p = ++(S.begin()); p != S.end(); t++, p++) {
    (*p).second += (*t).second;
  }
  for (auto i = S.begin(); i != S.end(); i++) {
    MIN = min(MIN, (*i).first * c + sum - (*i).second);
  }
  cout << MIN << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define P pair<int, int> // cost to
using namespace std;

map<int, int> S;
int mincost[200000];
vector<P> rinsetu[200000];
signed main() {
  int sum = 0;
  int a, b, c;
  cin >> a >> b >> c;
  for (int d = 0; d < b; d++) {
    int e, f, g;
    scanf("%lld%lld%lld", &e, &f, &g);
    sum += g;
    e--;
    f--;
    rinsetu[e].push_back(P(g, f));
    rinsetu[f].push_back(P(g, e));
  }
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, 0));
  fill(mincost, mincost + a, LLONG_MAX / 3);
  mincost[0] = 0;
  while (Q.size()) {
    P o = Q.top();
    Q.pop();
    if (o.first > mincost[o.second])
      continue;
    for (P t : rinsetu[o.second]) {
      if (mincost[t.second] > o.first + t.first) {
        mincost[t.second] = o.first + t.first;
        Q.push(P(mincost[t.second], t.second));
      }
    }
  }
  for (int i = 0; i < b; i++) {
    for (P j : rinsetu[i]) {
      if (i < j.second)
        S[max(mincost[i], mincost[j.second])] += j.first;
    }
  }
  int MIN = sum;
  for (auto t = S.begin(), p = ++(S.begin()); p != S.end(); t++, p++) {
    (*p).second += (*t).second;
  }
  for (auto i = S.begin(); i != S.end(); i++) {
    MIN = min(MIN, (*i).first * c + sum - (*i).second);
  }
  cout << MIN << endl;
}
Problem p00539 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAX 10005
typedef long long ll;
typedef pair<ll, ll> P;
struct edge {
  ll to, cost;
};
ll N, M, C, a, b, c;
vector<edge> G[MAX];
ll d[MAX];

void dijkstra() {
  for (int i = 0; i < MAX; i++)
    d[i] = (1LL << 60);
  d[1] = 0;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, 1));
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    ll pos = p.second, cost = p.first;
    if (d[pos] < cost)
      continue;
    for (int i = 0; i < (int)G[pos].size(); i++) {
      edge e = G[pos][i];
      if (d[e.to] > cost + e.cost) {
        d[e.to] = cost + e.cost;
        Q.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  ll sum = 0;
  scanf("%lld %lld %lld", &N, &M, &C);
  for (int i = 0; i < M; i++) {
    scanf("%lld %lld %lld", &a, &b, &c);
    sum += c;
    G[a].push_back((edge){b, c});
    G[b].push_back((edge){a, c});
  }
  dijkstra();
  map<ll, ll> mp;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < (int)G[i].size(); j++) {
      edge e = G[i][j];
      if (i < e.to)
        mp[max(d[i], d[e.to])] += e.cost;
    }
  }
  ll ans = sum;
  map<ll, ll>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    ll X = it->first, D = it->second;
    sum -= D;
    ans = min(ans, X * C + sum);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAX 100005
typedef long long ll;
typedef pair<ll, ll> P;
struct edge {
  ll to, cost;
};
ll N, M, C, a, b, c;
vector<edge> G[MAX];
ll d[MAX];

void dijkstra() {
  for (int i = 0; i < MAX; i++)
    d[i] = (1LL << 60);
  d[1] = 0;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, 1));
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    ll pos = p.second, cost = p.first;
    if (d[pos] < cost)
      continue;
    for (int i = 0; i < (int)G[pos].size(); i++) {
      edge e = G[pos][i];
      if (d[e.to] > cost + e.cost) {
        d[e.to] = cost + e.cost;
        Q.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  ll sum = 0;
  scanf("%lld %lld %lld", &N, &M, &C);
  for (int i = 0; i < M; i++) {
    scanf("%lld %lld %lld", &a, &b, &c);
    sum += c;
    G[a].push_back((edge){b, c});
    G[b].push_back((edge){a, c});
  }
  dijkstra();
  map<ll, ll> mp;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < (int)G[i].size(); j++) {
      edge e = G[i][j];
      if (i < e.to)
        mp[max(d[i], d[e.to])] += e.cost;
    }
  }
  ll ans = sum;
  map<ll, ll>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    ll X = it->first, D = it->second;
    sum -= D;
    ans = min(ans, X * C + sum);
  }
  cout << ans << endl;
  return 0;
}
Problem p00540 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define mod 100000
#define INF 100000000 // 10^8
#define LLINF 4000000000000000000
#define SIZE 100 // 100000

int n;
int P[SIZE] = {0};
vector<int> D;

bool check(int k) {
  int dp[SIZE * 2] = {0};

  for (int i = 0; i < n; i++) {
    if (P[i] > 0) {
      if (P[i] >= k)
        dp[i] = 0;
      else
        dp[i] = INF;
    } else
      dp[i] = 1;
  }

  //[l,r)
  int l = 0, r = n, sum = 0;

  while (l + 1 < r) {

    sort(&dp[l], &dp[l] + 3);

    sum = dp[l] + dp[l + 1];

    if (sum >= INF)
      dp[r] = INF;
    else
      dp[r] = sum;

    l += 3;
    r++;
  }

  int b = (int)(D.end() - lower_bound(D.begin(), D.end(), k));

  if (dp[l] <= b) {
    return true;
  }

  return false;
}

int main() {
  int m, p, d;

  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &d, &p);
    P[p - 1] = d;
  }

  for (int i = 0; i < n - m; i++) {
    scanf("%d", &d);
    D.push_back(d);
  }

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

  //[l,r]
  int l = 1, r = 1000000000, mid;

  while (l < r) {
    mid = (l + r + 1) / 2;

    if (check(mid)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }

  printf("%d\n", l);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define mod 100000
#define INF 100000000 // 10^8
#define LLINF 4000000000000000000
#define SIZE 100000

int n;
int P[SIZE] = {0};
vector<int> D;

bool check(int k) {
  int dp[SIZE * 2] = {0};

  for (int i = 0; i < n; i++) {
    if (P[i] > 0) {
      if (P[i] >= k)
        dp[i] = 0;
      else
        dp[i] = INF;
    } else
      dp[i] = 1;
  }

  //[l,r)
  int l = 0, r = n, sum = 0;

  while (l + 1 < r) {

    sort(&dp[l], &dp[l] + 3);

    sum = dp[l] + dp[l + 1];

    if (sum >= INF)
      dp[r] = INF;
    else
      dp[r] = sum;

    l += 3;
    r++;
  }

  int b = (int)(D.end() - lower_bound(D.begin(), D.end(), k));

  if (dp[l] <= b) {
    return true;
  }

  return false;
}

int main() {
  int m, p, d;

  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &d, &p);
    P[p - 1] = d;
  }

  for (int i = 0; i < n - m; i++) {
    scanf("%d", &d);
    D.push_back(d);
  }

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

  //[l,r]
  int l = 1, r = 1000000000, mid;

  while (l < r) {
    mid = (l + r + 1) / 2;

    if (check(mid)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }

  printf("%d\n", l);

  return 0;
}
Problem p00542 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main(void) {
  int e, f, x, y[4], z, i, j;
  scanf("%d %d %d %d %d %d", y[0], y[1], y[2], y[3], &e, &f);
  for (i = 0; i < 3; i++) {
    for (j = i + 1; j < 4; j++) {
      if (y[i] < y[j]) {
        z = y[i];
        y[i] = y[j];
        y[j] = z;
      }
    }
  }
  x = y[0] + y[1] + y[2];
  if (e >= f) {
    x = x + e;
    printf("%d\n", x);
  } else {
    x = x + f;
    printf("%d\n", x);
  }
  return 0;
}


Fixed submission:
#include <stdio.h>
int main(void) {
  int e, f, x, y[4], z, i, j;
  scanf("%d %d %d %d %d %d", &y[0], &y[1], &y[2], &y[3], &e, &f);
  for (i = 0; i < 3; i++) {
    for (j = i + 1; j < 4; j++) {
      if (y[i] < y[j]) {
        z = y[i];
        y[i] = y[j];
        y[j] = z;
      }
    }
  }
  x = y[0] + y[1] + y[2];
  if (e >= f) {
    x = x + e;
    printf("%d\n", x);
  } else {
    x = x + f;
    printf("%d\n", x);
  }
  return 0;
}
Problem p00542 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define GPA 4
#define ROLLMAN endl

using namespace std;
typedef int OTAKU;

OTAKU main() {

  OTAKU roll[GPA], man[GPA];

  for (OTAKU i = 0; i < 4; i++)
    cin >> roll[i];
  for (OTAKU i = 0; i < 2; i++)
    cin >> man[i];

  sort(roll, roll + 4);
  sort(man, man + 2);

  OTAKU Happy_Birthday = 0;

  for (OTAKU i = 3; i > 0; i--)
    Happy_Birthday += roll[i];
  Happy_Birthday += man[1];

  cout << Happy_Birthday << ROLLMAN;
  return GPA;
}

Fixed submission:
#include <bits/stdc++.h>
#define GPA 4
#define ROLLMAN endl

using namespace std;
typedef int OTAKU;

OTAKU main() {

  OTAKU roll[GPA], man[GPA];

  for (OTAKU i = 0; i < 4; i++)
    cin >> roll[i];
  for (OTAKU i = 0; i < 2; i++)
    cin >> man[i];

  sort(roll, roll + 4);
  sort(man, man + 2);

  OTAKU Happy_Birthday = 0;

  for (OTAKU i = 3; i > 0; i--)
    Happy_Birthday += roll[i];
  Happy_Birthday += man[1];

  cout << Happy_Birthday << ROLLMAN;
  return GPA - 4;
}
Problem p00544 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
using namespace std;

int a, b, ans = 0, W = 0, B = 0, R = 0;

int tansaku(int iti, int ryo, vector<string> c) {
  int kariAns = 0;
  for (int i = 0; i < b; i++) {
    for (int j = 0; j < ryo; j++) {
      if (c[iti + j][i] != 'B')
        kariAns++;
    }
    for (int j = 1; j < iti; j++) {
      if (c[iti - j][i] != 'W')
        kariAns++;
    }
    for (int j = iti + ryo; j < a - 1; j++) {
      if (c[j][i] != 'R')
        kariAns++;
    }
  }
  if (iti == a - 1) {
    iti = 1;
    ryo++;
  } else {
    iti++;
  }
  if (ryo >= a - 1)
    return kariAns;
  return min(kariAns, tansaku(iti, ryo, c));
}

int main() {
  cin >> a >> b;
  vector<string> c(a);
  for (int i = 0; i < a; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < b; i++) {
    if (c[0][i] != 'W')
      ans++;
    if (c[c.size() - 1][i] != 'R')
      ans++;
  }
  cout << ans + tansaku(1, 1, c) << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
using namespace std;

int a, b, ans = 0, W = 0, B = 0, R = 0;

int tansaku(int iti, int ryo, vector<string> c) {
  int kariAns = 0;
  for (int i = 0; i < b; i++) {
    for (int j = 0; j < ryo; j++) {
      if (c[iti + j][i] != 'B')
        kariAns++;
    }
    for (int j = 1; j < iti; j++) {
      if (c[iti - j][i] != 'W')
        kariAns++;
    }
    for (int j = iti + ryo; j < a - 1; j++) {
      if (c[j][i] != 'R')
        kariAns++;
    }
  }
  if (iti + ryo == a - 1) {
    iti = 1;
    ryo++;
  } else {
    iti++;
  }
  if (ryo >= a - 1)
    return kariAns;
  return min(kariAns, tansaku(iti, ryo, c));
}

int main() {
  cin >> a >> b;
  vector<string> c(a);
  for (int i = 0; i < a; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < b; i++) {
    if (c[0][i] != 'W')
      ans++;
    if (c[c.size() - 1][i] != 'R')
      ans++;
  }
  cout << ans + tansaku(1, 1, c) << endl;
}
Problem p00545 (C++)

Buggy submission (status: Runtime Error):

#include <cstdio>
#include <vector>

using namespace std;

using ll = long long;

const ll inf = 4e18;

ll n, t, q;
ll a[123456];
bool d[123456];
ll x[1000];
vector<ll> talkp;
ll place[123456];

inline ll mean(ll a, ll b) { return a / 2 + b / 2; }

int main() {
  scanf("%lld %lld %lld", &n, &t, &q);
  a[0] = -inf;
  d[0] = true;
  for (int i = 1; i <= n; ++i) {
    int tmpd;
    scanf("%lld %d", a + i, &tmpd);
    d[i] = (tmpd == 1);
  }
  a[n + 1] = inf;
  d[n + 1] = false;
  for (int j = 0; j < q; ++j) {
    scanf("%lld", x + j);
  }
  for (int i = 0; i <= n; ++i) {
    if (d[i] && !d[i + 1]) {
      talkp.push_back(mean(a[i], a[i + 1]));
    }
  }
  auto fritr = talkp.begin();
  auto bkitr = talkp.begin();

  for (int i = 0; i <= n; ++i) {
    place[i] = a[i];
    while ((*fritr) < a[i]) {
      bkitr = fritr;
      ++fritr;
    }
    if (d[i]) {
      ll dist = (*fritr) - a[i];
      if (dist < t)
        place[i] += dist;
      else
        place[i] += t;
    } else {
      ll dist = a[i] - (*bkitr);
      if (dist < t)
        place[i] -= dist;
      else
        place[i] -= t;
    }
  }
  for (int j = 0; j < q; ++j) {
    printf("%lld\n", place[x[j]]);
  }
  return 0;
}

Fixed submission:

#include <cstdio>
#include <vector>

using namespace std;

using ll = long long;

const ll inf = 4e18;

ll n, t, q;
ll a[123456];
bool d[123456];
ll x[1000];
vector<ll> talkp;
ll place[123456];

inline ll mean(ll a, ll b) { return a / 2 + b / 2; }

int main() {
  scanf("%lld %lld %lld", &n, &t, &q);
  a[0] = -inf;
  d[0] = true;
  for (int i = 1; i <= n; ++i) {
    int tmpd;
    scanf("%lld %d", a + i, &tmpd);
    d[i] = (tmpd == 1);
  }
  a[n + 1] = inf;
  d[n + 1] = false;
  for (int j = 0; j < q; ++j) {
    scanf("%lld", x + j);
  }
  for (int i = 0; i <= n; ++i) {
    if (d[i] && !d[i + 1]) {
      talkp.push_back(mean(a[i], a[i + 1]));
    }
  }
  auto fritr = talkp.begin();
  auto bkitr = talkp.begin();

  for (int i = 0; i <= n; ++i) {
    place[i] = a[i];
    while (fritr != talkp.end() && (*fritr) < a[i]) {
      bkitr = fritr;
      ++fritr;
    }
    if (d[i]) {
      ll dist = (*fritr) - a[i];
      if (dist < t)
        place[i] += dist;
      else
        place[i] += t;
    } else {
      ll dist = a[i] - (*bkitr);
      if (dist < t)
        place[i] -= dist;
      else
        place[i] -= t;
    }
  }
  for (int j = 0; j < q; ++j) {
    printf("%lld\n", place[x[j]]);
  }
  return 0;
}
Problem p00545 (C++)

Buggy submission (status: Runtime Error):
//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
// 1. dp?????????
ll dp[100001];
// 2. n?????????
ll N, T, Q;
ll A[10001];
int D[10001];

// 7. calc?????????
ll calc(int k) {
  // 8. dp???????????°??????
  if (dp[k] != A[k])
    return dp[k];
  // 9. dp????¨????

  if (D[k] == 1) {
    if (D[k + 1] == 1) {
      dp[k] = min(A[k] + T, calc(k + 1));
    } else {
      dp[k] = min((A[k] + A[k + 1]) / 2, A[k] + T);
    }
  }
  if (D[k] == 2) {
    if (D[k - 1] == 2) {
      dp[k] = max(A[k] - T, calc(k - 1));
    } else {
      dp[k] = max((A[k] + A[k - 1]) / 2, A[k] - T);
    }
  }

  return dp[k];
}
int main() {
  cin >> N >> T >> Q;

  // 3.?????????

  // 4.??\???
  for (int i = 1; i < N + 1; i++) {
    cin >> A[i] >> D[i];
    dp[i] = A[i];
  }

  // 5.???????????¨???

  if (D[1] == 2) {
    dp[1] -= T;
  }

  if (D[N] == 1) {
    dp[N] += T;
  }

  // 6.??????
  int X;
  for (int i = 1; i < Q + 1; i++) {
    cin >> X;
    cout << calc(X) << endl;
  }

  return 0;
}

Fixed submission:
//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
// 1. dp?????????
ll dp[100001];
// 2. n?????????
ll N, T, Q;
ll A[100001];
int D[100001];

// 7. calc?????????
ll calc(int k) {
  // 8. dp???????????°??????
  if (dp[k] != A[k])
    return dp[k];
  // 9. dp????¨????

  if (D[k] == 1) {
    if (D[k + 1] == 1) {
      dp[k] = min(A[k] + T, calc(k + 1));
    } else {
      dp[k] = min((A[k] + A[k + 1]) / 2, A[k] + T);
    }
  }
  if (D[k] == 2) {
    if (D[k - 1] == 2) {
      dp[k] = max(A[k] - T, calc(k - 1));
    } else {
      dp[k] = max((A[k] + A[k - 1]) / 2, A[k] - T);
    }
  }

  return dp[k];
}
int main() {
  cin >> N >> T >> Q;

  // 3.?????????

  // 4.??\???
  for (int i = 1; i < N + 1; i++) {
    cin >> A[i] >> D[i];
    dp[i] = A[i];
  }

  // 5.???????????¨???

  if (D[1] == 2) {
    dp[1] -= T;
  }

  if (D[N] == 1) {
    dp[N] += T;
  }

  // 6.??????
  int X;
  for (int i = 1; i < Q + 1; i++) {
    cin >> X;
    cout << calc(X) << endl;
  }

  return 0;
}
Problem p00545 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>

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

using namespace std;

typedef long long ll;

int n;
ll t;
int q;
ll a[100000];
int d[100000];
int x[1000];

int main() {
  scanf("%d%lld%d", &n, &t, &q);
  rep(i, n) { scanf("%lld%d", &a[i], &d[i]); }
  rep(i, q) { scanf("%d", &x[i]); }

  int i = 0;
  while (i < n && d[i] == 2) {
    a[i] -= t;
  }
  while (i < n) {
    int j = 0;
    while (i + j < n && d[i + j] == 1) {
      ++j;
    }
    int k = 0;
    while (i + j + k < n && d[i + j + k] == 2) {
      ++k;
    }
    if (k == 0) {
      rep(z, j) { a[i + z] += t; }
    } else {
      ll p = (a[i + j - 1] + a[i + j]) / 2;
      rep(z, j) { a[i + z] = min(a[i + z] + t, p); }
      rep(z, k) { a[i + j + z] = max(a[i + j + z] - t, p); }
    }
    i += j + k;
  }

  rep(i, q) { printf("%lld\n", a[x[i] - 1]); }
  return 0;
}

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

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

using namespace std;

typedef long long ll;

int n;
ll t;
int q;
ll a[100000];
int d[100000];
int x[1000];

int main() {
  scanf("%d%lld%d", &n, &t, &q);
  rep(i, n) { scanf("%lld%d", &a[i], &d[i]); }
  rep(i, q) { scanf("%d", &x[i]); }

  int i = 0;
  while (i < n && d[i] == 2) {
    a[i++] -= t;
  }
  while (i < n) {
    int j = 0;
    while (i + j < n && d[i + j] == 1) {
      ++j;
    }
    int k = 0;
    while (i + j + k < n && d[i + j + k] == 2) {
      ++k;
    }
    if (k == 0) {
      rep(z, j) { a[i + z] += t; }
    } else {
      ll p = (a[i + j - 1] + a[i + j]) / 2;
      rep(z, j) { a[i + z] = min(a[i + z] + t, p); }
      rep(z, k) { a[i + j + z] = max(a[i + j + z] - t, p); }
    }
    i += j + k;
  }

  rep(i, q) { printf("%lld\n", a[x[i] - 1]); }
  return 0;
}
Problem p00545 (C++)

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

using LL = long long;

int main() {
  int N, T, Q;
  cin >> N >> T >> Q;

  vector<LL> A(N), D(N);
  set<LL> s[2];
  for (auto i = 0; i < N; ++i) {
    cin >> A[i] >> D[i];
    s[--D[i]].emplace(A[i]);
  }
  s[0].emplace(-2e18);
  s[1].emplace(2e18);

  for (auto q = 0; q < Q; ++q) {
    int X;
    cin >> X;
    if (D[--X]) {
      auto x = *--s[0].upper_bound(A[X]);
      auto y = *s[1].upper_bound(x);
      cout << max(A[X] - T, x + (y - x) / 2) << endl;
    } else {
      auto x = *s[1].upper_bound(A[X]);
      auto y = *--s[0].upper_bound(x);
      cout << min(A[X] + T, x - (x - y) / 2) << endl;
    }
  }
}

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

using LL = long long;

int main() {
  LL N, T, Q;
  cin >> N >> T >> Q;

  vector<LL> A(N), D(N);
  set<LL> s[2];
  for (auto i = 0; i < N; ++i) {
    cin >> A[i] >> D[i];
    s[--D[i]].emplace(A[i]);
  }
  s[0].emplace(-2e18);
  s[1].emplace(2e18);

  for (auto q = 0; q < Q; ++q) {
    int X;
    cin >> X;
    if (D[--X]) {
      auto x = *--s[0].upper_bound(A[X]);
      auto y = *s[1].upper_bound(x);
      cout << max(A[X] - T, x + (y - x) / 2) << endl;
    } else {
      auto x = *s[1].upper_bound(A[X]);
      auto y = *--s[0].upper_bound(x);
      cout << min(A[X] + T, x - (x - y) / 2) << endl;
    }
  }
}
Problem p00545 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int main() {
  int N, Q, flag = 0;
  long long int T, A[10000], D[10000], X[10000];
  scanf("%d %lld %d", &N, &T, &Q);
  for (int i = 1; i <= N; i++) {
    scanf("%lld %lld", &A[i], &D[i]);
  }
  for (int i = 1; i <= Q; i++) {
    scanf("%lld", &X[i]);
  }
  for (int i = 1; i <= Q; i++) {
    if (D[X[i]] == 1) {
      for (int j = X[i] + 1; j <= N; j++) {
        if (D[j] == 2) {
          if ((A[j - 1] + A[j]) / 2 <= A[X[i]] + T) {
            printf("%lld\n", (A[j - 1] + A[j]) / 2);
            flag = 1;
          }
          break;
        }
      }
      if (flag == 0) {
        printf("%lld\n", A[X[i]] + T);
      }
    }
    if (D[X[i]] == 2) {
      for (int j = X[i] - 1; j >= 1; j--) {
        if (D[j] == 1) {
          if ((A[j + 1] + A[j]) / 2 >= A[X[i]] - T) {
            printf("%lld\n", (A[j + 1] + A[j]) / 2);
            flag = 1;
          }
          break;
        }
      }
      if (flag == 0) {
        printf("%lld\n", A[X[i]] - T);
      }
    }
    flag = 0;
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

int main() {
  int N, Q, flag = 0;
  long long int T, A[100000], D[100000], X[100000];
  scanf("%d %lld %d", &N, &T, &Q);
  for (int i = 1; i <= N; i++) {
    scanf("%lld %lld", &A[i], &D[i]);
  }
  for (int i = 1; i <= Q; i++) {
    scanf("%lld", &X[i]);
  }
  for (int i = 1; i <= Q; i++) {
    if (D[X[i]] == 1) {
      for (int j = X[i] + 1; j <= N; j++) {
        if (D[j] == 2) {
          if ((A[j - 1] + A[j]) / 2 <= A[X[i]] + T) {
            printf("%lld\n", (A[j - 1] + A[j]) / 2);
            flag = 1;
          }
          break;
        }
      }
      if (flag == 0) {
        printf("%lld\n", A[X[i]] + T);
      }
    }
    if (D[X[i]] == 2) {
      for (int j = X[i] - 1; j >= 1; j--) {
        if (D[j] == 1) {
          if ((A[j + 1] + A[j]) / 2 >= A[X[i]] - T) {
            printf("%lld\n", (A[j + 1] + A[j]) / 2);
            flag = 1;
          }
          break;
        }
      }
      if (flag == 0) {
        printf("%lld\n", A[X[i]] - T);
      }
    }
    flag = 0;
  }
  return 0;
}
Problem p00546 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1LL << 60;

int main() {
  ll n, m, k, t, b[2];
  cin >> n >> m >> k >> t >> b[0] >> b[1];
  int c[n];
  ll d[n];
  for (int i = 0; i < n; i++)
    c[i] = 1 << 29, d[i] = INF;
  queue<int> que2;
  for (int i = 0; i < k; i++) {
    int x;
    scanf("%d", &x);
    x--;
    c[x] = 0;
    que2.push(x);
  }
  vector<int> v[n];
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--, y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  while (!que2.empty()) {
    int x = que2.front();
    que2.pop();
    for (int i = 0; i < v[x].size(); i++) {
      int y = v[x][i];
      if (c[y] <= c[x] + 1)
        continue;
      c[y] = c[x] + 1;
      que2.push(y);
    }
  }
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  d[0] = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll x = p.second, cost = p.second;
    if (d[x] < cost)
      continue;
    for (int i = 0; i < v[x].size(); i++) {
      int y = v[x][i];
      ll cc = d[x] + (c[y] > t ? b[0] : b[1]);
      if (!c[y])
        continue;
      if (d[y] >= cc) {
        d[y] = cc;
        que.push(P(cc, y));
      }
    }
  }
  cout << d[n - 1] - (c[n - 1] > t ? b[0] : b[1]) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1LL << 60;

int main() {
  ll n, m, k, t, b[2];
  cin >> n >> m >> k >> t >> b[0] >> b[1];
  int c[n];
  ll d[n];
  for (int i = 0; i < n; i++)
    c[i] = 1 << 29, d[i] = INF;
  queue<int> que2;
  for (int i = 0; i < k; i++) {
    int x;
    scanf("%d", &x);
    x--;
    c[x] = 0;
    que2.push(x);
  }
  vector<int> v[n];
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--, y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  while (!que2.empty()) {
    int x = que2.front();
    que2.pop();
    for (int i = 0; i < v[x].size(); i++) {
      int y = v[x][i];
      if (c[y] <= c[x] + 1)
        continue;
      c[y] = c[x] + 1;
      que2.push(y);
    }
  }
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  d[0] = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll x = p.second, cost = p.second;
    if (d[x] < cost)
      continue;
    for (int i = 0; i < v[x].size(); i++) {
      int y = v[x][i];
      ll cc = d[x] + (c[y] > t ? b[0] : b[1]);
      if (!c[y])
        continue;
      if (d[y] > cc) {
        d[y] = cc;
        que.push(P(cc, y));
      }
    }
  }
  cout << d[n - 1] - (c[n - 1] > t ? b[0] : b[1]) << endl;
  return 0;
}