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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j;
  int a[100002], m, n;
  long long b[100002], mx = -1;
  a[0] = 0;
  a[100001] = 0;
  cin >> n >> m;
  for (i = 1; i < n + 1; i++)
    cin >> a[i];
  for (i = 1; i < n + 1; i = j) {
    for (; i < n + 1 && (a[i - 1] > a[i] || a[i + 1] > a[i]); i++)
      ;
    if (i != n + 1) {
      b[i] = m - a[i];
      for (j = i - 1; j > 0; j--) {
        if (a[j - 1] > a[j]) {
          b[j] = m - a[j] + (b[j - 1] > b[j + 1] ? b[j - 1] : b[j + 1]);
          break;
        } else
          b[j] = m - a[j] + b[j + 1];
      }
      for (j = i + 1; j < n + 1 && a[j - 1] > a[j]; j++)
        b[j] = m - a[j] + b[j - 1];
    }
  }
  for (i = 1; i < n + 1; i++) {
    if (mx < b[i])
      mx = b[i];
  }
  cout << mx << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j;
  int a[100002], m, n;
  long long b[100002], mx = -1;
  a[0] = 0;
  a[100001] = 0;
  cin >> n >> m;
  for (i = 1; i < n + 1; i++)
    cin >> a[i];
  for (i = 1; i < n + 1; i = j) {
    for (; i < n + 1 && (a[i - 1] > a[i] || a[i + 1] > a[i]); i++)
      ;
    if (i != n + 1) {
      b[i] = m - a[i];
      for (j = i - 1; j > 0; j--) {
        if (a[j - 1] > a[j]) {
          b[j] = m - a[j] + (b[j - 1] > b[j + 1] ? b[j - 1] : b[j + 1]);
          break;
        } else
          b[j] = m - a[j] + b[j + 1];
      }
      for (j = i + 1; j < n + 1 && a[j - 1] > a[j]; j++)
        b[j] = m - a[j] + b[j - 1];
    } else
      j = i;
  }
  for (i = 1; i < n + 1; i++) {
    if (mx < b[i])
      mx = b[i];
  }
  cout << mx << endl;
  return 0;
}
Problem p00474 (C++)

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

int turara[100000];
int dp[100000];
int N, L;
int rec(int idx) {
  if (~dp[idx])
    return dp[idx];
  int ret = 0;
  if (idx > 0 && turara[idx - 1] > turara[idx])
    ret = max(ret, rec(idx - 1));
  if (idx < N - 1 && turara[idx + 1] > turara[idx])
    ret = max(ret, rec(idx + 1));
  return ret + (L - turara[idx]);
}

int main() {
  fill_n(dp, 100000, -1);
  scanf("%d %d", &N, &L);
  for (int i = 0; i < N; i++) {
    scanf("%d", &turara[i]);
  }
  int ret = 0;
  for (int i = 0; i < N; i++) {
    ret = max(ret, rec(i));
  }
  printf("%d\n", ret);
}

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

int turara[100000];
int dp[100000];
int N, L;
int rec(int idx) {
  if (~dp[idx])
    return dp[idx];
  int ret = 0;
  if (idx > 0 && turara[idx - 1] > turara[idx])
    ret = max(ret, rec(idx - 1));
  if (idx < N - 1 && turara[idx + 1] > turara[idx])
    ret = max(ret, rec(idx + 1));
  return dp[idx] = ret + (L - turara[idx]);
}

int main() {
  fill_n(dp, 100000, -1);
  scanf("%d %d", &N, &L);
  for (int i = 0; i < N; i++) {
    scanf("%d", &turara[i]);
  }
  int ret = 0;
  for (int i = 0; i < N; i++) {
    ret = max(ret, rec(i));
  }
  printf("%d\n", ret);
}
Problem p00474 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100005;
const int MAX_L = 500001;

int a[MAX_N];
int k[MAX_N];
multimap<int, int> mp;
ll t[MAX_N];
int n, L;

int main() {
  scanf("%d%d", &n, &L);
  rep(i, n) {
    scanf("%d", &a[i]);
    k[i] = a[i];
    mp.emplace(a[i], i);
  }
  sort(a, a + n, greater<int>());
  int id = 0;
  while (id < n) {
    auto p = mp.equal_range(a[id]);
    auto it = p.first;
    int i = 0;
    while (i < mp.count(a[id])) {
      int prev = it->second;
      if (prev != 0 && prev != n - 1) {
        if (k[prev] > k[prev - 1] && k[prev] > k[prev + 1]) {
          t[prev] = L - a[id];
        } else if (k[prev] < k[prev - 1] && k[prev] > k[prev + 1]) {
          t[prev] = t[prev - 1] + L - a[id];
        } else if (k[prev] > k[prev - 1] && k[prev] < k[prev + 1]) {
          t[prev] = t[prev + 1] + L - a[id];
        } else {
          t[prev] = max(t[prev - 1], t[prev + 1]) + L - a[id];
        }
      } else if (prev == 0) {
        if (k[0] > k[1]) {
          t[0] = L - a[id];
        } else {
          t[0] = t[1] + L - a[id];
        }
      } else {
        if (k[n - 1] > k[n - 2]) {
          t[n - 1] = L - a[id];
        } else {
          t[n - 1] = t[n - 2] + L - a[id];
        }
      }
      it++;
      i++;
    }
    id += i;
  }
  printf("%lld\n", *max_element(t, t + n));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100005;
const int MAX_L = 500001;

int a[MAX_N];
int k[MAX_N];
multimap<int, int> mp;
ll t[MAX_N];
int n, L;

int main() {
  scanf("%d%d", &n, &L);
  rep(i, n) {
    scanf("%d", &a[i]);
    k[i] = a[i];
    mp.emplace(a[i], i);
  }
  sort(a, a + n, greater<int>());
  int id = 0;
  while (id < n) {
    auto p = mp.equal_range(a[id]);
    auto it = p.first;
    int i = 0;
    while (it != p.second) {
      int prev = it->second;
      if (prev != 0 && prev != n - 1) {
        if (k[prev] > k[prev - 1] && k[prev] > k[prev + 1]) {
          t[prev] = L - a[id];
        } else if (k[prev] < k[prev - 1] && k[prev] > k[prev + 1]) {
          t[prev] = t[prev - 1] + L - a[id];
        } else if (k[prev] > k[prev - 1] && k[prev] < k[prev + 1]) {
          t[prev] = t[prev + 1] + L - a[id];
        } else {
          t[prev] = max(t[prev - 1], t[prev + 1]) + L - a[id];
        }
      } else if (prev == 0) {
        if (k[0] > k[1]) {
          t[0] = L - a[id];
        } else {
          t[0] = t[1] + L - a[id];
        }
      } else {
        if (k[n - 1] > k[n - 2]) {
          t[n - 1] = L - a[id];
        } else {
          t[n - 1] = t[n - 2] + L - a[id];
        }
      }
      it++;
      i++;
    }
    id += i;
  }
  printf("%lld\n", *max_element(t, t + n));
  return 0;
}
Problem p00474 (C++)

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

using namespace std;

#define PII pair<int, int>

priority_queue<PII, vector<PII>, greater<PII>> Q;

int main(void) {
  int N, L;
  while (scanf("%d%d", &N, &L) && N != 0) {
    int a[100005] = {0};
    int temp;
    int ans = 0;

    for (int i = 1; i <= N; i++) {
      scanf("%d", &temp);
      Q.push(PII(temp, i));
    }

    while (!Q.empty()) {
      int leng = Q.top().first;
      int soej = Q.top().second;

      Q.pop();
      int time = a[soej] + L - leng;

      a[soej - 1] = max(time, a[soej - 1]);
      a[soej + 1] = max(time, a[soej + 1]);
    }

    for (int i = 1; i <= N; i++)
      ans = max(a[i], ans);

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

#define PII pair<int, int>

priority_queue<PII, vector<PII>, greater<PII>> Q;

int main(void) {
  int N, L;
  while (scanf("%d%d", &N, &L) != EOF) {
    int a[100005] = {0};
    int temp;
    int ans = 0;

    for (int i = 1; i <= N; i++) {
      scanf("%d", &temp);
      Q.push(PII(temp, i));
    }

    while (!Q.empty()) {
      int leng = Q.top().first;
      int soej = Q.top().second;

      Q.pop();
      int time = a[soej] + L - leng;

      a[soej - 1] = max(time, a[soej - 1]);
      a[soej + 1] = max(time, a[soej + 1]);
    }

    for (int i = 1; i <= N; i++)
      ans = max(a[i], ans);

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t[510];
int main() {
  int n, l;
  cin >> n >> l;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  t[0] = l - a[0];
  for (int i = 1; i < n; i++) {
    if (a[i - 1] > a[i])
      t[i] = t[i - 1] + (l - a[i]);
    else
      t[i] = l - a[i];
  }
  for (int i = n - 2; i >= 0; i--) {
    if (a[i] < a[i + 1])
      t[i] = max(t[i], t[i + 1] + (l - a[i]));
  }
  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, t[i]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t[100010];
int main() {
  int n, l;
  cin >> n >> l;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  t[0] = l - a[0];
  for (int i = 1; i < n; i++) {
    if (a[i - 1] > a[i])
      t[i] = t[i - 1] + (l - a[i]);
    else
      t[i] = l - a[i];
  }
  for (int i = n - 2; i >= 0; i--) {
    if (a[i] < a[i + 1])
      t[i] = max(t[i], t[i + 1] + (l - a[i]));
  }
  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, t[i]);
  cout << ans << endl;
  return 0;
}
Problem p00474 (C++)

Buggy submission (status: Runtime Error):
// 21
#include <algorithm>
#include <iostream>

using namespace std;

int n, l;
int c[10000];
int t[10000];
int a = 0;

int gr(int i) {
  if (t[i])
    return t[i];
  int li = (i == 0 || c[i - 1] < c[i]) ? 0 : gr(i - 1);
  int ri = (i == n - 1 || c[i + 1] < c[i]) ? 0 : gr(i + 1);
  t[i] = max(li, ri) + l - c[i];
  a = max(a, t[i]);
  return t[i];
}

int main() {
  cin >> n >> l;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < n; i++) {
    gr(i);
  }
  cout << a << endl;
}

Fixed submission:
// 21
#include <algorithm>
#include <iostream>

using namespace std;

int n, l;
int c[100000];
int t[100000];
int a = 0;

int gr(int i) {
  if (t[i])
    return t[i];
  int li = (i == 0 || c[i - 1] < c[i]) ? 0 : gr(i - 1);
  int ri = (i == n - 1 || c[i + 1] < c[i]) ? 0 : gr(i + 1);
  t[i] = max(li, ri) + l - c[i];
  a = max(a, t[i]);
  return t[i];
}

int main() {
  cin >> n >> l;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < n; i++) {
    gr(i);
  }
  cout << a << endl;
}
Problem p00474 (C++)

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

using namespace std;

#define MAX 100000
typedef pair<int, int> P;

int main() {
  int N, L, in, dp[MAX] = {0};
  priority_queue<P> Q;

  cin >> N >> L;
  for (int i = 1; i <= N; i++) {
    cin >> in;
    Q.push(P(in, i));
  }
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    dp[p.second] = max(dp[p.second - 1], dp[p.second + 1]) + (L - p.first);
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;

  return 0;
}

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

using namespace std;

#define MAX 100002
typedef pair<int, int> P;

int main() {
  int N, L, in, dp[MAX] = {0};
  priority_queue<P> Q;

  cin >> N >> L;
  for (int i = 1; i <= N; i++) {
    cin >> in;
    Q.push(P(in, i));
  }
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    dp[p.second] = max(dp[p.second - 1], dp[p.second + 1]) + (L - p.first);
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;

  return 0;
}
Problem p00474 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int ic[50002], icL[50002];
int maxer = 0;
int N, L;
void pick(int i) {
  if (icL[i - 1] < icL[i] && icL[i + 1] < icL[i]) {
    ic[i] = max(ic[i - 1], ic[i + 1]) + L - icL[i];
    maxer = max(ic[i], maxer);
    icL[i] = 0;
    pick(i + 1);
    pick(i - 1);
  }
}
int main() {

  cin >> N >> L;
  icL[0] = 0;
  icL[N + 1] = 0;
  for (int i = 1; i <= N; i++)
    cin >> icL[i];
  for (int i = 1; i <= N; i++) {
    pick(i);
  }
  cout << maxer << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int ic[100002], icL[100002];
int maxer = 0;
int N, L;
void pick(int i) {
  if (icL[i - 1] < icL[i] && icL[i + 1] < icL[i]) {
    ic[i] = max(ic[i - 1], ic[i + 1]) + L - icL[i];
    maxer = max(ic[i], maxer);
    icL[i] = 0;
    pick(i + 1);
    pick(i - 1);
  }
}
int main() {

  cin >> N >> L;
  icL[0] = 0;
  icL[N + 1] = 0;
  for (int i = 1; i <= N; i++)
    cin >> icL[i];
  for (int i = 1; i <= N; i++) {
    pick(i);
  }
  cout << maxer << endl;
  return 0;
}
Problem p00478 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;

int main() {
  string A, S, T;
  int P = 0;
  int n;
  cin >> A;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S;
    T = S + S;
    for (int j = 0; j < n; j++) {
      if (T.substr(j, A.length()) == A) {
        P++;
        break;
      }
    }
  }
  cout << P << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;

int main() {
  string A, S, T;
  int P = 0;
  int n;
  cin >> A;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S;
    T = S + S;
    for (int j = 0; j < 10; j++) {
      if (T.substr(j, A.length()) == A) {
        P++;
        break;
      }
    }
  }
  cout << P << endl;
  return 0;
}
Problem p00479 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#define lli long long int

using namespace std;

int main() {
  lli n, m, s, t, u, v;

  while (cin >> n && n) {
    cin >> m;
    if (n % 2 == 0) {
      v = n / 2;
    } else {
      v = (n + 1) / 2;
    }

    while (m--) {
      cin >> s >> t;
      if (s > v) {
        s = n - s + 1;
      }
      if (t > v) {
        t = n - t + 1;
      }

      if (s < t) {
        u = s;
      } else {
        u = t;
      }

      for (lli i = 1; i <= v; i += 3) {
        if (i == u) {
          cout << 1 << endl;
          break;
        } else if (i + 1 == u) {
          cout << 2 << endl;
          break;
        } else if (i + 2 == u) {
          cout << 3 << endl;
          break;
        }
      }
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>
#define lli long long int

using namespace std;

int main() {
  lli n, m, s, t, u, v;

  while (cin >> n && n) {
    cin >> m;
    if (n % 2 == 0) {
      v = n / 2;
    } else {
      v = (n + 1) / 2;
    }

    while (m--) {
      cin >> s >> t;
      if (s > v) {
        s = n - s + 1;
      }
      if (t > v) {
        t = n - t + 1;
      }

      if (s < t) {
        u = s;
      } else {
        u = t;
      }
      u = (u - 1) % 3;
      cout << (u + 1) << endl;
    }
  }

  return 0;
}
Problem p00480 (C++)

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

#define rep(a, b, c) for (int a = b; a < (c); a++)
#define repe(a, b, c) for (int a = b; a <= (c); a++)

using namespace std;

int main() {
  int N;
  cin >> N;

  vector<vector<long long unsigned>> dp(N, vector<long long unsigned>(21, 0));
  int num;
  cin >> num;
  dp[0][num] = 1;
  rep(i, 0, N - 1) {
    cin >> num;
    repe(j, 0, 20) {
      if (j + num <= 20)
        dp[i][j + num] += dp[i - 1][j];
      if (j - num >= 0)
        dp[i][j - num] += dp[i - 1][j];
    }
  }
  cin >> num;
  cout << dp[N - 2][num] << endl;

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

#define rep(a, b, c) for (int a = b; a < (c); a++)
#define repe(a, b, c) for (int a = b; a <= (c); a++)

using namespace std;

int main() {
  int N;
  cin >> N;

  vector<vector<long long unsigned>> dp(N, vector<long long unsigned>(21, 0));
  int num;
  cin >> num;
  dp[0][num] = 1;
  rep(i, 1, N) {
    cin >> num;
    repe(j, 0, 20) {
      if (j + num <= 20)
        dp[i][j + num] += dp[i - 1][j];
      if (j - num >= 0)
        dp[i][j - num] += dp[i - 1][j];
    }
  }
  cin >> num;
  cout << dp[N - 2][num] << endl;

  return 0;
}
Problem p00480 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
int main() {
  int a;
  int dp[21][101] = {0};
  int x[101];
  std::cin >> a;
  for (int i = 1; i <= a; i++)
    std::cin >> x[i];
  dp[1][x[1]] = 1;

  for (int j = 2; j < a; j++) {
    for (int s = 0; s <= 20; s++) {
      if (s + x[j] <= 20 && s - x[j] >= 0) {
        dp[j][s] = dp[j - 1][s - x[j]] + dp[j - 1][s + x[j]];
      } else if (s + x[j] <= 20 && s - x[j] < 0) {
        dp[j][s] = dp[j - 1][s + x[j]];
      } else if (s + x[j] > 20 && s - x[j] >= 0) {
        dp[j][s] = dp[j - 1][s - x[j]];
      }
    }
  }
  std::cout << dp[a - 1][x[a]] << std::endl;

  return 0;
}

Fixed submission:
#include <iostream>
int main() {
  int a;
  long long int dp[101][101] = {0};
  int x[101];
  std::cin >> a;
  for (int i = 1; i <= a; i++)
    std::cin >> x[i];
  dp[1][x[1]] = 1;

  for (int j = 2; j < a; j++) {
    for (int s = 0; s <= 20; s++) {
      if (s + x[j] <= 20 && s - x[j] >= 0) {
        dp[j][s] = dp[j - 1][s - x[j]] + dp[j - 1][s + x[j]];
      } else if (s + x[j] <= 20 && s - x[j] < 0) {
        dp[j][s] = dp[j - 1][s + x[j]];
      } else if (s + x[j] > 20 && s - x[j] >= 0) {
        dp[j][s] = dp[j - 1][s - x[j]];
      }
    }
  }
  std::cout << dp[a - 1][x[a]] << std::endl;

  return 0;
}
Problem p00480 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n;
int d[300];

int main() {
  while (cin >> n) {
    rep(i, n) { cin >> d[i]; }
    int p = 0;
    unsigned long long int dp[100][22] = {0}; // dp[i][j]
    dp[0][d[0]] = 1;
    for (int i = 1; i < n - 1; i++) {
      for (int j = 0; j <= 20; j++) {
        if (dp[i - 1][j] > 0) {
          if (j + d[i] <= 20) {
            dp[i][j + d[i]] += dp[i - 1][j];
          }
          if (j - d[i] >= 0)
            dp[i][j - d[i]] += dp[i - 1][j];
        }
      }
    }
    int sum = 0;
    for (int i = 0; i <= 100000000000; i++) {
      sum += 1;
    }
    cout << dp[n - 2][d[n - 1]] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n;
int d[300];

int main() {
  while (cin >> n) {
    rep(i, n) { cin >> d[i]; }
    int p = 0;
    unsigned long long int dp[100][22] = {0}; // dp[i][j]
    dp[0][d[0]] = 1;
    for (int i = 1; i < n - 1; i++) {
      for (int j = 0; j <= 20; j++) {
        if (dp[i - 1][j] > 0) {
          if (j + d[i] <= 20) {
            dp[i][j + d[i]] += dp[i - 1][j];
          }
          if (j - d[i] >= 0)
            dp[i][j - d[i]] += dp[i - 1][j];
        }
      }
    }
    int sum = 0;
    for (int i = 0; i <= 1000000000; i++) {
      sum += 1;
    }
    cout << dp[n - 2][d[n - 1]] << endl;
  }
  return 0;
}
Problem p00481 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
#define INF 100000
char map[1001][1001];
int h, w, n;
int d1[4] = {0, 1, 0, -1};
int d2[4] = {1, 0, -1, 0};
int i, j, x[10], y[10];
int bfs(int num) {
  int gy = y[num + 1], gx = x[num + 1];
  int d[1001][1001];
  for (int i = 0; i <= 1001; i++) {
    for (int j = 0; j <= 1001; j++) {
      d[i][j] = INF;
    }
  }
  queue<int> qx;
  queue<int> qy;
  qx.push(x[num]);
  qy.push(y[num]);
  d[y[num]][x[num]] = 0;
  while (qx.size() + qy.size() != 0) {
    int ny, nx;
    ny = qy.front();
    qy.pop();
    nx = qx.front();
    qx.pop();
    if (ny == gy && nx == gx) {
      break;
    }
    for (int i = 0; i < 4; i++) {
      int mx = nx + d1[i], my = ny + d2[i];
      if (0 <= mx && mx < w && 0 <= my && my < h && map[my][mx] != 'X' &&
          d[my][mx] == INF) {
        d[my][mx] = d[ny][nx] + 1;
        qx.push(mx);
        qy.push(my);
      }
    }
  }
  return d[gy][gx];
}
int main() {
  int ans = 0;
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> map[i][j];
      if (map[i][j] == 'S') {
        y[0] = i;
        x[0] = j;
      } else if ('0' < map[i][j] && map[i][j] <= '9') {
        y[(int)(map[i][j] - '0')] = i;
        x[(int)(map[i][j] - '0')] = j;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    ans += bfs(i);
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
#define INF 100000
char map[1001][1001];
int h, w, n;
int d1[4] = {0, 1, 0, -1};
int d2[4] = {1, 0, -1, 0};
int i, j, x[10], y[10];
int bfs(int num) {
  int gy = y[num + 1], gx = x[num + 1];
  int d[1001][1001];
  for (int i = 0; i < 1001; i++) {
    for (int j = 0; j < 1001; j++) {
      d[i][j] = INF;
    }
  }
  queue<int> qx;
  queue<int> qy;
  qx.push(x[num]);
  qy.push(y[num]);
  d[y[num]][x[num]] = 0;
  while (qx.size() + qy.size() != 0) {
    int ny, nx;
    ny = qy.front();
    qy.pop();
    nx = qx.front();
    qx.pop();
    if (ny == gy && nx == gx) {
      break;
    }
    for (int i = 0; i < 4; i++) {
      int mx = nx + d1[i], my = ny + d2[i];
      if (0 <= mx && mx < w && 0 <= my && my < h && map[my][mx] != 'X' &&
          d[my][mx] == INF) {
        d[my][mx] = d[ny][nx] + 1;
        qx.push(mx);
        qy.push(my);
      }
    }
  }
  return d[gy][gx];
}
int main() {
  int ans = 0;
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> map[i][j];
      if (map[i][j] == 'S') {
        y[0] = i;
        x[0] = j;
      } else if ('0' < map[i][j] && map[i][j] <= '9') {
        y[(int)(map[i][j] - '0')] = i;
        x[(int)(map[i][j] - '0')] = j;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    ans += bfs(i);
  }

  cout << ans << endl;
  return 0;
}
Problem p00481 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#define min(x, y) ((x) < (y) ? (x) : (y))
#define INFI 65535

using namespace std;
typedef pair<int, int> P;

int Strength = 1;
char Maps[1010][1010];
int dist[1010][1010];
int H, W, N;
int hor[] = {1, 0, -1, 0};
int ver[] = {0, -1, 0, 1};
int ans;

void clear();
void show();
P bfs(int cheese, P n);

int main(void) {
  cin >> H >> W >> N;
  P s, e;
  for (int i = 0; i < H; i++)
    cin >> Maps[i];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (Maps[i][j] == 'S')
        s.first = i, s.second = j;

  for (int i = 1; i <= N; i++) {
    clear();
    s = bfs(i, s);
  }

  cout << ans << endl;
  return 0;
}

P bfs(int cheese, P n) {
  queue<P> que;
  que.push(n);
  dist[n.first][n.second] = 0;

  P top;
  while (que.size()) {
    top = que.front();
    if (Maps[top.first][top.second] - '0' == cheese) {
      ans += dist[top.first][top.second];
      break;
    }

    que.pop();
    for (int i = 0; i < 4; i++) {
      int nv = top.first + ver[i], nh = top.second + hor[i];
      if (nv >= 0 && nv < H && nh >= 0 && nh < W && dist[nv][nh] == INFI &&
          Maps[nv][nh] != 'X') {
        que.push(P(nv, nh));
        dist[nv][nh] = min(dist[nv][nh], dist[top.first][top.second] + 1);
      }
    }
  }
  return top;
}

void clear() {
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      dist[i][j] = INFI;
}

Fixed submission:
#include <iostream>
#include <queue>
#define min(x, y) ((x) < (y) ? (x) : (y))
#define INFI 100000000

using namespace std;
typedef pair<int, int> P;

int Strength = 1;
char Maps[1010][1010];
int dist[1010][1010];
int H, W, N;
int hor[] = {1, 0, -1, 0};
int ver[] = {0, -1, 0, 1};
int ans;

void clear();
void show();
P bfs(int cheese, P n);

int main(void) {
  cin >> H >> W >> N;
  P s, e;
  for (int i = 0; i < H; i++)
    cin >> Maps[i];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (Maps[i][j] == 'S')
        s.first = i, s.second = j;

  for (int i = 1; i <= N; i++) {
    clear();
    s = bfs(i, s);
  }

  cout << ans << endl;
  return 0;
}

P bfs(int cheese, P n) {
  queue<P> que;
  que.push(n);
  dist[n.first][n.second] = 0;

  P top;
  while (que.size()) {
    top = que.front();
    if (Maps[top.first][top.second] - '0' == cheese) {
      ans += dist[top.first][top.second];
      break;
    }

    que.pop();
    for (int i = 0; i < 4; i++) {
      int nv = top.first + ver[i], nh = top.second + hor[i];
      if (nv >= 0 && nv < H && nh >= 0 && nh < W && dist[nv][nh] == INFI &&
          Maps[nv][nh] != 'X') {
        que.push(P(nv, nh));
        dist[nv][nh] = min(dist[nv][nh], dist[top.first][top.second] + 1);
      }
    }
  }
  return top;
}

void clear() {
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      dist[i][j] = INFI;
}
Problem p00481 (C++)

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

typedef pair<int, int> P;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define F first
#define S second

const int M = 1024;
const int INF = 114514;
int h, w, n;
char f[M][M];
int d[M][M];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
vector<P> cheese(12);

void init() {
  rep(y, M) rep(x, M) f[y][x] = '$';
  cheese.clear();
}

bool ng(int x, int y) { return (x < 0 or w <= x or y < 0 or h <= y); }

int bfs(P s, P g) {
  int gx = g.F, gy = g.S;
  queue<P> Q;
  rep(y, M) rep(x, M) d[y][x] = INF;
  Q.push(s);
  d[s.S][s.F] = 0;

  while (Q.size()) {
    P p = Q.front();
    Q.pop();
    if (p.F == gx and p.S == gy)
      break;

    rep(i, 4) {
      int nx = p.F + dx[i], ny = p.S + dy[i];
      if (ng(nx, ny) or f[ny][nx] == 'X' or d[ny][nx] < INF)
        continue;
      d[ny][nx] = d[p.S][p.F] + 1;
      Q.push(P(nx, ny));
    }
  }

  return d[gy][gx];
}

int main() {
  while (cin >> h >> w >> n) {
    init();
    rep(y, h) rep(x, w) {
      cin >> f[y][x];
      char c = f[y][x];
      if (c != '.' and c != 'S' and c != 'X')
        cheese[c - '0'] = make_pair(x, y);
      if (c == 'S')
        cheese[0] = make_pair(x, y);
    }

    int ans = 0;
    rep(i, n) ans += bfs(cheese[i], cheese[i + 1]);
    cout << ans << endl;
  }

  return 0;
}

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

typedef pair<int, int> P;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define F first
#define S second

const int M = 1024;
const int INF = 114514810;
int h, w, n;
char f[M][M];
int d[M][M];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
vector<P> cheese(12);

void init() {
  rep(y, M) rep(x, M) f[y][x] = '$';
  cheese.clear();
}

bool ng(int x, int y) { return (x < 0 or w <= x or y < 0 or h <= y); }

int bfs(P s, P g) {
  int gx = g.F, gy = g.S;
  queue<P> Q;
  rep(y, M) rep(x, M) d[y][x] = INF;
  Q.push(s);
  d[s.S][s.F] = 0;

  while (Q.size()) {
    P p = Q.front();
    Q.pop();
    if (p.F == gx and p.S == gy)
      break;

    rep(i, 4) {
      int nx = p.F + dx[i], ny = p.S + dy[i];
      if (ng(nx, ny) or f[ny][nx] == 'X' or d[ny][nx] < INF)
        continue;
      d[ny][nx] = d[p.S][p.F] + 1;
      Q.push(P(nx, ny));
    }
  }

  return d[gy][gx];
}

int main() {
  while (cin >> h >> w >> n) {
    init();
    rep(y, h) rep(x, w) {
      cin >> f[y][x];
      char c = f[y][x];
      if (c != '.' and c != 'S' and c != 'X')
        cheese[c - '0'] = make_pair(x, y);
      if (c == 'S')
        cheese[0] = make_pair(x, y);
    }

    int ans = 0;
    rep(i, n) ans += bfs(cheese[i], cheese[i + 1]);
    cout << ans << endl;
  }

  return 0;
}
Problem p00481 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <utility>
using namespace std;

int H, W, N;
char field[1000][1000];
int d[1000][1000];
pair<int, int> start;
pair<int, int> cheese;
int INF = 1000000;
int result = 0;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void bfs() {
  for (int n = 1; n < N + 1; n++) {
    queue<pair<int, int>> que;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        d[i][j] = INF;
      }
    }
    que.push(start);
    d[start.first][start.second] = 0;

    while (que.size()) {
      pair<int, int> p = que.front();
      que.pop();
      if (field[p.first][p.second] == '0' + (char)n) {
        cheese = p;
        break;
      }

      for (int i = 0; i < 4; i++) {
        int nx = p.first + dx[i];
        int ny = p.second + dy[i];

        if (0 <= nx && nx <= H && 0 <= ny && ny <= W && field[nx][ny] != 'X' &&
            d[nx][ny] == INF) {
          que.push(pair<int, int>(nx, ny));
          d[nx][ny] = d[p.first][p.second] + 1;
        }
      }
    }
    result += d[cheese.first][cheese.second];
    start = cheese;
  }
}

int main() {
  scanf(" %d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf(" %c", &field[i][j]);
      if (field[i][j] == 'S') {
        start.first = i;
        start.second = j;
      }
    }
  }
  bfs();
  printf("%d\n", result);
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <utility>
using namespace std;

int H, W, N;
char field[1000][1000];
int d[1000][1000];
pair<int, int> start;
pair<int, int> cheese;
int INF = 1000000;
int result = 0;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void bfs() {
  for (int n = 1; n < N + 1; n++) {
    queue<pair<int, int>> que;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        d[i][j] = INF;
      }
    }
    que.push(start);
    d[start.first][start.second] = 0;

    while (que.size()) {
      pair<int, int> p = que.front();
      que.pop();
      if (field[p.first][p.second] == '0' + (char)n) {
        cheese = p;
        break;
      }

      for (int i = 0; i < 4; i++) {
        int nx = p.first + dx[i];
        int ny = p.second + dy[i];

        if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] != 'X' &&
            d[nx][ny] == INF) {
          que.push(pair<int, int>(nx, ny));
          d[nx][ny] = d[p.first][p.second] + 1;
        }
      }
    }
    result += d[cheese.first][cheese.second];
    start = cheese;
  }
}

int main() {
  scanf(" %d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf(" %c", &field[i][j]);
      if (field[i][j] == 'S') {
        start.first = i;
        start.second = j;
      }
    }
  }
  bfs();
  printf("%d\n", result);
}
Problem p00481 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;

#define INF 10000000

typedef pair<int, int> P;

queue<P> que;
char map[1000][1001];
int vst[1000][1000];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int H, W, N, sx, sy, sum = 0;

int main() {
  scanf("%d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    scanf("%s", map[i]);
    char *ptr = strchr(map[i], 'S');
    if (ptr != NULL)
      sx = ptr - map[i], sy = i;
  }
  que.push(P(sx, sy));
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < H; j++) {
      for (int k = 0; k < W; k++) {
        vst[j][k] = INF;
      }
    }
    vst[sy][sx] = 0;
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      if (map[p.second][p.first] == i + '0') {
        sum += vst[p.second][p.first];
        while (!que.empty())
          que.pop();
        que.push(P(p.first, p.second));
        sx = p.first, sy = p.second;
        break;
      }
      for (int j = 0; j < 4; j++) {
        int nx = p.first + dx[j], ny = p.second + dy[j];
        if (map[ny][nx] != 'X' && vst[ny][nx] == INF) {
          vst[ny][nx] = vst[p.second][p.first] + 1;
          que.push(P(nx, ny));
        }
      }
    }
  }
  printf("%d\n", sum);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;

#define INF 10000000

typedef pair<int, int> P;

queue<P> que;
char map[1000][1001];
int vst[1000][1000];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int H, W, N, sx, sy, sum = 0;

int main() {
  scanf("%d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    scanf("%s", map[i]);
    char *ptr = strchr(map[i], 'S');
    if (ptr != NULL)
      sx = ptr - map[i], sy = i;
  }
  que.push(P(sx, sy));
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < H; j++) {
      for (int k = 0; k < W; k++) {
        vst[j][k] = INF;
      }
    }
    vst[sy][sx] = 0;
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      if (map[p.second][p.first] == i + '0') {
        sum += vst[p.second][p.first];
        while (!que.empty())
          que.pop();
        que.push(P(p.first, p.second));
        sx = p.first, sy = p.second;
        break;
      }
      for (int j = 0; j < 4; j++) {
        int nx = p.first + dx[j], ny = p.second + dy[j];
        if (0 <= nx && nx < W && 0 <= ny && ny < H && map[ny][nx] != 'X' &&
            vst[ny][nx] == INF) {
          vst[ny][nx] = vst[p.second][p.first] + 1;
          que.push(P(nx, ny));
        }
      }
    }
  }
  printf("%d\n", sum);
  return 0;
}
Problem p00481 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;
typedef std::pair<LL, LL> P;
struct edge {
  LL to, cost;
};

int H, W, N;
std::string field[1123];
int Sx, Sy, x[11], y[11], ans;

int d[1123][1123];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void bfs(LL sx, LL sy, LL gx, LL gy) {
  std::queue<P> que;
  memset(d, -1, sizeof(d));
  que.push({sx, sy});
  d[sx][sy] = 0;
  while (que.size() > 0) {
    auto pos = que.front();
    que.pop();
    if (pos.first == gx && pos.second == gy) {
      break;
    }
    rep(i, 4) {
      auto toX = pos.first + dx[i], toY = pos.second + dy[i];
      if (0 <= toX && toX < H && 0 <= toY && toY < W) {
        if (field[toX][toY] != 'X' && d[toX][toY] == -1) {
          que.push({toX, toY});
          d[toX][toY] = d[pos.first][pos.second] + 1;
        }
      }
    }
  }
}

int main() {
  in >> H >> W >> N;
  rep(i, H) { in >> field[i]; }

  rep(i, H) {
    rep(j, W) {
      if (field[i][j] == 'S') {
        Sx = i;
        Sy = j;
      } else if (field[i][j] != '.') {
        x[field[i][j] - '0'] = i;
        y[field[i][j] - '0'] = j;
      }
    }
  }
  x[0] = Sx;
  y[0] = Sy;
  rep(i, N) {
    bfs(x[i], y[i], x[i + 1], y[i + 1]);
    ans += d[x[i + 1]][y[i + 1]];
  }
  out << ans << std::endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;
typedef std::pair<LL, LL> P;
struct edge {
  LL to, cost;
};

int H, W, N;
std::string field[1123];
int Sx, Sy, x[11], y[11], ans;

int d[1123][1123];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void bfs(LL sx, LL sy, LL gx, LL gy) {
  std::queue<P> que;
  memset(d, -1, sizeof(d));
  que.push({sx, sy});
  d[sx][sy] = 0;
  while (que.size() > 0) {
    auto pos = que.front();
    que.pop();
    if (pos.first == gx && pos.second == gy) {
      break;
    }
    rep(i, 4) {
      auto toX = pos.first + dx[i], toY = pos.second + dy[i];
      if (0 <= toX && toX < H && 0 <= toY && toY < W) {
        if (field[toX][toY] != 'X' && d[toX][toY] == -1) {
          que.push({toX, toY});
          d[toX][toY] = d[pos.first][pos.second] + 1;
        }
      }
    }
  }
}

int main() {
  in >> H >> W >> N;
  rep(i, H) { in >> field[i]; }

  rep(i, H) {
    rep(j, W) {
      if (field[i][j] == 'X') {
        continue;
      }
      if (field[i][j] == 'S') {
        Sx = i;
        Sy = j;
      } else if (field[i][j] != '.') {
        x[field[i][j] - '0'] = i;
        y[field[i][j] - '0'] = j;
      }
    }
  }
  x[0] = Sx;
  y[0] = Sy;
  rep(i, N) {
    bfs(x[i], y[i], x[i + 1], y[i + 1]);
    ans += d[x[i + 1]][y[i + 1]];
  }
  out << ans << std::endl;
  return 0;
}
Problem p00481 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <queue>
#define ROP(i, n) for (int i = 0; i < n; i++)
#define ROPP(i, n) for (int i = 1; i <= n; i++)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
int N, M, G, gx[10], gy[10];
char MAP[1000][1000 + 1];
int DX[4] = {1, 0, -1, 0}, DY[4] = {0, 1, 0, -1}, FAR[1000][1000 + 1];
int BFS(int u) {
  queue<P> que;
  ROPP(i, N) { ROPP(j, M) FAR[j][i] = INF; }
  que.push(P(gx[u], gy[u]));
  FAR[gx[u]][gy[u]] = 0;
  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.first == gx[u + 1] && p.second == gy[u + 1])
      break;
    ROP(i, 4) {
      int nx = p.first + DX[i], ny = p.second + DY[i];
      if (1 <= nx && nx <= M && 1 <= ny && ny <= N && MAP[nx][ny] != 'X' &&
          FAR[nx][ny] == INF) {
        que.push(P(nx, ny));
        FAR[nx][ny] = FAR[p.first][p.second] + 1;
      }
    }
  }
  return FAR[gx[u + 1]][gy[u + 1]];
}
void solve() {
  int res = 0;
  ROP(i, G) { res += BFS(i); }
  printf("%d\n", res);
}
int main() {
  scanf("%d %d %d", &N, &M, &G);
  ROPP(i, N) {
    ROP(j, M + 1) {
      scanf("%c", &MAP[j][i]);
      if (MAP[j][i] == 'S')
        gx[0] = j, gy[0] = i;
      ROPP(k, G) {
        char l = '0' + k;
        if (MAP[j][i] == l)
          gx[k] = j, gy[k] = i;
      }
    }
  }
  solve();
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <queue>
#define ROP(i, n) for (int i = 0; i < n; i++)
#define ROPP(i, n) for (int i = 1; i <= n; i++)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
int N, M, G, gx[10], gy[10];
char MAP[1005][1005];
int DX[4] = {1, 0, -1, 0}, DY[4] = {0, 1, 0, -1}, FAR[1005][1005];
int BFS(int u) {
  queue<P> que;
  ROPP(i, N) { ROPP(j, M) FAR[j][i] = INF; }
  que.push(P(gx[u], gy[u]));
  FAR[gx[u]][gy[u]] = 0;
  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.first == gx[u + 1] && p.second == gy[u + 1])
      break;
    ROP(i, 4) {
      int nx = p.first + DX[i], ny = p.second + DY[i];
      if (1 <= nx && nx <= M && 1 <= ny && ny <= N && MAP[nx][ny] != 'X' &&
          FAR[nx][ny] == INF) {
        que.push(P(nx, ny));
        FAR[nx][ny] = FAR[p.first][p.second] + 1;
      }
    }
  }
  return FAR[gx[u + 1]][gy[u + 1]];
}
void solve() {
  int res = 0;
  ROP(i, G) { res += BFS(i); }
  printf("%d\n", res);
}
int main() {
  scanf("%d %d %d", &N, &M, &G);
  ROPP(i, N) {
    ROP(j, M + 1) {
      scanf("%c", &MAP[j][i]);
      if (MAP[j][i] == 'S')
        gx[0] = j, gy[0] = i;
      ROPP(k, G) {
        char l = '0' + k;
        if (MAP[j][i] == l)
          gx[k] = j, gy[k] = i;
      }
    }
  }
  solve();
  return 0;
}
Problem p00481 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <queue>
#define x first
#define y second
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int h, w, k;
pair<int, int> p[12];
bool t[1010][1010], vis[1010][1010];
queue<pair<int, pair<int, int>>> Q;
int main() {
  cin >> h >> w >> k;
  rep(i, 0, h) rep(j, 0, w) {
    t[i][j] = true;
    char z;
    cin >> z;
    if (z == 'X')
      t[i][j] = 0;
    if (z == 'S')
      p[0] = make_pair(i, j);
    if (z > '0' && z <= '9')
      p[z - '0'] = make_pair(i, j);
  }
  int a = 0;
  rep(r, 0, k) {
    Q.push(make_pair(0, p[r]));
    rep(i, 0, h) rep(j, 0, w) vis[i][j] = 0;
    vis[p[r].x][p[r].y] = 1;
    while (1) {
      int L = Q.front().x, Y = Q.front().y.x, X = Q.front().y.y;
      if (Y == p[r + 1].x && X == p[r + 1].y) {
        a += L;
        break;
      }
      int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
      rep(i, 0, 4) {
        int f = Y + dy[i], g = X + dx[i];
        if (f >= 0 && f < h && g >= 0 && g < w) {
          if (t[f][g] == 1 && vis[f][g] == 0) {
            vis[Y][X] = 1;
            Q.push(make_pair(L + 1, make_pair(f, g)));
          }
        }
      }
      Q.pop();
    }
    while (!Q.empty())
      Q.pop();
  }
  cout << a << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#define x first
#define y second
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int h, w, k;
pair<int, int> p[12];
bool t[1010][1010], vis[1010][1010];
queue<pair<int, pair<int, int>>> Q;
int main() {
  cin >> h >> w >> k;
  rep(i, 0, h) rep(j, 0, w) {
    t[i][j] = true;
    char z;
    cin >> z;
    if (z == 'X')
      t[i][j] = 0;
    if (z == 'S')
      p[0] = make_pair(i, j);
    if (z > '0' && z <= '9')
      p[z - '0'] = make_pair(i, j);
  }
  int a = 0;
  rep(r, 0, k) {
    Q.push(make_pair(0, p[r]));
    rep(i, 0, h) rep(j, 0, w) vis[i][j] = 0;
    vis[p[r].x][p[r].y] = 1;
    while (1) {
      int L = Q.front().x, Y = Q.front().y.x, X = Q.front().y.y;
      if (Y == p[r + 1].x && X == p[r + 1].y) {
        a += L;
        break;
      }
      int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
      rep(i, 0, 4) {
        int f = Y + dy[i], g = X + dx[i];
        if (f >= 0 && f < h && g >= 0 && g < w) {
          if (t[f][g] == 1 && vis[f][g] == 0) {
            vis[f][g] = 1;
            Q.push(make_pair(L + 1, make_pair(f, g)));
          }
        }
      }
      Q.pop();
    }
    while (!Q.empty())
      Q.pop();
  }
  cout << a << endl;
  return 0;
}
Problem p00481 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <map>
#include <queue>

#define rep(i, a, b) for (int i = (a); (i) < (b); (i)++)
#define ft first
#define sd second

using namespace std;

char m[1000][1000];
int dis[1000][1000];
queue<pair<int, int>> state;

int main() {
  int h, w, n;

  cin >> h >> w >> n;

  int sx, sy;
  rep(i, 0, h) {
    rep(j, 0, w) {
      cin >> m[j][i];
      if (m[j][i] == 'S') {
        sx = j;
        sy = i;
      }
    }
  }

  pair<int, int> p;
  p.ft = sx;
  p.sd = sy;

  rep(i, 0, h) {
    rep(j, 0, w) { dis[j][i] = -1; }
  }
  dis[sx][sy] = 0;
  state.push(p);

  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, -1, 0, 1};
  int map2[1000][1000];
  for (int lp = 1; lp <= n; lp++) {
    rep(i, 0, h) {
      rep(j, 0, w) { map2[j][i] = 0; }
    }

    map2[sx][sy] = 1;
    while (state.size()) {
      pair<int, int> now = state.front();
      state.pop();
      if (m[now.ft][now.sd] - '0' == lp) {
        sx = now.ft;
        sy = now.sd;
        break;
      }

      rep(lp2, 0, 4) {
        int x = now.ft + dx[lp2];
        int y = now.sd + dy[lp2];

        if (x < 0 || w <= x || y < 0 || h <= y)
          continue;
        if (map2[x][y] == 0 && m[x][y] != 'X') {
          state.push(make_pair(x, y));
          dis[x][y] = dis[now.ft][now.sd] + 1;
          map2[now.ft][now.sd] = 1;
        }
      }
    }
    while (state.size())
      state.pop();

    state.push(make_pair(sx, sy));
  }

  cout << dis[sx][sy] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <map>
#include <queue>

#define rep(i, a, b) for (int i = (a); (i) < (b); (i)++)
#define ft first
#define sd second

using namespace std;

char m[1000][1000];
int dis[1000][1000];
queue<pair<int, int>> state;

int main() {
  int h, w, n;

  cin >> h >> w >> n;

  int sx, sy;
  rep(i, 0, h) {
    rep(j, 0, w) {
      cin >> m[j][i];
      if (m[j][i] == 'S') {
        sx = j;
        sy = i;
      }
    }
  }

  pair<int, int> p;
  p.ft = sx;
  p.sd = sy;

  rep(i, 0, h) {
    rep(j, 0, w) { dis[j][i] = -1; }
  }
  dis[sx][sy] = 0;
  state.push(p);

  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, -1, 0, 1};
  int map2[1000][1000];
  for (int lp = 1; lp <= n; lp++) {
    rep(i, 0, h) {
      rep(j, 0, w) { map2[j][i] = 0; }
    }

    map2[sx][sy] = 1;
    while (state.size()) {
      pair<int, int> now = state.front();
      state.pop();
      if (m[now.ft][now.sd] - '0' == lp) {
        sx = now.ft;
        sy = now.sd;
        break;
      }

      rep(lp2, 0, 4) {
        int x = now.ft + dx[lp2];
        int y = now.sd + dy[lp2];

        if (x < 0 || w <= x || y < 0 || h <= y)
          continue;
        if (map2[x][y] == 0 && m[x][y] != 'X') {
          state.push(make_pair(x, y));
          dis[x][y] = dis[now.ft][now.sd] + 1;
          map2[x][y] = 1;
        }
      }
    }
    while (state.size())
      state.pop();

    state.push(make_pair(sx, sy));
  }

  cout << dis[sx][sy] << endl;
  return 0;
}
Problem p00481 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

typedef std::pair<int, int> T;

const int vecx[4] = {1, 0, -1, 0};
const int vecy[4] = {0, -1, 0, 1};
int h, w, n;
T goal[10];
char filed[1000][1000];
int si, sj;
int ans;
int dis[1000][1000];

void bfs(T _goal, T _start) {
  std::queue<T> q;
  q.push(_start);
  while (q.size()) {
    T tmp = q.front();
    q.pop();
    if (tmp == _goal) {
      ans += dis[_goal.first][_goal.second];
      break;
    }
    int x = tmp.first;
    int y = tmp.second;
    for (int i = 0; i < 4; i++) {
      if (x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
          y + vecy[i] >= w)
        continue;
      if (filed[x + vecx[i]][y + vecy[i]] != 'X') {
        dis[x + vecx[i]][y + vecy[i]] = dis[x][y] + 1;
        q.push(std::make_pair(x + vecx[i], y + vecy[i]));
      }
    }
  }
}

int main() {
  std::cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      std::cin >> filed[i][j];
      if (filed[i][j] == 'S') {
        goal[0].first = i;
        goal[0].second = j;
      }
      if (filed[i][j] > '0' && filed[i][j] <= '9') {
        goal[filed[i][j] - '0'].first = i;
        goal[filed[i][j] - '0'].second = j;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    bfs(goal[i + 1], goal[i]);
    for (int j = 0; j < h; j++)
      for (int k = 0; k < w; k++)
        dis[j][k] = 0;
  }
  std::cout << ans << std::endl;
  return 0;
}

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

typedef std::pair<int, int> T;

const int vecx[4] = {1, 0, -1, 0};
const int vecy[4] = {0, -1, 0, 1};
int h, w, n;
T goal[10];
char filed[1000][1000];
int si, sj;
int ans;
int dis[1000][1000];

void bfs(T _goal, T _start) {
  std::queue<T> q;
  q.push(_start);
  while (q.size()) {
    T tmp = q.front();
    q.pop();
    if (tmp == _goal) {
      ans += dis[_goal.first][_goal.second];
      break;
    }
    int x = tmp.first;
    int y = tmp.second;
    for (int i = 0; i < 4; i++) {
      if (x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
          y + vecy[i] >= w)
        continue;
      if (filed[x + vecx[i]][y + vecy[i]] != 'X' &&
          !dis[x + vecx[i]][y + vecy[i]]) {
        dis[x + vecx[i]][y + vecy[i]] = dis[x][y] + 1;
        q.push(std::make_pair(x + vecx[i], y + vecy[i]));
      }
    }
  }
}

int main() {
  std::cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      std::cin >> filed[i][j];
      if (filed[i][j] == 'S') {
        goal[0].first = i;
        goal[0].second = j;
      }
      if (filed[i][j] > '0' && filed[i][j] <= '9') {
        goal[filed[i][j] - '0'].first = i;
        goal[filed[i][j] - '0'].second = j;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    bfs(goal[i + 1], goal[i]);
    for (int j = 0; j < h; j++)
      for (int k = 0; k < w; k++)
        dis[j][k] = 0;
  }
  std::cout << ans << std::endl;
  return 0;
}