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

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

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {

    vector<int> v[500];
    for (int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      v[a - 1].push_back(b - 1);
      v[b - 1].push_back(a - 1);
    }

    bool f[500] = {0};
    for (int i = 0; i < v[0].size(); i++) {
      f[v[0][i]] = 1;
      for (int j = 0; j < v[i].size(); j++) {
        f[v[v[0][i]][j]] = 1;
      }
    }

    int ans = 0;
    for (int i = 1; i < N; i++) {
      if (f[i])
        ans++;
    }

    cout << ans << endl;
  }
}

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

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {

    vector<int> v[500];
    for (int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      v[a - 1].push_back(b - 1);
      v[b - 1].push_back(a - 1);
    }

    bool f[500] = {0};
    for (int i = 0; i < v[0].size(); i++) {
      f[v[0][i]] = 1;
      for (int j = 0; j < v[v[0][i]].size(); j++) {
        f[v[v[0][i]][j]] = 1;
      }
    }

    int ans = 0;
    for (int i = 1; i < N; i++) {
      if (f[i])
        ans++;
    }

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

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

constexpr int MAX_N = 500;
constexpr int MAX_M = 10000;

int N, M;
int e[MAX_N][MAX_N];
bool flag[MAX_N] = {false};

int dfs(int n = 0, int depth = 0) {
  if (depth == 2) {
    if (flag[n]) {
      return 0;
    } else {
      flag[n] = true;
      return 1;
    }
  }

  int res = 0;
  if (!flag[n]) {
    ++res;
    flag[n] = true;
  }

  for (int i = 0; i < N; ++i) {
    if (e[n][i]) {
      res += dfs(i, depth + 1);
    }
  }

  return res;
}

int main() {
  while (true) {
    scanf("%d%d", &N, &M);
    if (N == 0 && M == 0) {
      break;
    }

    for (int i = 0; i < N; ++i) {
      flag[i] = false;
      for (int j = 0; j < M; ++j) {
        e[i][j] = false;
      }
    }
    flag[0] = true;

    for (int i = 0; i < M; ++i) {
      int a, b;
      scanf("%d%d", &a, &b);
      --a, --b;
      e[a][b] = e[b][a] = true;
    }

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

Fixed submission:
#include <cstdio>

constexpr int MAX_N = 500;
constexpr int MAX_M = 10000;

int N, M;
int e[MAX_N][MAX_N];
bool flag[MAX_N] = {false};

int dfs(int n = 0, int depth = 0) {
  if (depth == 2) {
    if (flag[n]) {
      return 0;
    } else {
      flag[n] = true;
      return 1;
    }
  }

  int res = 0;
  if (!flag[n]) {
    ++res;
    flag[n] = true;
  }

  for (int i = 0; i < N; ++i) {
    if (e[n][i]) {
      res += dfs(i, depth + 1);
    }
  }

  return res;
}

int main() {
  while (true) {
    scanf("%d%d", &N, &M);
    if (N == 0 && M == 0) {
      break;
    }

    for (int i = 0; i < N; ++i) {
      flag[i] = false;
      for (int j = 0; j < N; ++j) {
        e[i][j] = false;
      }
    }
    flag[0] = true;

    for (int i = 0; i < M; ++i) {
      int a, b;
      scanf("%d%d", &a, &b);
      --a, --b;
      e[a][b] = e[b][a] = true;
    }

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

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (!N)
      break;
    vector<vector<int>> edges(N);
    for (int i = 0; i < N; ++i) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      edges[a].push_back(b);
      edges[b].push_back(a);
    }
    vector<int> comes(N);
    for (auto e : edges[0]) {
      comes[e] = true;
    }
    vector<int> friends(N);
    for (int i = 1; i < N; ++i) {
      if (comes[i]) {
        friends[i] = true;
        for (auto e : edges[i])
          friends[e] = true;
      }
    }
    int ans = count(friends.begin() + 1, friends.end(), 1);
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (!N)
      break;
    vector<vector<int>> edges(N);
    for (int i = 0; i < M; ++i) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      edges[a].push_back(b);
      edges[b].push_back(a);
    }
    vector<int> comes(N);
    for (auto e : edges[0]) {
      comes[e] = true;
    }
    vector<int> friends(N);
    for (int i = 1; i < N; ++i) {
      if (comes[i]) {
        friends[i] = true;
        for (auto e : edges[i])
          friends[e] = true;
      }
    }
    int ans = count(friends.begin() + 1, friends.end(), 1);
    cout << ans << endl;
  }

  return 0;
}
Problem p00468 (C++)

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

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<vector<bool>> relation(n, vector<bool>(n));
    for (int i = 0; i < n; ++i) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      relation[a][b] = relation[b][a] = true;
    }
    vector<bool> friends(n);
    for (int i = 0; i < n; ++i) {
      if (relation[0][i]) {
        friends[i] = true;
        for (int j = 0; j < n; ++j) {
          if (relation[i][j]) {
            friends[j] = true;
          }
        }
      }
    }
    friends[0] = false;
    cout << count(friends.begin(), friends.end(), true) << endl;
  }
}

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

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<vector<bool>> relation(n, vector<bool>(n));
    for (int i = 0; i < m; ++i) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      relation[a][b] = relation[b][a] = true;
    }
    vector<bool> friends(n);
    for (int i = 0; i < n; ++i) {
      if (relation[0][i]) {
        friends[i] = true;
        for (int j = 0; j < n; ++j) {
          if (relation[i][j]) {
            friends[j] = true;
          }
        }
      }
    }
    friends[0] = false;
    cout << count(friends.begin(), friends.end(), true) << endl;
  }
}
Problem p00469 (C++)

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

using namespace std;

#define INF (1 << 29)

set<string> st;

void solve(int K, int S, vector<string> &s, string str) {
  if (K == 0) {
    st.insert(str);
  }

  int N = s.size();
  for (int i = 0; i < N; i++) {
    if (S >> i & 1)
      continue;
    solve(K - 1, S | (1 << i), s, str + s[i]);
  }
}

int main() {
  int N, K;
  while (cin >> N, N) {
    vector<string> s(N);
    cin >> K;
    for (int i = 0; i < N; i++) {
      cin >> s[i];
    }
    st.clear();
    solve(K, 0, s, "");
    cout << st.size() << endl;
  }
  return 0;
}

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

using namespace std;

#define INF (1 << 29)

set<string> st;

void solve(int K, int S, vector<string> &s, string str) {
  if (K == 0) {
    st.insert(str);
    return;
  }

  int N = s.size();
  for (int i = 0; i < N; i++) {
    if (S >> i & 1)
      continue;
    solve(K - 1, S | (1 << i), s, str + s[i]);
  }
}

int main() {
  int N, K;
  while (cin >> N, N) {
    vector<string> s(N);
    cin >> K;
    for (int i = 0; i < N; i++) {
      cin >> s[i];
    }
    st.clear();
    solve(K, 0, s, "");
    cout << st.size() << endl;
  }
  return 0;
}
Problem p00469 (C++)

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

int n, k;
string a[11];
set<string> all;
bool flag[11];

void solve(int num, string s) {
  if (num == k) {
    all.insert(s);
  }
  for (int i = 0; i < n; i++) {
    if (flag[i])
      continue;
    flag[i] = true;
    solve(num + 1, s + a[i]);
    flag[i] = false;
  }
}

int main() {
  while (cin >> n >> k, n + k) {
    all.clear();
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < 11; i++)
      flag[i] = false;
    solve(0, "");
    cout << all.size() << endl;
  }
}

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

int n, k;
string a[11];
set<string> all;
bool flag[11];

void solve(int num, string s) {
  if (num == k) {
    all.insert(s);
    return;
  }
  for (int i = 0; i < n; i++) {
    if (flag[i])
      continue;
    flag[i] = true;
    solve(num + 1, s + a[i]);
    flag[i] = false;
  }
}

int main() {
  while (cin >> n >> k, n + k) {
    all.clear();
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < 11; i++)
      flag[i] = false;
    solve(0, "");
    cout << all.size() << endl;
  }
}
Problem p00469 (C++)

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

using namespace std;

set<int> se;

int N, K;

int card[10];
bool used[10];

void rec(int num, int k) {
  if (k == K) {
    se.insert(num);
    return;
  }
  for (int i = 0; i < N; i++) {
    if (used[i] == true)
      continue;
    used[i] = true;
    int nnum;
    if (card[i] < 10)
      nnum = num * 10 + card[i];
    else
      nnum = num * 100 + card[i];
    rec(nnum, k + 1);
    used[i] = false;
  }
}

int main() {
  while (true) {
    scanf("%d%d", &N, &K);
    se.clear();
    for (int i = 0; i < N; i++) {
      scanf("%d", card + i);
    }
    memset(used, false, sizeof(used));
    rec(0, 0);
    printf("%d\n", se.size());
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <set>

using namespace std;

set<int> se;

int N, K;

int card[10];
bool used[10];

void rec(int num, int k) {
  if (k == K) {
    se.insert(num);
    return;
  }
  for (int i = 0; i < N; i++) {
    if (used[i] == true)
      continue;
    used[i] = true;
    int nnum;
    if (card[i] < 10)
      nnum = num * 10 + card[i];
    else
      nnum = num * 100 + card[i];
    rec(nnum, k + 1);
    used[i] = false;
  }
}

int main() {
  while (true) {
    scanf("%d%d", &N, &K);
    if (N == 0 && K == 0)
      break;
    se.clear();
    for (int i = 0; i < N; i++) {
      scanf("%d", card + i);
    }
    memset(used, false, sizeof(used));
    rec(0, 0);
    printf("%d\n", se.size());
  }
  return 0;
}
Problem p00469 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

#ifdef _MSC_VER
#define __builtin_popcount __popcnt
#endif

int n, m;
int main() {
  while (cin >> n >> m, n) {
    vector<int> a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    vector<set<ll>> dp(1 << n);
    dp[0] = set<ll>({0});
    for (int i = 1; i < 1 << n; i++) {
      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          for (int k : dp[i - (1 << j)]) {
            dp[i].insert(stoll(to_string(k) + to_string(a[j])));
          }
        }
      }
    }
    set<ll> ret;
    for (int i = 0; i < 1 << n; i++) {
      if (__builtin_popcount(i) == m) {
        ret.insert(dp[i].begin(), dp[i].end());
      }
    }
    cout << ret.size() << endl;
  }
  return 0;
}

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

typedef long long ll;

#ifdef _MSC_VER
#define __builtin_popcount __popcnt
#endif

int n, m;
int main() {
  while (cin >> n >> m, n) {
    vector<int> a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    vector<set<ll>> dp(1 << n);
    dp[0] = set<ll>({0});
    for (int i = 1; i < 1 << n; i++) {
      if (__builtin_popcount(i) > m)
        continue;
      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          for (int k : dp[i - (1 << j)]) {
            dp[i].insert(stoll(to_string(k) + to_string(a[j])));
          }
        }
      }
    }
    set<ll> ret;
    for (int i = 0; i < 1 << n; i++) {
      if (__builtin_popcount(i) == m) {
        ret.insert(dp[i].begin(), dp[i].end());
      }
    }
    cout << ret.size() << endl;
  }
  return 0;
}
Problem p00470 (C++)

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

int main() {
  int w, h;
  while (cin >> w >> h) {
    static int dp[111][111][4];
    for (int i = 0; i < 111; i++)
      for (int j = 0; j < 111; j++)
        for (int k = 0; k < 4; k++)
          dp[i][j][k] = 0;
    for (int i = 0; i < h; i++)
      dp[i][0][0] = 1;
    for (int i = 0; i < w; i++)
      dp[0][i][2] = 1;
    for (int i = 1; i < h; i++) {
      for (int j = 1; j < w; j++) {
        if (i - 1 >= 0)
          dp[i][j][0] += dp[i - 1][j][0] + dp[i - 1][j][1];
        if (i - 1 >= 0)
          dp[i][j][1] += dp[i - 1][j][2];
        if (j - 1 >= 0)
          dp[i][j][2] += dp[i][j - 1][2] + dp[i][j - 1][3];
        if (j - 1 >= 0)
          dp[i][j][3] += dp[i][j - 1][0];
        for (int k = 0; k < 4; k++)
          dp[i][j][k] %= 100000;
      }
    }

    int ans = 0;
    for (int i = 0; i < 4; i++)
      ans += dp[h - 1][w - 1][i];
    cout << ans % 100000 << endl;
  }
}

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

int main() {
  int w, h;
  while (cin >> w >> h && h | w) {
    static int dp[111][111][4];
    for (int i = 0; i < 111; i++)
      for (int j = 0; j < 111; j++)
        for (int k = 0; k < 4; k++)
          dp[i][j][k] = 0;
    for (int i = 0; i < h; i++)
      dp[i][0][0] = 1;
    for (int i = 0; i < w; i++)
      dp[0][i][2] = 1;
    for (int i = 1; i < h; i++) {
      for (int j = 1; j < w; j++) {
        if (i - 1 >= 0)
          dp[i][j][0] += dp[i - 1][j][0] + dp[i - 1][j][1];
        if (i - 1 >= 0)
          dp[i][j][1] += dp[i - 1][j][2];
        if (j - 1 >= 0)
          dp[i][j][2] += dp[i][j - 1][2] + dp[i][j - 1][3];
        if (j - 1 >= 0)
          dp[i][j][3] += dp[i][j - 1][0];
        for (int k = 0; k < 4; k++)
          dp[i][j][k] %= 100000;
      }
    }

    int ans = 0;
    for (int i = 0; i < 4; i++)
      ans += dp[h - 1][w - 1][i];
    cout << ans % 100000 << endl;
  }
}
Problem p00470 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;

int w, h;
int dp[100][100][2][2];

int main() {
  while (1) {
    scanf("%d %d", &w, &h);
    for (int i = 1; i < w; i++) {
      dp[i][0][1][1] = 1;
    }
    for (int j = 1; j < h; j++) {
      dp[0][j][0][0] = 1;
    }
    for (int i = 1; i < w; i++) {
      for (int j = 1; j < h; j++) {
        dp[i][j][0][0] = (dp[i][j - 1][0][0] + dp[i][j - 1][0][1]) % 100000;
        dp[i][j][0][1] = dp[i][j - 1][1][1] % 100000;
        dp[i][j][1][0] = dp[i - 1][j][0][0] % 100000;
        dp[i][j][1][1] = (dp[i - 1][j][1][0] + dp[i - 1][j][1][1]) % 100000;
        ;
      }
    }
    int ans = 0;
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < 2; j++) {
        ans += dp[w - 1][h - 1][i][j];
        ans %= 100000;
      }
    printf("%d\n", ans);
/*	
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				printf("%d\t", dp[j][i][0][0] + dp[j][i][0][1] + dp[j][i][1][0] + dp[j][i][1][1]);
			}
			printf("\n");
		}
*/	}
return 0;
}

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

int w, h;
int dp[100][100][2][2];

int main() {
  while (1) {
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;
    for (int i = 1; i < w; i++) {
      dp[i][0][1][1] = 1;
    }
    for (int j = 1; j < h; j++) {
      dp[0][j][0][0] = 1;
    }
    for (int i = 1; i < w; i++) {
      for (int j = 1; j < h; j++) {
        dp[i][j][0][0] = (dp[i][j - 1][0][0] + dp[i][j - 1][0][1]) % 100000;
        dp[i][j][0][1] = dp[i][j - 1][1][1] % 100000;
        dp[i][j][1][0] = dp[i - 1][j][0][0] % 100000;
        dp[i][j][1][1] = (dp[i - 1][j][1][0] + dp[i - 1][j][1][1]) % 100000;
        ;
      }
    }
    int ans = 0;
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < 2; j++) {
        ans += dp[w - 1][h - 1][i][j];
        ans %= 100000;
      }
    printf("%d\n", ans);
/*	
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				printf("%d\t", dp[j][i][0][0] + dp[j][i][0][1] + dp[j][i][1][0] + dp[j][i][1][1]);
			}
			printf("\n");
		}
*/	}
return 0;
}
Problem p00470 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100001
#define dmp make_pair
#define dpb push_back
#define P pair<int, int>
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int dp[101][101][2][2];
int main() {
  int w, h;
  while (1) {
    scanf("%d%d", &w, &h);
    for (int i = 1; i <= w; i++) {
      for (int j = 1; j <= h; j++) {
        dp[i][j][0][0] = dp[i][j - 1][0][0] + dp[i][j - 1][1][0];
        dp[i][j][0][1] = dp[i - 1][j][0][1] + dp[i - 1][j][1][1];
        dp[i][j][1][0] = dp[i - 1][j][0][1];
        dp[i][j][1][1] = dp[i][j - 1][0][0];
        rep(x, 2) rep(y, 2) dp[i][j][x][y] = dp[i][j][x][y] % 100000;
        dp[1][2][0][0] = dp[2][1][0][1] = 1;
        dp[1][2][1][1] = dp[2][1][1][0] = 1;
      }
    }
    int ans = 0;
    rep(i, 2) ans += dp[w][h][0][i];
    printf("%d\n", ans % 100000);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100001
#define dmp make_pair
#define dpb push_back
#define P pair<int, int>
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int dp[101][101][2][2];
int main() {
  int w, h;
  while (1) {
    scanf("%d%d", &w, &h);
    if (!w)
      break;
    for (int i = 1; i <= w; i++) {
      for (int j = 1; j <= h; j++) {
        dp[i][j][0][0] = dp[i][j - 1][0][0] + dp[i][j - 1][1][0];
        dp[i][j][0][1] = dp[i - 1][j][0][1] + dp[i - 1][j][1][1];
        dp[i][j][1][0] = dp[i - 1][j][0][1];
        dp[i][j][1][1] = dp[i][j - 1][0][0];
        rep(x, 2) rep(y, 2) dp[i][j][x][y] = dp[i][j][x][y] % 100000;
        dp[1][2][0][0] = dp[2][1][0][1] = 1;
        dp[1][2][1][1] = dp[2][1][1][0] = 1;
      }
    }
    int ans = 0;
    rep(i, 2) ans += dp[w][h][0][i];
    printf("%d\n", ans % 100000);
  }
  return 0;
}
Problem p00470 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define LL long long
static const LL INF = 1 << 24;
static const LL LL_INF = 1 << 60;
static const int mod = 1e+5;
static const int MAX_N = 100;
int dx[] = {0, 1};
int dy[] = {1, 0};
using namespace std;

int w, h;
LL mem[MAX_N][MAX_N][2][2][2][2];
LL cal(int x, int y, int px, int py, int ppx, int ppy) {
  LL res = 0;
  if (mem[x][y][px][py][ppx][ppy] != 0)
    return mem[x][y][px][py][ppx][ppy];
  if (x >= w || y >= h)
    return 0;
  if (x == w - 1 && y == h - 1)
    return 1;
  FOR(i, 0, 1) {
    if (px != ppx && py != ppy && px != dx[i] && py != dy[i])
      continue;
    res += cal(x + dx[i], y + dy[i], dx[i], dy[i], px, py) % mod;
  }
  return mem[x][y][px][py][ppx][ppy] = res % mod;
}

int main() {
  while (1) {
    cin >> w >> h;
    memset(mem, 0, sizeof(mem));
    if (w == 0 && h == 0)
      break;
    cout << cal(0, 0, 0, 0, 0, 0) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define LL long long
static const LL INF = 1 << 24;
static const LL LL_INF = 1 << 60;
static const int mod = 1e+5;
static const int MAX_N = 101;
int dx[] = {0, 1};
int dy[] = {1, 0};
using namespace std;

int w, h;
LL mem[MAX_N][MAX_N][2][2][2][2];
LL cal(int x, int y, int px, int py, int ppx, int ppy) {
  LL res = 0;
  if (mem[x][y][px][py][ppx][ppy] != 0)
    return mem[x][y][px][py][ppx][ppy];
  if (x >= w || y >= h)
    return 0;
  if (x == w - 1 && y == h - 1)
    return 1;
  FOR(i, 0, 1) {
    if (px != ppx && py != ppy && px != dx[i] && py != dy[i])
      continue;
    res += cal(x + dx[i], y + dy[i], dx[i], dy[i], px, py) % mod;
  }
  return mem[x][y][px][py][ppx][ppy] = res % mod;
}

int main() {
  while (1) {
    cin >> w >> h;
    memset(mem, 0, sizeof(mem));
    if (w == 0 && h == 0)
      break;
    cout << cal(0, 0, 0, 0, 0, 0) << endl;
  }
  return 0;
}
Problem p00471 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#include <string.h>

int map[10 + 2][10 + 2];

int housecnt;
int ans;
int cathx, cathy;

int calc(int x, int y, int depth) {
  int i;

  if (depth == housecnt && (cathx == x || cathy == y)) {
    ans++;
    return (0);
  }

  map[x][y] = 0;
  i = 1;
  while (map[x + i][y] == 0)
    i++;
  if (map[x + i][y] == 1) {
    calc(x + i, y, depth + 1);
  }
  i = 1;
  while (map[x - i][y] == 0)
    i++;
  if (map[x - i][y] == 1) {
    calc(x - i, y, depth + 1);
  }
  i = 1;
  while (map[x][y + i] == 0)
    i++;
  if (map[x][y + i] == 1) {
    calc(x, y + i, depth + 1);
  }
  i = 1;
  while (map[x][y - i] == 0)
    i++;
  if (map[x][y - i] == 1) {
    calc(x, y - i, depth + 1);
  }
  map[x][y] = 1;

  return (0);
}

int main(void) {
  int w, h;

  while (1) {
    scanf("%d%d", &w, &h);

    memset(map, -1, sizeof(map));

    housecnt = ans = 0;

    for (int y = 1; y <= h; y++) {
      for (int x = 1; x <= w; x++) {
        scanf("%d", &map[x][y]);
        if (map[x][y] == 2) {
          cathx = x;
          cathy = y;
          map[x][y] = 0;
        }
        if (map[x][y] == 1) {
          housecnt++;
        }
      }
    }

    calc(cathx, cathy, 0);

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

  return (0);
}

Fixed submission:
#include <stdio.h>
#include <string.h>

int map[10 + 2][10 + 2];

int housecnt;
int ans;
int cathx, cathy;

int calc(int x, int y, int depth) {
  int i;

  if (depth == housecnt && (cathx == x || cathy == y)) {
    ans++;
    return (0);
  }

  map[x][y] = 0;
  i = 1;
  while (map[x + i][y] == 0)
    i++;
  if (map[x + i][y] == 1) {
    calc(x + i, y, depth + 1);
  }
  i = 1;
  while (map[x - i][y] == 0)
    i++;
  if (map[x - i][y] == 1) {
    calc(x - i, y, depth + 1);
  }
  i = 1;
  while (map[x][y + i] == 0)
    i++;
  if (map[x][y + i] == 1) {
    calc(x, y + i, depth + 1);
  }
  i = 1;
  while (map[x][y - i] == 0)
    i++;
  if (map[x][y - i] == 1) {
    calc(x, y - i, depth + 1);
  }
  map[x][y] = 1;

  return (0);
}

int main(void) {
  int w, h;

  while (1) {
    scanf("%d%d", &w, &h);

    if (w == 0 && h == 0) {
      break;
    }

    memset(map, -1, sizeof(map));

    housecnt = ans = 0;

    for (int y = 1; y <= h; y++) {
      for (int x = 1; x <= w; x++) {
        scanf("%d", &map[x][y]);
        if (map[x][y] == 2) {
          cathx = x;
          cathy = y;
          map[x][y] = 0;
        }
        if (map[x][y] == 1) {
          housecnt++;
        }
      }
    }

    calc(cathx, cathy, 0);

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

  return (0);
}
Problem p00472 (C++)

Buggy submission (status: Runtime Error):
// 57
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int d[10000] = {};
  for (int i = 1; i < n; i++) {
    cin >> d[i];
    d[i] += d[i - 1];
  }
  int p = 0;
  int s = 0;
  while (m--) {
    int a;
    cin >> a;
    s = (s + abs(d[p] - d[p + a])) % 100000;
    p += a;
  }
  cout << s << endl;
  return 0;
}

Fixed submission:
// 57
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int d[100000] = {};
  for (int i = 1; i < n; i++) {
    cin >> d[i];
    d[i] += d[i - 1];
  }
  int p = 0;
  int s = 0;
  while (m--) {
    int a;
    cin >> a;
    s = (s + abs(d[p] - d[p + a])) % 100000;
    p += a;
  }
  cout << s << endl;
  return 0;
}
Problem p00472 (C++)

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

const int MAX_N = 1 << 17, MOD = 100000; // ちょっと多いけど気にしないし
class SegmentTree {
public:
  SegmentTree(int _n) {
    n = 1;
    while (n < _n)
      n *= 2;
    std::fill(data, data + MAX_N, 0);
  }

  void update(int k, int i) {
    k += n - 1;
    data[k] = i;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = (data[k * 2 + 1] + data[k * 2 + 2]) % MOD;
    }
  }

  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;

    if (a <= l && r <= b)
      return data[k];
    else {
      int cl = query(a, b, k * 2 + 1, l, (l + r) / 2),
          cr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return (cl + cr) % MOD;
    }
  }

  int getN() { return n; }

private:
  int n, data[MAX_N];
};

int main() {
  int n, m;
  std::cin >> n >> m;
  SegmentTree st(--n);

  for (int i = 0; i < n; i++) {
    int d;
    std::cin >> d;
    st.update(i, d);
  }

  int from = 0, to, res = 0;
  for (int i = 0; i < m; i++) {
    int a;
    std::cin >> a;
    to = from + a;
    int lower = std::min(from, to), upper = std::max(from, to);
    res = (res + st.query(lower, upper, 0, 0, st.getN())) % MOD;
    from = to;
  }

  std::cout << res << std::endl;
}

Fixed submission:
#include <iostream>

const int MAX_N = 1 << 18,
          MOD = 100000; // ちょっと多いけど気にしないし(少なかったし)
class SegmentTree {
public:
  SegmentTree(int _n) {
    n = 1;
    while (n < _n)
      n *= 2;
    std::fill(data, data + MAX_N, 0);
  }

  void update(int k, int i) {
    k += n - 1;
    data[k] = i;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = (data[k * 2 + 1] + data[k * 2 + 2]) % MOD;
    }
  }

  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;

    if (a <= l && r <= b)
      return data[k];
    else {
      int cl = query(a, b, k * 2 + 1, l, (l + r) / 2),
          cr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return (cl + cr) % MOD;
    }
  }

  int getN() { return n; }

private:
  int n, data[MAX_N];
};

int main() {
  int n, m;
  std::cin >> n >> m;
  SegmentTree st(--n);

  for (int i = 0; i < n; i++) {
    int d;
    std::cin >> d;
    st.update(i, d);
  }

  int from = 0, to, res = 0;
  for (int i = 0; i < m; i++) {
    int a;
    std::cin >> a;
    to = from + a;
    int lower = std::min(from, to), upper = std::max(from, to);
    res = (res + st.query(lower, upper, 0, 0, st.getN())) % MOD;
    from = to;
  }

  std::cout << res << std::endl;
}
Problem p00472 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m;
int s[100001];
int a;

int main() {
  cin >> n >> m;
  int ans = 0;
  s[1] = 0;
  FOR(i, 2, n + 1) cin >> s[i];
  FOR(i, 1, n + 1) s[i + 1] += s[i];
  int now = 1;
  REP(i, m) {
    cin >> a;
    ans = (ans + abs(s[now + a] - s[now])) % 100000;
    now += a;
  }
  cout << ans << endl;
}

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

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m;
int s[100100];
int a;

int main() {
  cin >> n >> m;
  int ans = 0;
  s[1] = 0;
  FOR(i, 2, n + 1) cin >> s[i];
  FOR(i, 1, n + 1) s[i + 1] += s[i];
  int now = 1;
  REP(i, m) {
    cin >> a;
    ans = (ans + abs(s[now + a] - s[now])) % 100000;
    now += a;
  }
  cout << ans << endl;
}
Problem p00472 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> // require sort next_permutation count etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits> // require INT_MAX etc
#include <cmath>   // require sqrt ceil floor
#include <cstdio>  // require printf
#include <cstdlib> // require abs
#include <cstring> // require memset
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <stdint.h> // require uint64_t
#include <string>
#include <vector>

using namespace std;

const int M = 100000;

int main() {
  //	cut here before submit
  //	freopen ("testcase.traveler", "r", stdin );
  int n, m;

  while (cin >> n >> m && n && m) {
    vector<int> d(n, 0);
    for (int i = 0; i < n - 1; ++i) {
      cin >> d[i];
    } // end for
    vector<int> a(m, 0);
    for (int i = 0; i < m; ++i) {
      cin >> a[i];
    } // end for

    int res = 0;
    int curr = 0;
    for (int i = 0; i < m; ++i) {
      int sign = (a[i] > 0 ? 1 : -1);
      int offset = (sign == 1 ? 0 : -1);
      for (int j = 0; j < sign * a[i]; ++j) {
        res = (res + d[curr + offset]) % M;
        curr += sign;
      } // end if
    }   // end for
    cout << res << endl;
  } // end loop

  return 0;
}

Fixed submission:
#include <algorithm> // require sort next_permutation count etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits> // require INT_MAX etc
#include <cmath>   // require sqrt ceil floor
#include <cstdio>  // require printf
#include <cstdlib> // require abs
#include <cstring> // require memset
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <stdint.h> // require uint64_t
#include <string>
#include <vector>

using namespace std;

const int M = 100000;

int main() {
  //	cut here before submit
  //	freopen ("testcase.traveler", "r", stdin );
  int n, m;

  while (cin >> n >> m && n && m) {
    vector<int> d(n, 0);
    for (int i = 0; i < n - 1; ++i) {
      cin >> d[i];
    } // end for
    vector<int> a(m, 0);
    for (int i = 0; i < m; ++i) {
      cin >> a[i];
    } // end for

    int res = 0;
    int curr = 0;
    for (int i = 0; i < m; ++i) {
      //			int sign = (a[i] > 0 ? 1 : -1 );
      //			int offset = (sign == 1 ? 0 : -1 );
      int sum =
          (a[i] > 0 ? accumulate(d.begin() + curr, d.begin() + curr + a[i], 0)
                    : accumulate(d.begin() + curr + a[i], d.begin() + curr, 0));
      res = (res + sum) % M;
      curr += a[i];
    } // end for
    cout << res << endl;
  } // end loop

  return 0;
}
Problem p00472 (C++)

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

int main() {
  int town[100] = {0};
  int n, m;
  int ans = 0;
  int now = 0;
  scanf("%d%d", &n, &m);
  town[0] = 0;
  for (int i = 1; i < n; i++) {
    scanf("%d", &town[i]);
  }

  /*
  for(int i = 0; i < n; i++)
    printf("town%d:%d\n", i, town[i]);
  */

  for (int i = 0; i < m; i++) {
    int h;
    scanf("%d", &h);
    // printf("now:%d ans:%d\n", now, ans);

    if (h > 0) {
      for (int j = 0; j < h; j++) {
        ans += town[++now];
        // printf("mooov:%d\n", town[now]);
      }
    } else {
      for (int j = 0; j < -h; j++) {
        ans += town[now--];
        // printf("mooov:%d\n", town[now]);
      }
    }

    ans %= 100000;
  }
  printf("%d\n", ans);
}

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

int main() {
  int town[1000000] = {0};
  int n, m;
  int ans = 0;
  int now = 0;
  scanf("%d%d", &n, &m);
  town[0] = 0;
  for (int i = 1; i < n; i++) {
    scanf("%d", &town[i]);
  }

  /*
  for(int i = 0; i < n; i++)
    printf("town%d:%d\n", i, town[i]);
  */

  for (int i = 0; i < m; i++) {
    int h;
    scanf("%d", &h);
    // printf("now:%d ans:%d\n", now, ans);

    if (h > 0) {
      for (int j = 0; j < h; j++) {
        ans += town[++now];
        // printf("mooov:%d\n", town[now]);
      }
    } else {
      for (int j = 0; j < -h; j++) {
        ans += town[now--];
        // printf("mooov:%d\n", town[now]);
      }
    }

    ans %= 100000;
  }
  printf("%d\n", ans);
}
Problem p00472 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, m;
  for (; cin >> n >> m, n;) {
    long long int map[n + 2];
    long long int sum = 0;
    map[1] = 0;
    for (int i = 2; i <= n; i++) {
      int tmp;
      cin >> tmp;
      sum += tmp;
      map[i] = sum;
      //	  cout<<"map["<<i<<"]="<<map[i]<<endl;
    }
    long long int ans = 0;
    int now = 1;
    for (int i = 1; i <= m; i++) {
      int tmp;
      cin >> tmp;
      int old = now;
      now += tmp;
      ans += abs(map[now] - map[old]);
    }
    cout << ans % 100000 << endl;
  }
}

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

int main() {
  int n, m;
  for (; cin >> n >> m;) {
    long long int map[n + 2];
    long long int sum = 0;
    map[1] = 0;
    for (int i = 2; i <= n; i++) {
      int tmp;
      cin >> tmp;
      sum += tmp;
      map[i] = sum;
      //	  cout<<"map["<<i<<"]="<<map[i]<<endl;
    }
    long long int ans = 0;
    int now = 1;
    for (int i = 1; i <= m; i++) {
      int tmp;
      cin >> tmp;
      int old = now;
      now += tmp;
      ans += abs(map[now] - map[old]);
    }
    cout << ans % 100000 << endl;
  }
}
Problem p00472 (C++)

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

#define MAX_N (1 << 16)
#define N 100000
#define mod 100000

class Range {
  int data[MAX_N];
  int n;

public:
  void init(int pn) {
    n = 1;
    while (n < pn)
      n <<= 1;
    for (int i = 0; i < 2 * n; ++i)
      data[i] = 0;
  }

  void add(int k, int a) {
    k += n - 1;

    while (k) {
      data[k] = (data[k] + a) % mod;
      k = (k - 1) / 2;
    }
    data[0] = (data[0] + a) % mod;
  }

  int query(int a, int b, int l, int r, int k) {
    if (r <= a || b <= l)
      return 0;
    int sum = 0;
    if (a <= l && r <= b)
      return data[k];
    sum += query(a, b, l, (l + r) / 2, 2 * k + 1);
    sum += query(a, b, (l + r) / 2, r, 2 * k + 2);
    return sum % mod;
  }

  int sum(int a, int b) { return query(a, b, 0, n, 0); }
};

int main() {
  int n, m;
  Range r;
  int s, a, ans = 0;
  cin >> n >> m;
  r.init(n - 1);
  for (int i = 0; i < n - 1; ++i) {
    cin >> s;
    r.add(i, s);
  }
  int pos = 0;
  for (int i = 0; i < m; ++i) {
    cin >> a;
    if (a > 0) {
      ans = (ans + r.sum(pos, pos + a)) % mod;
    } else if (a < 0) {
      ans = (ans + r.sum(pos + a, pos)) % mod;
    }
    pos += a;
  }
  cout << ans << endl;
  return 0;
}

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

#define MAX_N (1 << 18)
#define N 100000
#define mod 100000

class Range {
  int data[MAX_N];
  int n;

public:
  void init(int pn) {
    n = 1;
    while (n < pn)
      n <<= 1;
    for (int i = 0; i < 2 * n; ++i)
      data[i] = 0;
  }

  void add(int k, int a) {
    k += n - 1;

    while (k) {
      data[k] = (data[k] + a) % mod;
      k = (k - 1) / 2;
    }
    data[0] = (data[0] + a) % mod;
  }

  int query(int a, int b, int l, int r, int k) {
    if (r <= a || b <= l)
      return 0;
    int sum = 0;
    if (a <= l && r <= b)
      return data[k];
    sum += query(a, b, l, (l + r) / 2, 2 * k + 1);
    sum += query(a, b, (l + r) / 2, r, 2 * k + 2);
    return sum % mod;
  }

  int sum(int a, int b) { return query(a, b, 0, n, 0); }
};

int main() {
  int n, m;
  Range r;
  int s, a, ans = 0;
  cin >> n >> m;
  r.init(n - 1);
  for (int i = 0; i < n - 1; ++i) {
    cin >> s;
    r.add(i, s);
  }
  int pos = 0;
  for (int i = 0; i < m; ++i) {
    cin >> a;
    if (a > 0) {
      ans = (ans + r.sum(pos, pos + a)) % mod;
    } else if (a < 0) {
      ans = (ans + r.sum(pos + a, pos)) % mod;
    }
    pos += a;
  }
  cout << ans << endl;
  return 0;
}
Problem p00473 (C++)

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int n, t[10000];
  cin >> n;
  rep(i, 0, n - 1) cin >> t[i];

  static int dp[2][2][6000];
  fill_n((int *)dp, 2 * 2 * 6000, INF);
  dp[0][0][1] = dp[0][1][0] = 0;
  rep(i, 0, n - 1) {
    rep(j, 0, i + 2) {
      if (j - 1 >= 0)
        dp[1][0][j] = min(dp[0][0][j - 1], dp[0][1][j - 1] + t[i]);
      dp[1][1][j] = min(dp[0][1][j], dp[0][0][j] + t[i]);

      dump(j);
      dump(dp[1][0][j]);
      dump(dp[1][1][j]);
    }
    swap(dp[0], dp[1]);
  }

  cout << min(dp[0][0][n / 2], dp[0][1][n / 2]) << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int n, t[10000];
  cin >> n;
  rep(i, 0, n - 1) cin >> t[i];

  static int dp[2][2][6000];
  fill_n((int *)dp, 2 * 2 * 6000, INF);
  dp[0][0][1] = dp[0][1][0] = 0;
  rep(i, 0, n - 1) {
    rep(j, 0, n / 2 + 100) {
      if (j - 1 >= 0)
        dp[1][0][j] = min(dp[0][0][j - 1], dp[0][1][j - 1] + t[i]);
      dp[1][1][j] = min(dp[0][1][j], dp[0][0][j] + t[i]);

      dump(j);
      dump(dp[1][0][j]);
      dump(dp[1][1][j]);
    }
    swap(dp[0], dp[1]);
  }

  cout << min(dp[0][0][n / 2], dp[0][1][n / 2]) << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}
Problem p00473 (C++)

Buggy submission (status: Runtime Error):
// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int dp[5005][2][2];
int main() {
  int n;
  int v[10005];
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d", &v[i]);
  }
  for (int i = 0; i < 5005; i++)
    for (int j = 0; j < 2; j++)
      for (int k = 0; k < 2; k++)
        dp[i][j][k] = INF;
  dp[1][0][0] = 0;
  int cur = 0, nex = 1;
  for (int i = 1; i < n; i++) {
    for (int i = 0; i < 5005; i++)
      for (int j = 0; j < 2; j++)
        dp[i][j][nex] = INF;
    for (int j = n - 1; j >= 0; j--) {
      dp[j + 1][0][nex] = min(dp[j + 1][0][nex], dp[j][0][cur]);
      dp[j][1][nex] = min(dp[j][1][nex], dp[j][0][cur] + v[i]);

      dp[j][1][nex] = min(dp[j][1][nex], dp[j][1][cur]);
      dp[j + 1][0][nex] = min(dp[j + 1][0][nex], dp[j][1][cur] + v[i]);
    }
    cur = 1 - cur;
    nex = 1 - nex;
  }
  printf("%d\n", min(dp[n / 2][0][cur], dp[n / 2][1][cur]));
}

Fixed submission:
// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int dp[5005][2][2];
int main() {
  int n;
  int v[10005];
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d", &v[i]);
  }
  for (int i = 0; i < 5005; i++)
    for (int j = 0; j < 2; j++)
      for (int k = 0; k < 2; k++)
        dp[i][j][k] = INF;
  dp[1][0][0] = 0;
  int cur = 0, nex = 1;
  for (int i = 1; i < n; i++) {
    for (int i = 0; i < 5005; i++)
      for (int j = 0; j < 2; j++)
        dp[i][j][nex] = INF;
    for (int j = min(n / 2, i); j >= max(0, i - n / 2); j--) {
      dp[j + 1][0][nex] = min(dp[j + 1][0][nex], dp[j][0][cur]);
      dp[j][1][nex] = min(dp[j][1][nex], dp[j][0][cur] + v[i]);

      dp[j][1][nex] = min(dp[j][1][nex], dp[j][1][cur]);
      dp[j + 1][0][nex] = min(dp[j + 1][0][nex], dp[j][1][cur] + v[i]);
    }
    cur = 1 - cur;
    nex = 1 - nex;
  }
  printf("%d\n", min(dp[n / 2][0][cur], dp[n / 2][1][cur]));
}
Problem p00473 (C++)

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

#define MAX_N 2000
#define INF 2000000000

int dp[2][MAX_N]
      [2]; // dp[????????????][???????????§??§A?????????????????????][??´???]
int n, a[MAX_N];

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> a[i];
  }
  memset(dp, 127, sizeof(dp));
  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (dp[0][j][0] < INF) {
        dp[1][j + 1][0] = min(dp[1][j + 1][0], dp[0][j][0]);
        dp[1][j + 1][1] = min(dp[1][j + 1][1], dp[0][j][0] + a[i + 1]);
      }
      if (dp[0][j][1] < INF) {
        dp[1][j][1] = min(dp[1][j][1], dp[0][j][1]);
        dp[1][j][0] = min(dp[1][j][0], dp[0][j][1] + a[i + 1]);
      }
    }
    for (int j = 0; j <= n; j++) {
      dp[0][j][0] = dp[1][j][0];
      dp[0][j][1] = dp[1][j][1];
      dp[1][j][0] = INF;
      dp[1][j][1] = INF;
    }
  }
  cout << min(dp[0][n / 2][0], dp[0][n / 2][1]) << endl;
  return 0;
}

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

#define MAX_N 500000
#define INF 2000000000

int dp[2][MAX_N]
      [2]; // dp[????????????][???????????§??§A?????????????????????][??´???]
int n, a[MAX_N];

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> a[i];
  }
  memset(dp, 127, sizeof(dp));
  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (dp[0][j][0] < INF) {
        dp[1][j + 1][0] = min(dp[1][j + 1][0], dp[0][j][0]);
        dp[1][j + 1][1] = min(dp[1][j + 1][1], dp[0][j][0] + a[i + 1]);
      }
      if (dp[0][j][1] < INF) {
        dp[1][j][1] = min(dp[1][j][1], dp[0][j][1]);
        dp[1][j][0] = min(dp[1][j][0], dp[0][j][1] + a[i + 1]);
      }
    }
    for (int j = 0; j <= n; j++) {
      dp[0][j][0] = dp[1][j][0];
      dp[0][j][1] = dp[1][j][1];
      dp[1][j][0] = INF;
      dp[1][j][1] = INF;
    }
  }
  cout << min(dp[0][n / 2][0], dp[0][n / 2][1]) << endl;
  return 0;
}
Problem p00474 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second
#define all(v) v.begin(), v.end()

using namespace std;

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

const int INF = 1 << 29;

int N, L;

int v[10002];
int memo[10002];

int dfs(int n) {
  if (memo[n])
    return memo[n];
  int res = L - v[n];
  int wait = 0;
  if (v[n] < v[n + 1])
    wait = dfs(n + 1);
  if (v[n] < v[n - 1])
    wait = max(wait, dfs(n - 1));
  return memo[n] = res + wait;
}

int main() {
  cin >> N >> L;
  for (int i = 1; i <= N; i++)
    cin >> v[i];
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, dfs(i));
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second
#define all(v) v.begin(), v.end()

using namespace std;

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

const int INF = 1 << 29;

int N, L;

int v[100002];
int memo[100002];

int dfs(int n) {
  if (memo[n])
    return memo[n];
  int res = L - v[n];
  int wait = 0;
  if (v[n] < v[n + 1])
    wait = dfs(n + 1);
  if (v[n] < v[n - 1])
    wait = max(wait, dfs(n - 1));
  return memo[n] = res + wait;
}

int main() {
  cin >> N >> L;
  for (int i = 1; i <= N; i++)
    cin >> v[i];
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, dfs(i));
  }
  cout << ans << endl;

  return 0;
}