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

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

#define rep(i, s) for (int i = 0; i < s; ++i)
#define all(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define VEC(a, n) vector<int> a(n)
#define PQ(a) priority_queue<int> a
#define PQmin(a) priority_queue<int, : vector<int>, greater<int>> a
#define PAIR pair<int, int>
/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main() {
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    for (int j = i + 1; j < s.size(); j++) {
      if (s[i] == 'C' && s[j] == 'F') {
        cout << "Yes" << endl;
        exit(1);
      }
    }
  }
  cout << "No" << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, s) for (int i = 0; i < s; ++i)
#define all(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define VEC(a, n) vector<int> a(n)
#define PQ(a) priority_queue<int> a
#define PQmin(a) priority_queue<int, : vector<int>, greater<int>> a
#define PAIR pair<int, int>
/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main() {
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    for (int j = i + 1; j < s.size(); j++) {
      if (s[i] == 'C' && s[j] == 'F') {
        cout << "Yes" << endl;
        return 0;
      }
    }
  }
  cout << "No" << endl;

  return 0;
}
Problem p03957 (Python)

Buggy submission (status: Runtime Error):
s = input()
n = len(s)

f = 0
for i in range(s):
    if s[i] == "C" and f == 0:
        f += 1
    if s[i] == "F" and f == 1:
        print("Yes")
        exit()
print("No")


Fixed submission:
s = input()
n = len(s)

f = 0
for i in range(n):
    if s[i] == "C" and f == 0:
        f += 1
    if s[i] == "F" and f == 1:
        print("Yes")
        exit()
print("No")
Problem p03957 (C++)

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

int main() {
  string s;
  cin >> s;
  vector<int> ci;
  vector<int> fi;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'C')
      ci.push_back(i);
    if (s[i] == 'F')
      fi.push_back(i);
  }
  sort(ci.begin(), ci.end());
  sort(fi.begin(), fi.end());

  if (ci[0] > fi[fi.size() - 1])
    cout << "No" << endl;
  else
    cout << "Yes" << endl;

  return 0;
}


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

int main() {
  string s;
  cin >> s;
  vector<int> ci;
  vector<int> fi;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'C')
      ci.push_back(i);
    if (s[i] == 'F')
      fi.push_back(i);
  }
  sort(ci.begin(), ci.end());
  sort(fi.begin(), fi.end());

  if (ci.size() == 0 || fi.size() == 0)
    cout << "No" << endl;
  else if (ci[0] > fi[fi.size() - 1])
    cout << "No" << endl;
  else
    cout << "Yes" << endl;

  return 0;
}
Problem p03957 (C++)

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

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define RANGEBOX(x, y, maxX, maxY)                                             \
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define PB push_back

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVi;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;

int main() {
  ios::sync_with_stdio(false);

  string s;
  cin >> s;
  vector<char> v;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'C' || s[i] == 'F') {
      v.PB((char)s[i]);
    }
  }
  string ans = "No";
  for (int i = 0; i < v.size() - 1; i++) {
    if (v[i] == 'C' && v[i + 1] == 'F') {
      ans = "Yes";
    }
  }
  cout << ans << endl;
  return 0;
}


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

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define RANGEBOX(x, y, maxX, maxY)                                             \
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define PB push_back

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVi;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;

int main() {
  ios::sync_with_stdio(false);

  string s;
  cin >> s;
  vector<char> v;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'C' || s[i] == 'F') {
      v.PB((char)s[i]);
    }
  }
  string ans = "No";
  if (!v.size()) {
    cout << ans << endl;
    return 0;
  }
  for (int i = 0; i < v.size() - 1; i++) {
    if (v[i] == 'C' && v[i + 1] == 'F') {
      ans = "Yes";
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03957 (Python)

Buggy submission (status: Runtime Error):
s = input()
print("Yes" if 0 <= s.index("C") < s.rindex("F") else "No")


Fixed submission:
s = input()
print("Yes" if 0 <= s.find("C") < s.rfind("F") else "No")
Problem p03957 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#pragma warning(disable : 4996)

int main() {
  char str[101];
  scanf("%s", &str);
  int counter = 0;
  int makeCounter = 0;
  while (str[counter] != '\0') {
    if (makeCounter == 0) {
      if (str[counter] == 'C') {
        makeCounter++;
      }
    }
    if (makeCounter == 1) {
      if (str[counter] == 'F') {
        makeCounter++;
      }
    }
  }

  if (makeCounter == 2) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#pragma warning(disable : 4996)

int main() {
  char str[101];
  scanf("%s", &str);
  int counter = 0;
  int makeCounter = 0;
  while (str[counter] != '\0') {
    if (makeCounter == 0) {
      if (str[counter] == 'C') {
        makeCounter++;
      }
    }
    if (makeCounter == 1) {
      if (str[counter] == 'F') {
        makeCounter++;
      }
    }
    counter++;
  }

  if (makeCounter == 2) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
  return 0;
}
Problem p03958 (C++)

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

using namespace std;

int a[111];
int main() {
  int n, m;
  while (cin >> n >> m) {
    for (int i = 0; i < m; ++i) {
      cin >> a[i];
    }
    int ans = 0, last = -1;
    for (int k = 0; k < n; ++k) {
      int mx = 0, id = -1;
      for (int i = 0; i < n; ++i) {
        if (a[i] > mx && i != last) {
          mx = a[i];
          id = i;
        }
      }
      if (id != -1) {
        a[id]--;
        last = id;
      } else {
        a[last]--;
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

int a[111];
int main() {
  int n, m;
  while (cin >> n >> m) {
    for (int i = 0; i < m; ++i) {
      cin >> a[i];
    }
    int ans = 0, last = -1;
    for (int k = 0; k < n; ++k) {
      int mx = 0, id = -1;
      for (int i = 0; i < m; ++i) {
        if (a[i] > mx && i != last) {
          mx = a[i];
          id = i;
        }
      }
      if (id != -1) {
        a[id]--;
        last = id;
      } else {
        a[last]--;
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p03958 (C++)

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

typedef long long ll;

ll MOD = 1000000007;
ll INF = 100000000000000;
double PI = 3.1415926535;

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<ll>>;

int main() {
  int K, T;
  cin >> K >> T;
  vector<pair<int, int>> cake(T);
  for (int i = 0; i < T; i++) {
    cin >> cake[i].first;
    cake[i].second = i;
  }
  int ans = 0;
  int last = 0;
  for (int i = 0; i < K; i++) {
    if (i == 0) {
      sort(cake.begin(), cake.end());
      last = cake[T - 1].second;
      cake[T - 1].first--;
    } else {
      sort(cake.begin(), cake.end());
      if (last == cake[T - 1].second) {
        if (cake[T - 2].first > 0) {
          last = cake[T - 2].second;
          cake[T - 2].first--;
        } else {
          last = cake[T - 1].second;
          cake[T - 1].first--;
          ans++;
        }
      } else {
        last = cake[T - 1].second;
        cake[T - 1].first--;
      }
    }
  }
  cout << ans << endl;
}


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

typedef long long ll;

ll MOD = 1000000007;
ll INF = 100000000000000;
double PI = 3.1415926535;

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<ll>>;

int main() {
  int K, T;
  cin >> K >> T;
  vector<pair<int, int>> cake(T);
  for (int i = 0; i < T; i++) {
    cin >> cake[i].first;
    cake[i].second = i;
  }
  if (T == 1) {
    cout << K - 1 << endl;
    return 0;
  }
  int ans = 0;
  int last = 0;
  for (int i = 0; i < K; i++) {
    if (i == 0) {
      sort(cake.begin(), cake.end());
      last = cake[T - 1].second;
      cake[T - 1].first--;
    } else {
      sort(cake.begin(), cake.end());
      if (last == cake[T - 1].second) {
        if (cake[T - 2].first > 0) {
          last = cake[T - 2].second;
          cake[T - 2].first--;
        } else {
          last = cake[T - 1].second;
          cake[T - 1].first--;
          ans++;
        }
      } else {
        last = cake[T - 1].second;
        cake[T - 1].first--;
      }
    }
  }
  cout << ans << endl;
}
Problem p03958 (C++)

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

int main(void) {
  int i, k, t, max = 0, a;
  scanf("%d", &k, &t);
  for (i = 0; i < t; ++i) {
    scanf("%d", &a);
    if (a > max)
      max = a;
  }
  printf("%d", 2 * max - k - 1 > 0 ? 2 * max - k - 1 : 0);
  return 0;
}


Fixed submission:
#include <stdio.h>

int main(void) {
  int i, k, t, max = 0, a;
  scanf("%d%d", &k, &t);
  for (i = 0; i < t; ++i) {
    scanf("%d", &a);
    if (a > max)
      max = a;
  }
  printf("%d", 2 * max - k - 1 > 0 ? 2 * max - k - 1 : 0);
  return 0;
}
Problem p03958 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int k, t;
int a[123];
int big = 0;

int main(void) {
  cin >> k >> t;
  for (int i = 0; i < t; ++i) {
    cin >> a[i];
    big = max(a[i], big);
  }
  cout << (int)max((int)ceil((double)big / ((k - big) + 1)) - 1, 0) << endl;
  while ((int)max((int)ceil((double)big / ((k - big) + 1)) - 1, 0) == 1) {
    if (t < 20)
      break;
  }
  return 0;
}


Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int k, t;
int a[123];
int big = 0;

int main(void) {
  cin >> k >> t;
  for (int i = 0; i < t; ++i) {
    cin >> a[i];
    big = max(a[i], big);
  }
  cout << max(0, big - (k - big + 1)) << endl;
  return 0;
}
Problem p03958 (C++)

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

const ll MOD = 1e9 + 7;

int main() {
  int K, T;
  cin >> K >> T;
  priority_queue<pair<int, int>> Q;

  for (int i = 0; i < T; i++) {
    int q;
    cin >> q;
    Q.push(make_pair(q, i));
  }

  vector<int> simulator;

  while (!Q.empty()) {
    int p = Q.top().first;
    int q = Q.top().second;
    // qは種類、pはqの残りの数
    Q.pop();
    if (simulator.empty() || simulator.back() != q) {
      simulator.push_back(q);
      p--;
    } else {
      int r = Q.top().first;
      int s = Q.top().second;
      Q.pop();
      simulator.push_back(s);
      r--;
      if (r != 0) {
        Q.push(make_pair(r, s));
      }
    }
    if (p != 0) {
      Q.push(make_pair(p, q));
    }
  }
  int cnt = 0;
  for (int i = 1; i < (int)simulator.size(); i++) {
    if (simulator[i] == simulator[i - 1]) {
      cnt++;
    }
  }

  cout << cnt << endl;
  return 0;
}


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

const ll MOD = 1e9 + 7;

int main() {
  int K, T;
  cin >> K >> T;
  priority_queue<pair<int, int>> Q;

  for (int i = 0; i < T; i++) {
    int q;
    cin >> q;
    Q.push(make_pair(q, i));
  }

  vector<int> simulator;

  while (!Q.empty()) {
    int p = Q.top().first;
    int q = Q.top().second;
    // qは種類、pはqの残りの数
    Q.pop();
    if (simulator.empty() || simulator.back() != q || Q.empty()) {
      simulator.push_back(q);
      p--;
    } else {
      int r = Q.top().first;
      int s = Q.top().second;
      Q.pop();
      simulator.push_back(s);
      r--;
      if (r != 0) {
        Q.push(make_pair(r, s));
      }
    }
    if (p != 0) {
      Q.push(make_pair(p, q));
    }
  }
  int cnt = 0;
  for (int i = 1; i < (int)simulator.size(); i++) {
    if (simulator[i] == simulator[i - 1]) {
      cnt++;
    }
  }

  cout << cnt << endl;
  return 0;
}
Problem p03958 (Python)

Buggy submission (status: Runtime Error):
k, t = list(map(int, input().split()))
nums = list(map(int, input().split()))

m = max(nums)
rest = k - m
if max - rest - 1 > 0:
    print(max - rest - 1)
else:
    print(0)


Fixed submission:
k, t = list(map(int, input().split()))
nums = list(map(int, input().split()))

m = max(nums)
rest = k - m
if m - rest - 1 > 0:
    print(m - rest - 1)
else:
    print(0)
Problem p03958 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int K, T;
int a[110];

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

  cin >> K >> T;
  priority_queue<int> que;
  for (int i = 0; i < T; i++) {
    cin >> a[i];
    que.emplace(a[i]);
  }
  while (true) {
    if (que.size() == 1) {
      cout << que.top() - 1 << endl;
      return 0;
    }
    int a = que.top();
    que.pop();
    int b = que.top();
    que.pop();
    a -= b;
    if (a)
      que.emplace(a);
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int K, T;
int a[110];

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

  cin >> K >> T;
  priority_queue<int> que;
  for (int i = 0; i < T; i++) {
    cin >> a[i];
    que.emplace(a[i]);
  }
  while (true) {
    if (que.size() == 0) {
      cout << 0 << endl;
      return 0;
    }
    if (que.size() == 1) {
      cout << que.top() - 1 << endl;
      return 0;
    }
    int a = que.top();
    que.pop();
    int b = que.top();
    que.pop();
    a -= b;
    if (a)
      que.emplace(a);
  }

  return 0;
}
Problem p03958 (C++)

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

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define PI (acos(-1.0))
#define MODN 1000000007
#define MODN2 998244353
#define ALL(V) (V).begin(), (V).end()
#define INT_MAX_HALF (INT_MAX / 2)
#define EPS (1e-10)

using namespace std;
typedef long long ll;

int main() {

  int k, t;
  cin >> k >> t;

  vector<pair<int, int>> a(t);

  rep(i, t) {
    int tmp;
    cin >> tmp;

    a[i] = make_pair(tmp, i + 1);
  }

  sort(ALL(a));

  vector<int> eat(k);

  eat[0] = a[t - 1].second;
  a[t - 1].first = a[t - 1].first - 1;
  sort(ALL(a));

  for (int i = 1; i < k; i++) {

    if (eat[i - 1] == a[t - 1].second) {

      if (a[t - 2].first > 0) {
        eat[i] = a[t - 2].second;
        a[t - 2].first = a[t - 2].first - 1;
      } else {
        eat[i] = a[t - 1].second;
        a[t - 1].first = a[t - 1].first - 1;
      }
    } else {
      eat[i] = a[t - 1].second;
      a[t - 1].first = a[t - 1].first - 1;
    }
    sort(ALL(a));
  }

  /*
  rep(i, k){
      cerr << eat[i] << " ";
  }
  cerr << endl;
  */

  int ans = 0;

  rep(i, k - 1) {
    if (eat[i] == eat[i + 1])
      ans++;
  }

  cout << ans << endl;

  return 0;
}


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

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define PI (acos(-1.0))
#define MODN 1000000007
#define MODN2 998244353
#define ALL(V) (V).begin(), (V).end()
#define INT_MAX_HALF (INT_MAX / 2)
#define EPS (1e-10)

using namespace std;
typedef long long ll;

int main() {

  int k, t;
  cin >> k >> t;

  vector<pair<int, int>> a(t);

  rep(i, t) {
    int tmp;
    cin >> tmp;

    a[i] = make_pair(tmp, i + 1);
  }

  sort(ALL(a));

  vector<int> eat(k);

  eat[0] = a[t - 1].second;
  a[t - 1].first = a[t - 1].first - 1;
  sort(ALL(a));

  for (int i = 1; i < k; i++) {

    if (eat[i - 1] == a[t - 1].second) {

      if (t >= 2 && a[t - 2].first > 0) {
        eat[i] = a[t - 2].second;
        a[t - 2].first = a[t - 2].first - 1;
      } else {
        eat[i] = a[t - 1].second;
        a[t - 1].first = a[t - 1].first - 1;
      }
    } else {
      eat[i] = a[t - 1].second;
      a[t - 1].first = a[t - 1].first - 1;
    }
    sort(ALL(a));
  }

  /*
  rep(i, k){
      cerr << eat[i] << " ";
  }
  cerr << endl;
  */

  int ans = 0;

  rep(i, k - 1) {
    if (eat[i] == eat[i + 1])
      ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p03958 (C++)

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

void solve(long long K, long long T, std::vector<long long> &&as) {
  vector<ll> cakes(T);
  for (auto &&a : as) {
    cakes[a - 1]++;
  }

  auto cake_max = *max_element(begin(as), end(as));
  if (cake_max * 2 - K <= 1) {
    cout << 0 << endl;
  } else {
    cout << cake_max * 2 - K - 1 << endl;
  }
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
// clang-format off
int main(){
    long long K;
    scanf("%lld",&K);
    long long T;
    scanf("%lld",&T);
    std::vector<long long> a(T);
    for(int i = 0 ; i < T ; i++){
        scanf("%lld",&a[i]);
    }
    solve(K, T, std::move(a));
    return 0;
}


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

void solve(long long K, long long T, std::vector<long long> &&as) {
  auto cake_max = *max_element(begin(as), end(as));
  if (cake_max * 2 - K <= 1) {
    cout << 0 << endl;
  } else {
    cout << cake_max * 2 - K - 1 << endl;
  }
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
// clang-format off
int main(){
    long long K;
    scanf("%lld",&K);
    long long T;
    scanf("%lld",&T);
    std::vector<long long> a(T);
    for(int i = 0 ; i < T ; i++){
        scanf("%lld",&a[i]);
    }
    solve(K, T, std::move(a));
    return 0;
}
Problem p03958 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n, k;
  cin >> n >> k;
  priority_queue<ll> q;
  for (ll i = 0; i < k; i++) {
    ll now;
    cin >> now;
    q.push(now);
  }
  ll ans = 0;
  while (q.size() != 1) {
    ll next = q.top();
    next--;
    q.pop();
    ll nnext = q.top();
    nnext--;
    q.pop();
    if (nnext != 0) {
      q.push(nnext);
    }
    if (next != 0) {
      q.push(next);
    }
  }
  cout << q.top() - 1;
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n, k;
  cin >> n >> k;
  priority_queue<ll> q;
  for (ll i = 0; i < k; i++) {
    ll now;
    cin >> now;
    q.push(now);
  }
  ll ans = 0;
  while (q.size() > 1) {
    ll next = q.top();
    next--;
    q.pop();
    ll nnext = q.top();
    nnext--;
    q.pop();
    if (nnext != 0) {
      q.push(nnext);
    }
    if (next != 0) {
      q.push(next);
    }
  }
  cout << q.top() - 1;
  // your code goes here
  return 0;
}
Problem p03958 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
#define tl 1'000'000'007
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REREP(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, a, n) for (ll i = a; i < n; i++)
#define REFOR(i, n, a) for (ll i = n; i >= a; i--)
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll k, t;
  cin >> k >> t;
  priority_queue<int> que;
  REP(i, t) {
    ll x;
    cin >> x;
    que.push(x);
  }
  ll day = 0;
  ll cnt = 0;
  ll p = que.top() - 1;
  que.pop();
  REP(i, k) {
    ll q = que.top() - 1;
    que.pop();
    if (p)
      que.push(p);
    p = q;
    if (que.empty()) {
      cnt = p;
      break;
    }
  }
  cout << cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
#define tl 1'000'000'007
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REREP(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, a, n) for (ll i = a; i < n; i++)
#define REFOR(i, n, a) for (ll i = n; i >= a; i--)
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll k, t;
  cin >> k >> t;
  priority_queue<int> que;
  REP(i, t) {
    ll x;
    cin >> x;
    que.push(x);
  }
  ll day = 0;
  ll cnt = 0;
  ll p = que.top() - 1;
  que.pop();
  REP(i, k) {
    if (que.empty()) {
      cnt = p;
      break;
    }
    ll q = que.top() - 1;
    que.pop();
    if (p)
      que.push(p);
    p = q;
    if (que.empty()) {
      cnt = p;
      break;
    }
  }
  cout << cnt << endl;
}
Problem p03959 (C++)

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

int main() {
  long N;
  long long int T[100004] = {};
  long long int A[100004] = {};
  unsigned long long int count = 1;

  cin >> N;
  for (long i = 1; i <= N; i++) {
    cin >> T[i];
  }
  for (long i = 1; i <= N; i++) {
    cin >> A[i];
  }

  for (long i = 1; i <= N; i++) {
    assert(count <= 1000000000000);
    if (T[i] == T[i - 1]) {
      if (A[i] == A[i + 1]) {
        if (A[i] > T[i]) {
          count *= T[i];
        } else {
          count *= A[i];
        }
      } else {
        if (A[i] <= T[i]) {
          count *= 1;
        } else {
          count *= 0;
        }
      }
    } else {
      if (A[i] == A[i + 1]) {
        if (T[i] <= A[i]) {
          count *= 1;
        } else {
          count *= 0;
        }
      } else {
        if (T[i] == A[i]) {
          count *= 1;
        } else {
          count *= 0;
        }
      }
    }
  }

  cout << count % 1000000007 << "\n";

  return 0;
}

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

int main() {
  long N;
  long long int T[100004] = {};
  long long int A[100004] = {};
  unsigned long long int count = 1;

  cin >> N;
  for (long i = 1; i <= N; i++) {
    cin >> T[i];
  }
  for (long i = 1; i <= N; i++) {
    cin >> A[i];
  }

  for (long i = 1; i <= N; i++) {
    count = count % 1000000007;
    if (T[i] == T[i - 1]) {
      if (A[i] == A[i + 1]) {
        if (A[i] > T[i]) {
          count *= T[i];
        } else {
          count *= A[i];
        }
      } else {
        if (A[i] <= T[i]) {
          count *= 1;
        } else {
          count *= 0;
        }
      }
    } else {
      if (A[i] == A[i + 1]) {
        if (T[i] <= A[i]) {
          count *= 1;
        } else {
          count *= 0;
        }
      } else {
        if (T[i] == A[i]) {
          count *= 1;
        } else {
          count *= 0;
        }
      }
    }
  }

  cout << count % 1000000007 << "\n";

  return 0;
}
Problem p03959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;

int main(void) {
  ll n;
  cin >> n;
  vector<ll> t(n + 2, 0), a(n + 2, 0);
  rep(i, n) { cin >> t.at(i + 1); }
  rep(i, n) { cin >> a.at(i + 1); }
  t.at(n + 1) = t.at(n);
  a.at(0) = a.at(1);
  ll res = 1;
  ll max_now = 0;
  ll cur_from = 1;
  while (cur_from <= n + 1 && t[cur_from] != a[cur_from]) {
    if (t.at(cur_from - 1) < t.at(cur_from)) {
      max_now = t.at(cur_from);
    } else {
      res *= max_now;
      res %= MOD;
    }
    DEBUG { cout << res << endl; }
    cur_from++;
  }
  ll cur_to = n;
  max_now = 0;
  while (cur_to >= 0 && t[cur_to] != a[cur_to]) {
    if (a.at(cur_to) > a.at(cur_to + 1)) {
      max_now = a.at(cur_to);
    } else {
      res *= max_now;
      res %= MOD;
    }
    DEBUG { cout << res << " max : " << max_now << endl; }
    cur_to--;
  }
  if (t.at(n) != a.at(1) || t.at(cur_from) != t.at(n) ||
      a.at(1) != a.at(cur_to)) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = cur_from + 1; i < cur_to; i++) {
    res *= t.at(i);
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;

int main(void) {
  ll n;
  cin >> n;
  vector<ll> t(n + 2, 0), a(n + 2, 0);
  rep(i, n) { cin >> t.at(i + 1); }
  rep(i, n) { cin >> a.at(i + 1); }
  t.at(n + 1) = t.at(n);
  a.at(0) = a.at(1);
  ll res = 1;
  ll max_now = 0;
  ll cur_from = 1;
  while (cur_from <= n + 1 && t[cur_from] != a[cur_from]) {
    if (t.at(cur_from - 1) < t.at(cur_from)) {
      max_now = t.at(cur_from);
    } else {
      res *= max_now;
      res %= MOD;
    }
    DEBUG { cout << res << endl; }
    cur_from++;
  }
  ll cur_to = n;
  max_now = 0;
  while (cur_to >= 0 && t[cur_to] != a[cur_to]) {
    if (a.at(cur_to) > a.at(cur_to + 1)) {
      max_now = a.at(cur_to);
    } else {
      res *= max_now;
      res %= MOD;
    }
    DEBUG { cout << res << " max : " << max_now << endl; }
    cur_to--;
  }
  if (cur_from >= n + 1 || cur_to <= 0 || t.at(n) != a.at(1) ||
      t.at(cur_from) != t.at(n) || a.at(1) != a.at(cur_to)) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = cur_from + 1; i < cur_to; i++) {
    res *= t.at(i);
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}
Problem p03959 (C++)

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

const long long MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> t(n), a(n);
  for (int i = 0; i < n; i++) {
    cin >> t.at(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }

  if (n == 1) {
    if (t.at(0) != t.at(1)) {
      cout << 0 << endl;
      return 0;
    }
  }

  vector<int> h(n);
  h.at(0) = t.at(0);
  h.at(n - 1) = a.at(n - 1);
  for (int i = 1; i < n; i++) {
    if (t.at(i - 1) < t.at(i)) {
      if (h.at(i) != 0 && h.at(i) != t.at(i)) {
        cout << 0 << endl;
        return 0;
      }
      h.at(i) = t.at(i);
    }
  }
  for (int i = 1; i < n; i++) {
    if (a.at(i - 1) > a.at(i)) {
      if (h.at(i - 1) != 0 && h.at(i - 1) != a.at(i - 1)) {
        cout << 0 << endl;
        return 0;
      }
      h.at(i - 1) = a.at(i - 1);
    }
    if (h.at(i - 1) != 0 && h.at(i - 1) > a.at(i - 1)) {
      cout << 0 << endl;
      return 0;
    }
  }

  long long ans = 1;
  for (int i = 0; i < n; i++) {
    if (h.at(i) == 0) {
      ans *= (long long)min(t.at(i), a.at(i));
      ans %= MOD;
    }
  }

  cout << ans << endl;
}

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

const long long MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> t(n), a(n);
  for (int i = 0; i < n; i++) {
    cin >> t.at(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }

  if (n == 1) {
    if (t.at(0) != a.at(0)) {
      cout << 0 << endl;
      return 0;
    }
  }

  vector<int> h(n);
  h.at(0) = t.at(0);
  h.at(n - 1) = a.at(n - 1);
  for (int i = 1; i < n; i++) {
    if (t.at(i - 1) < t.at(i)) {
      if (h.at(i) != 0 && h.at(i) != t.at(i)) {
        cout << 0 << endl;
        return 0;
      }
      h.at(i) = t.at(i);
    }
  }
  for (int i = 1; i < n; i++) {
    if (a.at(i - 1) > a.at(i)) {
      if (h.at(i - 1) != 0 && h.at(i - 1) != a.at(i - 1)) {
        cout << 0 << endl;
        return 0;
      }
      h.at(i - 1) = a.at(i - 1);
    }
    if (h.at(i - 1) != 0 && h.at(i - 1) > a.at(i - 1)) {
      cout << 0 << endl;
      return 0;
    }
  }

  long long ans = 1;
  for (int i = 0; i < n; i++) {
    if (h.at(i) == 0) {
      ans *= (long long)min(t.at(i), a.at(i));
      ans %= MOD;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <time.h>
#include <vector>
#define MODD 1000000007
#define ll long long
using namespace std;

int T[11111];
int A[11111];

int main() {
  int N;
  cin >> N;
  T[0] = 0;
  for (int i = 1; i <= N; i++) {
    cin >> T[i];
  }

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

  ll ans = 1;
  for (int i = 1; i <= N; i++) {
    if (T[i] > T[i - 1] || A[i] > A[i + 1]) {
      int x = (T[i] > T[i - 1] ? T[i] : A[i]);

      if (x > T[i] || x > A[i])
        ans = 0;

    } else {
      ans = (ans * min(A[i], T[i])) % MODD;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <time.h>
#include <vector>
#define MODD 1000000007
#define ll long long
using namespace std;

int T[111111];
int A[111111];

int main() {
  int N;
  cin >> N;
  T[0] = 0;
  for (int i = 1; i <= N; i++) {
    cin >> T[i];
  }

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

  ll ans = 1;
  for (int i = 1; i <= N; i++) {
    if (T[i] > T[i - 1] || A[i] > A[i + 1]) {
      int x = (T[i] > T[i - 1] ? T[i] : A[i]);

      if (x > T[i] || x > A[i])
        ans = 0;

    } else {
      ans = (ans * min(A[i], T[i])) % MODD;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 510000;
const int MOD = 1000000007;
//

signed main() {
  int h, w, ans = 0;
  cin >> h >> w;
  string c[h];
  for (int i = 0; i < h; i++)
    cin >> c[i];
  for (int i = 1; i < w; i++) {
    int dp[h + 1][h + 1];
    for (int p = 0; p <= h; p++)
      for (int q = 0; q <= h; q++) {
        dp[p][q] = 0;
        for (int j = 1; h - j - p >= 0 && h - j - q >= 0; j++) {
          if (c[h - j - p][i - 1] == c[h - j - q][i])
            dp[p][q]++;
        }
      }
    for (int q = 1; q <= h; q++)
      dp[0][q] += dp[0][q - 1];
    for (int p = 1; p <= h; p++) {
      dp[p][0] += dp[p - 1][0];
      for (int q = 1; q <= h; q++)
        dp[p][q] += min(dp[p - 1][q], dp[p][q - 1]);
    }
    ans += dp[h][h];
    //  cerr<<dp[h][h]<<' ';
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 510000;
const int MOD = 1000000007;
//

signed main() {
  int h, w, ans = 0;
  cin >> h >> w;
  string c[h];
  for (int i = 0; i < h; i++)
    cin >> c[i];
  for (int i = 1; i < w; i++) {
    int dp[h + 1][h + 1];
    for (int p = h; p >= 0; p--)
      for (int q = h; q >= 0; q--) {
        if (p == h || q == h)
          dp[p][q] = 0;
        else
          dp[p][q] =
              dp[p + 1][q + 1] + (c[h - 1 - p][i - 1] == c[h - 1 - q][i]);
      }
    for (int q = 1; q <= h; q++)
      dp[0][q] += dp[0][q - 1];
    for (int p = 1; p <= h; p++) {
      dp[p][0] += dp[p - 1][0];
      for (int q = 1; q <= h; q++)
        dp[p][q] += min(dp[p - 1][q], dp[p][q - 1]);
    }
    ans += dp[h][h];
    //  cerr<<dp[h][h]<<' ';
  }
  cout << ans << endl;
}
Problem p03960 (C++)

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

const int MAXN = 305;
const int INF = 0x3f3f3f3f;

int N, M;
char mat[MAXN][MAXN];
string col[MAXN];
int trans[MAXN][MAXN];
int dp[2 * MAXN][MAXN];

void load() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; i++)
    scanf("%s", mat[i]);
}

void mn(int &ref, int val) { ref = min(ref, val); }

int calc(string a, string b) {
  memset(trans, 0, sizeof trans);
  for (int i = 0; i <= 2 * N; i++)
    for (int j = 0; j < N; j++) {
      int pos = j + i - N;
      if (pos >= 0 && pos < N)
        trans[i][j] = a[j] == b[pos];
      if (j)
        trans[i][j] += trans[i][j - 1];
    }

  memset(dp, INF, sizeof dp);
  dp[0][0] = 0;
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= N; j++) {
      if (!i && !j)
        continue;
      if (i)
        mn(dp[i][j], dp[i - 1][j]);
      if (j)
        mn(dp[i][j], dp[i][j - 1]);
      if (!i || !j)
        continue;
      int diff = i - j;
      int lo = max(0, diff);
      dp[i][j] += trans[N - diff][i - 1] - (lo ? trans[N - diff][lo - 1] : 0);
    }

  return dp[N][N];
}

int solve() {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      col[j] += mat[i][j];

  int sol = 0;
  for (int i = 1; i < M; i++)
    sol += calc(col[i], col[i - 1]);

  return sol;
}

int main() {
  load();
  printf("%d\n", solve());
  return 0;
}

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

const int MAXN = 305;
const int INF = 0x3f3f3f3f;

int N, M;
char mat[MAXN][MAXN];
string col[MAXN];
int trans[2 * MAXN][MAXN];
int dp[MAXN][MAXN];

void load() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; i++)
    scanf("%s", mat[i]);
}

void mn(int &ref, int val) { ref = min(ref, val); }

int calc(string a, string b) {
  memset(trans, 0, sizeof trans);
  for (int i = 0; i <= 2 * N; i++)
    for (int j = 0; j < N; j++) {
      int pos = j + i - N;
      if (pos >= 0 && pos < N)
        trans[i][j] = a[j] == b[pos];
      if (j)
        trans[i][j] += trans[i][j - 1];
    }

  memset(dp, INF, sizeof dp);
  dp[0][0] = 0;
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= N; j++) {
      if (!i && !j)
        continue;
      if (i)
        mn(dp[i][j], dp[i - 1][j]);
      if (j)
        mn(dp[i][j], dp[i][j - 1]);
      if (!i || !j)
        continue;
      int diff = i - j;
      int lo = max(0, diff);
      dp[i][j] += trans[N - diff][i - 1] - (lo ? trans[N - diff][lo - 1] : 0);
    }

  return dp[N][N];
}

int solve() {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      col[j] += mat[i][j];

  int sol = 0;
  for (int i = 1; i < M; i++)
    sol += calc(col[i], col[i - 1]);

  return sol;
}

int main() {
  load();
  printf("%d\n", solve());
  return 0;
}
Problem p03960 (C++)

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

using int64 = long long;

const int MAX_W = 301;
const int MAX_H = 301;
const int64 INF = (1LL << 50);

char C[MAX_W][MAX_H]; // C[i][j] :=
int cost[MAX_W][MAX_H][MAX_H];
int64 dp[MAX_H][MAX_H];

int H, W;

int calc_cost(int id, int LH, int RH) {
  if (cost[id][LH][RH] >= 0)
    return cost[id][LH][RH];
  if (LH == 0 or RH == 0)
    return cost[id][LH][RH] = 0;
  // どっちも1より大きい
  int idxl = H - LH + 1;
  int idxr = H - RH + 1;
  return calc_cost(id, LH - 1, RH - 1) + (C[id][idxl] == C[id + 1][idxr]);
}

int main() {
  cin >> H >> W;

  for (int r = 0; r < H; r++) {
    string s;
    cin >> s;
    for (int c = 0; c < W; c++)
      C[c][H - r] = s[c];
  }

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

  int64 ans = 0;
  for (int c = 0; c < W - 1; c++) {
    // dp
    fill((int64 *)dp, (int64 *)(dp + MAX_H), INF);

    dp[0][0] = 0;
    for (int LH = 0; LH <= H; LH++) {
      for (int RH = 0; RH <= H; RH++) {
        if (LH == 0 and RH == 0)
          continue;
        if (LH > 0)
          dp[LH][RH] = min(dp[LH][RH], dp[LH - 1][RH] + calc_cost(c, LH, RH));
        if (RH > 0)
          dp[LH][RH] = min(dp[LH][RH], dp[LH][RH - 1] + calc_cost(c, LH, RH));
      }
    }

    ans += dp[H][H];
    // cerr << "c: " << dp[H][H] << endl;
  }
  cout << ans << endl;

  /*
  cerr << "deg: " << calc_cost(1, 2, 2) << endl;
  cerr << "deg: " << calc_cost(1, 1, 1) << endl;
  */

  return 0;
}

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

using int64 = long long;

const int MAX_W = 301;
const int MAX_H = 301;
const int64 INF = (1LL << 50);

char C[MAX_W][MAX_H]; // C[i][j] :=
int cost[MAX_W][MAX_H][MAX_H];
int64 dp[MAX_H][MAX_H];

int H, W;

int calc_cost(int id, int LH, int RH) {
  if (cost[id][LH][RH] >= 0)
    return cost[id][LH][RH];
  if (LH == 0 or RH == 0)
    return cost[id][LH][RH] = 0;
  // どっちも1より大きい
  int idxl = H - LH + 1;
  int idxr = H - RH + 1;
  return cost[id][LH][RH] =
             calc_cost(id, LH - 1, RH - 1) + (C[id][idxl] == C[id + 1][idxr]);
}

int main() {
  cin >> H >> W;

  for (int r = 0; r < H; r++) {
    string s;
    cin >> s;
    for (int c = 0; c < W; c++)
      C[c][H - r] = s[c];
  }

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

  int64 ans = 0;
  for (int c = 0; c < W - 1; c++) {
    // dp
    fill((int64 *)dp, (int64 *)(dp + MAX_H), INF);

    dp[0][0] = 0;
    for (int LH = 0; LH <= H; LH++) {
      for (int RH = 0; RH <= H; RH++) {
        if (LH == 0 and RH == 0)
          continue;
        if (LH > 0)
          dp[LH][RH] = min(dp[LH][RH], dp[LH - 1][RH] + calc_cost(c, LH, RH));
        if (RH > 0)
          dp[LH][RH] = min(dp[LH][RH], dp[LH][RH - 1] + calc_cost(c, LH, RH));
      }
    }

    ans += dp[H][H];
    // cerr << "c: " << dp[H][H] << endl;
  }
  cout << ans << endl;

  /*
  cerr << "deg: " << calc_cost(1, 2, 2) << endl;
  cerr << "deg: " << calc_cost(1, 1, 1) << endl;
  */

  return 0;
}
Problem p03960 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

const int MAX = 333;
string board[MAX];
const int INF = 1e9;
int H, W;

int memo[MAX][MAX][MAX];
int dp[MAX][MAX];

int calc(int c, int l, int r) {
  int &ret = memo[c][l][r];
  if (ret >= 0)
    return ret;
  ret = 0;
  if (l < r) {
    for (int i = 0; i < l; i++) {
      if (board[i][c] == board[i + r - l][c + 1])
        ++ret;
    }
  } else {
    for (int i = 0; i < r; i++) {
      if (board[i][c + 1] == board[i + l - r][c])
        ++ret;
    }
  }
  return ret;
}

int dfs(int col, int lh, int rh) {
  int &ret = dp[lh][rh];
  if (ret >= 0)
    return ret;
  ret = INF;
  if (lh * rh == 0)
    return ret = 0;
  return ret = min(calc(col, lh, rh) + dfs(col, lh - 1, rh),
                   calc(col, lh, rh) + dfs(col, lh, rh - 1));
}

int solve(int col) {
  memset(dp, -1, sizeof(dp));
  return dfs(col, H, H);
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> board[i];
  }
  memset(memo, -1, sizeof(memo));
  int ans = 0;
  for (int i = 0; i < W - 1; i++) {
    ans += solve(i);
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

const int MAX = 333;
string board[MAX];
const int INF = 1e9;
int H, W;

int memo[MAX][MAX][MAX];
int dp[MAX][MAX];

int calc(int c, int l, int r) {
  int &ret = memo[c][l][r];
  if (ret >= 0)
    return ret;
  if (l * r == 0)
    return ret = 0;
  return ret = calc(c, l - 1, r - 1) + (board[l - 1][c] == board[r - 1][c + 1]);
}

int dfs(int col, int lh, int rh) {
  int &ret = dp[lh][rh];
  if (ret >= 0)
    return ret;
  ret = INF;
  if (lh * rh == 0)
    return ret = 0;
  return ret = min(calc(col, lh, rh) + dfs(col, lh - 1, rh),
                   calc(col, lh, rh) + dfs(col, lh, rh - 1));
}

int solve(int col) {
  memset(dp, -1, sizeof(dp));
  return dfs(col, H, H);
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> board[i];
  }
  memset(memo, -1, sizeof(memo));
  int ans = 0;
  for (int i = 0; i < W - 1; i++) {
    ans += solve(i);
  }
  cout << ans << endl;
  return 0;
}
Problem p03960 (C++)

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

using namespace std;

int H, W;
char map[305][305];
int cost[305][305][305];
int dp[305][305];

int main(void) {
  cin >> H >> W;
  for (int y = 1; y <= H; y++) {
    for (int x = 1; x <= W; x++) {
      cin >> map[x][y];
    }
  }

  for (int i = 1; i < W; i++) {
    for (int j = 0; j <= H; j++) {
      int cnt = 0;
      for (int k = 1; k <= H - j; k++) {
        if (map[i][j + k] == map[i + 1][k])
          cnt++;
      }
      for (int k = 0; k <= H - j; k++) {
        cost[i][H - k][H - j - k] = cnt;
        if (map[i][H - k] == map[i + 1][H - j - k])
          cnt--;
      }

      cnt = 0;
      for (int k = 1; k <= H - j; k++) {
        if (map[i + 1][j + k] == map[i][k])
          cnt++;
      }
      for (int k = 0; k <= H - j; k++) {
        cost[i][H - j - k][H - k] = cnt;
        if (map[i + 1][H - k] == map[i][H - j - k])
          cnt--;
      }
    }
  }

  int ans = 0;
  for (int i = 1; i < W; i++) {
    for (int j = H; j >= 0; j--) {
      for (int k = H; k >= 0; k--) {
        dp[j][k] = inf;
      }
    }
    dp[H][H] = 0;

    for (int j = H; j >= 0; j--) {
      for (int k = H; k >= 0; k--) {
        dp[j - 1][k] = min(dp[j - 1][k], dp[j][k] + cost[i][j][k]);
        dp[j][k - 1] = min(dp[j][k - 1], dp[j][k] + cost[i][j][k]);
      }
    }
    ans += dp[0][0];
  }

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

Fixed submission:
#include <iostream>
#define inf 1000000000

using namespace std;

int H, W;
char map[305][305];
int cost[305][305][305];
int dp[305][305];

int main(void) {
  cin >> H >> W;
  for (int y = 1; y <= H; y++) {
    for (int x = 1; x <= W; x++) {
      cin >> map[x][y];
    }
  }

  for (int i = 1; i < W; i++) {
    for (int j = 0; j <= H; j++) {
      int cnt = 0;
      for (int k = 1; k <= H - j; k++) {
        if (map[i][j + k] == map[i + 1][k])
          cnt++;
      }
      for (int k = 0; k <= H - j; k++) {
        cost[i][H - k][H - j - k] = cnt;
        if (map[i][H - k] == map[i + 1][H - j - k])
          cnt--;
      }

      cnt = 0;
      for (int k = 1; k <= H - j; k++) {
        if (map[i + 1][j + k] == map[i][k])
          cnt++;
      }
      for (int k = 0; k <= H - j; k++) {
        cost[i][H - j - k][H - k] = cnt;
        if (map[i + 1][H - k] == map[i][H - j - k])
          cnt--;
      }
    }
  }

  int ans = 0;
  for (int i = 1; i < W; i++) {
    for (int j = H; j >= 0; j--) {
      for (int k = H; k >= 0; k--) {
        dp[j][k] = inf;
      }
    }
    dp[H][H] = 0;

    for (int j = H; j >= 0; j--) {
      for (int k = H; k >= 0; k--) {
        if (j > 0)
          dp[j - 1][k] = min(dp[j - 1][k], dp[j][k] + cost[i][j][k]);
        if (k > 0)
          dp[j][k - 1] = min(dp[j][k - 1], dp[j][k] + cost[i][j][k]);
      }
    }
    ans += dp[0][0];
  }

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

Buggy submission (status: Time Limit Exceeded):
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 305;
char mp[N][N], s[N][N];
int f[N][N], n, m, res;
int calc(R int id) {
  memset(f, 0, sizeof(f));
  fp(i, 1, m) fp(j, 1, m) {
    f[i][j] = 0;
    for (R int x = i, y = j; x && y; --x, --y)
      f[i][j] += (s[id][x] == s[id + 1][y]);
  }
  fp(i, 1, m) fp(j, 1, m) f[i][j] += min(f[i - 1][j], f[i][j - 1]);
  return f[m][m];
}
int main() {
  //	freopen("testdata.in","r",stdin);
  scanf("%d%d", &n, &m);
  fp(i, 1, n) scanf("%s", mp[i] + 1);
  fp(i, 1, n) fp(j, 1, m) s[j][i] = mp[i][j];
  swap(n, m);
  fp(i, 1, n - 1) res += calc(i);
  printf("%d\n", res);
  return 0;
}

Fixed submission:
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 305;
char mp[N][N], s[N][N];
int f[N][N], n, m, res;
int calc(R int id) {
  fp(i, 1, m) fp(j, 1, m) f[i][j] = (s[id][i] == s[id + 1][j]);
  fp(i, 1, m) fp(j, 1, m) f[i][j] += f[i - 1][j - 1];
  fp(i, 1, m) fp(j, 1, m) f[i][j] += min(f[i - 1][j], f[i][j - 1]);
  return f[m][m];
}
int main() {
  //	freopen("testdata.in","r",stdin);
  scanf("%d%d", &n, &m);
  fp(i, 1, n) scanf("%s", mp[i] + 1);
  fp(i, 1, n) fp(j, 1, m) s[j][i] = mp[i][j];
  swap(n, m);
  fp(i, 1, n - 1) res += calc(i);
  printf("%d\n", res);
  return 0;
}