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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
const int INF = 2147483647;
const ll MOD = 1000000007;

using namespace std;

int dp1[3001][3000] = {};
int dp2[3002][3000] = {};

int main() {
  int n, t;
  cin >> n >> t;
  vector<pair<int, int>> ab(n);
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    ab[i + 1] = make_pair(a, b);
  }

  FOR(i, 1, n + 1) {
    FOR(j, 1, t) {
      if (j < ab[i].first) {
        dp1[i][j] = dp1[i - 1][j];
      } else {
        dp1[i][j] =
            max(dp1[i - 1][j], dp1[i - 1][j - ab[i].first] + ab[i].second);
      }
    }
  }

  for (int i = n; i >= 0; i--) {
    FOR(j, 1, t) {
      if (j < ab[i].first) {
        dp2[i][j] = dp2[i + 1][j];
      } else {
        dp2[i][j] =
            max(dp2[i + 1][j], dp2[i + 1][j - ab[i].first] + ab[i].second);
      }
    }
  }

  int ans = 0;
  FOR(i, 1, n + 1) {
    int tmp = 0;
    REP(j, t) { tmp = max(tmp, dp1[i - 1][j] + dp2[i + 1][t - j - 1]); }
    ans = max(ans, tmp + ab[i].second);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
const int INF = 2147483647;
const ll MOD = 1000000007;

using namespace std;

int dp1[3001][3000] = {};
int dp2[3002][3000] = {};

int main() {
  int n, t;
  cin >> n >> t;
  vector<pair<int, int>> ab(n + 1);
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    ab[i + 1] = make_pair(a, b);
  }

  FOR(i, 1, n + 1) {
    FOR(j, 1, t) {
      if (j < ab[i].first) {
        dp1[i][j] = dp1[i - 1][j];
      } else {
        dp1[i][j] =
            max(dp1[i - 1][j], dp1[i - 1][j - ab[i].first] + ab[i].second);
      }
    }
  }

  for (int i = n; i >= 0; i--) {
    FOR(j, 1, t) {
      if (j < ab[i].first) {
        dp2[i][j] = dp2[i + 1][j];
      } else {
        dp2[i][j] =
            max(dp2[i + 1][j], dp2[i + 1][j - ab[i].first] + ab[i].second);
      }
    }
  }

  int ans = 0;
  FOR(i, 1, n + 1) {
    int tmp = 0;
    REP(j, t) { tmp = max(tmp, dp1[i - 1][j] + dp2[i + 1][t - j - 1]); }
    ans = max(ans, tmp + ab[i].second);
  }
  cout << ans << endl;
}
Problem p02863 (C++)

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

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

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

  vector<vector<int>> dp(N + 1, vector<int>(T, 0));
  vector<vector<int>> ep(N + 1, vector<int>(T, 0));
  int a, b;
  for (int i = 0; i < N; i++) {
    a = A[i];
    b = B[i];
    for (int j = 0; j < T; j++) {
      if (dp[i + 1][j] + ep[i + 1][j] < dp[i][j] + ep[i][j]) {
        dp[i + 1][a + j] = dp[i][j];
        ep[i + 1][a + j] = ep[i][j];
      }

      if (dp[i + 1][j] + ep[i + 1][j] < dp[i][j] + b) {
        dp[i + 1][j] = dp[i][j];
        ep[i + 1][j] = b;
      }

      if (a + j < T) {
        dp[i + 1][a + j] = dp[i][j] + b;
        ep[i + 1][a + j] = ep[i][j];
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < T; i++) {
    ans = max(ans, dp.back()[i] + ep.back()[i]);
  }
  cout << ans << endl;
}


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

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

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

  vector<vector<int>> dp(N + 1, vector<int>(T, 0));
  vector<vector<int>> ep(N + 1, vector<int>(T, 0));
  int a, b;
  for (int i = 0; i < N; i++) {
    a = A[i];
    b = B[i];
    for (int j = 0; j < T; j++) {
      if (dp[i + 1][j] + ep[i + 1][j] < dp[i][j] + ep[i][j]) {
        dp[i + 1][j] = dp[i][j];
        ep[i + 1][j] = ep[i][j];
      }

      if (dp[i + 1][j] + ep[i + 1][j] < dp[i][j] + b) {
        dp[i + 1][j] = dp[i][j];
        ep[i + 1][j] = b;
      }

      if (a + j < T) {
        dp[i + 1][a + j] = dp[i][j] + b;
        ep[i + 1][a + j] = ep[i][j];
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < T; i++) {
    ans = max(ans, dp.back()[i] + ep.back()[i]);
  }
  cout << ans << endl;
}
Problem p02863 (C++)

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

using namespace std;

int main() {
  int n, t;
  cin >> n >> t;

  vector<pair<int, int>> ab(n);

  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    ab[i] = make_pair(a, b);
  }

  sort(ab.begin(), ab.end(), [](pair<int, int> lhs, pair<int, int> rhs) {
    return lhs.first < rhs.first;
  });

  vector<vector<int>> dp(n, vector<int>(t + 1, 0));

  dp[0][ab[0].first] = ab[0].second;
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < t; j++) {
      dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
      if (j + ab[i + 1].first < t) {
        int aip1 = ab[i + 1].first;
        int bip1 = ab[i + 1].second;
        dp[i + 1][j + aip1] = max(dp[i][j] + bip1, dp[i + 1][j + aip1]);
      }
    }
  }

  int max_b = 0;
  for (int i = 0; i < n - 1; i++) {
    int max_dp = 0;
    for (int j = 0; j < t; j++) {
      max_dp = max(max_dp, dp[i][j]);
    }
    max_dp += ab[i + 1].second;
    max_b = max(max_b, max_dp);
  }

  cout << max_b << endl;

  return 0;
}

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

using namespace std;

int main() {
  int n, t;
  cin >> n >> t;

  vector<pair<int, int>> ab(n);

  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    ab[i] = make_pair(a, b);
  }

  sort(ab.begin(), ab.end(), [](pair<int, int> lhs, pair<int, int> rhs) {
    return lhs.first < rhs.first;
  });

  vector<vector<int>> dp(n, vector<int>(t + 10, 0));

  dp[0][ab[0].first] = ab[0].second;
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < t; j++) {
      dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
      if (j + ab[i + 1].first < t) {
        int aip1 = ab[i + 1].first;
        int bip1 = ab[i + 1].second;
        dp[i + 1][j + aip1] = max(dp[i][j] + bip1, dp[i + 1][j + aip1]);
      }
    }
  }

  int max_b = 0;
  for (int i = 0; i < n - 1; i++) {
    int max_dp = 0;
    for (int j = 0; j < t; j++) {
      max_dp = max(max_dp, dp[i][j]);
    }
    max_dp += ab[i + 1].second;
    max_b = max(max_b, max_dp);
  }

  cout << max_b << endl;

  return 0;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define xmod (int)(1e9 + 7)
using namespace std;

int n, t, dp[3005], ans;
pii a[3005];
bool used[3005];
int main() {
  scanf("%d%d", &n, &t);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].x, &a[i].y);
  }
  sort(a + 1, a + 1 + n);
  used[0] = true;
  for (int i = 1; i <= n; i++) {
    for (int j = t; j >= 0; j--) {
      if (j + a[i].x > t && used[j] && j != t) {
        used[t] = true;
        dp[t] = max(dp[t], dp[j] + a[i].y);
      } else if (used[j]) {
        used[j + a[i].x] = true;
        dp[j + a[i].x] = max(dp[j + a[i].x], dp[j] + a[i].y);
      }
    }
  }
  for (int i = 1; i <= t; i++) {
    ans = max(ans, dp[i]);
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define xmod (int)(1e9 + 7)
using namespace std;

int n, t, dp[3005], ans;
pii a[3005];
bool used[3005];
int main() {
  scanf("%d%d", &n, &t);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].x, &a[i].y);
  }
  sort(a + 1, a + 1 + n);
  used[0] = true;
  for (int i = 1; i <= n; i++) {
    for (int j = t; j >= 0; j--) {
      if (j + a[i].x > t && used[j] && j != t) {
        used[t] = true;
        dp[t] = max(dp[t], dp[j] + a[i].y);
      } else if (j + a[i].x <= t && used[j]) {
        used[j + a[i].x] = true;
        dp[j + a[i].x] = max(dp[j + a[i].x], dp[j] + a[i].y);
      }
    }
  }
  for (int i = 1; i <= t; i++) {
    ans = max(ans, dp[i]);
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02864 (C++)

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

using namespace std;

int n;

const int N = 300;
const int K = N;
const int H = 1e9;
const long long LLINF = N * 1LL * H + 7;

long long dp[N + 7][K + 7];
int h[N + 7];

long long dpf(int i, int k) {
  long long &d = dp[i][k];

  if (d != -1) {
    return d;
  }

  if (i > n) {
    return d = 0;
  }

  d = LLINF;

  for (int j = i; j <= i + k; j++) {
    d = min(d, max(0, h[j + 1] - h[i]) + dpf(j + 1, k - (j - i)));
  }

  return d;
}

int main() {
  memset(dp, -1, sizeof dp);

  int k;
  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }

  cout << dpf(0, k) << "\n";

  return 0;
}

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

using namespace std;

int n;

const int N = 300;
const int K = N;
const int H = 1e9;
const long long LLINF = N * 1LL * H + 7;

long long dp[N + 7][K + 7];
int h[N + 7];

long long dpf(int i, int k) {
  long long &d = dp[i][k];

  if (d != -1) {
    return d;
  }

  if (i > n) {
    return d = 0;
  }

  d = LLINF;

  for (int j = i; j <= min(n, i + k); j++) {
    d = min(d, max(0, h[j + 1] - h[i]) + dpf(j + 1, k - (j - i)));
  }

  return d;
}

int main() {
  memset(dp, -1, sizeof dp);

  int k;
  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }

  cout << dpf(0, k) << "\n";

  return 0;
}
Problem p02864 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 305 + 5;
int N, K, a[MN];
ll dp[MN][MN];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  memset(dp, 0x3f, sizeof(dp));
  cin >> N >> K;
  for (int i = 1; i <= N; i++)
    cin >> a[i];
  for (int k = 0; k <= K; k++)
    dp[0][k] = 0;
  for (int i = 1; i <= N + 1; i++) {
    for (int k = 0; k <= K; k++) {
      for (int j = 0; j <= k; j++) {
        dp[i][k] =
            min(dp[i][k], dp[i - 1 - j][k - j] + max(0, a[i] - a[i - 1 - j]));
      }
    }
  }
  cout << *min_element(dp[N + 1], dp[N + 1] + K + 1) << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 305 + 5;
int N, K, a[MN];
ll dp[MN][MN];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  memset(dp, 0x3f, sizeof(dp));
  cin >> N >> K;
  for (int i = 1; i <= N; i++)
    cin >> a[i];
  for (int k = 0; k <= K; k++)
    dp[0][k] = 0;
  for (int i = 1; i <= N + 1; i++) {
    for (int k = 0; k <= min(i - 1, K); k++) {
      for (int j = 0; j <= k; j++) {
        dp[i][k] =
            min(dp[i][k], dp[i - 1 - j][k - j] + max(0, a[i] - a[i - 1 - j]));
      }
    }
  }
  cout << *min_element(dp[N + 1], dp[N + 1] + K + 1) << '\n';
  return 0;
}
Problem p02864 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[505][505];
int n, k, h[305], b[305], he[305][305];
int main() {
  scanf("%d%d", &n, &k);
  memset(dp, 0x7f, sizeof(dp));
  ll ans = dp[0][0];
  int i, j;
  for (i = 1; i <= n; i++) {
    scanf("%d", &h[i]);
    for (j = 0; j <= k; j++)
      he[i][j] = h[i];
    // b[i]=h[i];
  }
  // sort(b+1,b+n+1);
  for (i = 0; i <= k; i++)
    dp[0][i] = 0;
  //	for(i=1;i<=k;i++) dp[i][i]=0;
  /*	for(i=1;i<=n;i++)
            for(j=0;j<=k;j++){
              if(h[i]>he[i-1][j])
     dp[i][j]=min(dp[i][j],dp[i-1][j]+h[i]-he[i-1][j]); else
     dp[i][j]=min(dp[i][j],dp[i-1][j]); if(dp[i][j]>dp[i-1][j-1]&&j>=1){
                  dp[i][j]=dp[i-1][j-1];
                  he[i][j]=he[i-1][j-1];
                  }
            }
          for(i=1;i<=n;i++){
                   for(j=0;j<=k;j++) printf("%d ",dp[i][j]);
                   printf("\n");
          }

          cout<<dp[n][k];*/
  for (i = 1; i <= n + 1; i++)
    for (j = 0; j <= k; j++)
      for (int ii = 0; ii <= j; ii++)
        if (h[i] > h[i - ii - 1])
          dp[i][j] =
              min(dp[i][j], dp[i - ii - 1][j - ii] + h[i] - h[i - ii - 1]);
        else
          dp[i][j] = min(dp[i][j], dp[i - ii - 1][j - ii]);
  for (i = 0; i <= k; i++)
    ans = min(ans, dp[n + 1][i]);
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[505][505];
int n, k, h[305], b[305], he[305][305];
int main() {
  scanf("%d%d", &n, &k);
  memset(dp, 0x7f, sizeof(dp));
  ll ans = dp[0][0];
  int i, j;
  for (i = 1; i <= n; i++) {
    scanf("%d", &h[i]);
    for (j = 0; j <= k; j++)
      he[i][j] = h[i];
    // b[i]=h[i];
  }
  // sort(b+1,b+n+1);
  for (i = 0; i <= k; i++)
    dp[0][i] = 0;
  //	for(i=1;i<=k;i++) dp[i][i]=0;
  /*	for(i=1;i<=n;i++)
            for(j=0;j<=k;j++){
              if(h[i]>he[i-1][j])
     dp[i][j]=min(dp[i][j],dp[i-1][j]+h[i]-he[i-1][j]); else
     dp[i][j]=min(dp[i][j],dp[i-1][j]); if(dp[i][j]>dp[i-1][j-1]&&j>=1){
                  dp[i][j]=dp[i-1][j-1];
                  he[i][j]=he[i-1][j-1];
                  }
            }
          for(i=1;i<=n;i++){
                   for(j=0;j<=k;j++) printf("%d ",dp[i][j]);
                   printf("\n");
          }

          cout<<dp[n][k];*/
  for (i = 1; i <= n + 1; i++)
    for (j = 0; j <= min(k, i - 1); j++)
      for (int ii = 0; ii <= j; ii++)
        if (h[i] > h[i - ii - 1])
          dp[i][j] =
              min(dp[i][j], dp[i - ii - 1][j - ii] + h[i] - h[i - ii - 1]);
        else
          dp[i][j] = min(dp[i][j], dp[i - ii - 1][j - ii]);
  for (i = 0; i <= k; i++)
    ans = min(ans, dp[n + 1][i]);
  cout << ans;
  return 0;
}
Problem p02864 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 301;
ll dp[MAXN][MAXN], h[MAXN];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &h[i]);
  n++;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= k; j++) {
      dp[i][j] = 1e16;
      for (int x = 0; x < i; x++)
        if (j - (i - x - 1) >= 0)
          dp[i][j] =
              min(dp[i][j], dp[x][j - (i - x - 1)] + max(0ll, h[i] - h[x]));
    }

  ll ans = 1e16;
  for (int i = 0; i <= k; i++)
    ans = min(ans, dp[n][i]);
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 302;
ll dp[MAXN][MAXN], h[MAXN];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &h[i]);
  n++;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= k; j++) {
      dp[i][j] = 1e16;
      for (int x = 0; x < i; x++)
        if (j - (i - x - 1) >= 0)
          dp[i][j] =
              min(dp[i][j], dp[x][j - (i - x - 1)] + max(0ll, h[i] - h[x]));
    }

  ll ans = 1e16;
  for (int i = 0; i <= k; i++)
    ans = min(ans, dp[n][i]);
  printf("%lld\n", ans);
  return 0;
}
Problem p02864 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
using lint = long long;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  k = n - k;
  VV<lint> dp(k + 1, V<lint>(n, 1e18));
  V<> h(n);
  REP(i, n) {
    cin >> h[i];
    for (int x = k; x >= 2; --x)
      REP(j, i) {
        dp[x][i] =
            min(dp[x][i], dp[x - 1][j] + (h[j] < h[i] ? h[i] - h[j] : 0));
      }
    dp[1][i] = h[i];
  }
  cout << *min_element(begin(dp[k]), end(dp[k])) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
using lint = long long;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  k = n - k;
  if (k == 0)
    return cout << 0 << endl, 0;
  VV<lint> dp(k + 2, V<lint>(n, 1e18));
  V<> h(n);
  REP(i, n) {
    cin >> h[i];
    for (int x = k; x >= 2; --x)
      REP(j, i) {
        dp[x][i] =
            min(dp[x][i], dp[x - 1][j] + (h[j] < h[i] ? h[i] - h[j] : 0));
      }
    dp[1][i] = h[i];
  }
  cout << *min_element(begin(dp[k]), end(dp[k])) << endl;
}
Problem p02864 (C++)

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

typedef long long ll;

const int MAX_N = 3e2 + 10;
const ll INF = 1e18 + 10;

ll dp[MAX_N][MAX_N][MAX_N], H[MAX_N];
int N, K, M;

int main() {
  for (int i = 0; i < MAX_N; i++)
    for (int j = 0; j < MAX_N; j++)
      for (int k = 0; k < MAX_N; k++)
        dp[i][j][k] = INF;
  scanf("%d%d", &N, &K);
  M = N;
  for (int i = 0; i < N; i++)
    scanf("%d", &H[i]);
  bool flag = false;
  for (int i = 0; i < N; i++)
    if (H[i] == 0)
      flag = true;
  if (!flag)
    H[M++] = 0;
  //	for(int i=0;i<M;i++) printf("%d ",H[i]); printf("\n");
  dp[0][0][0] = H[0];
  if (K > 0)
    for (int i = 1; i <= N; i++)
      dp[0][1][i] = H[i];
  for (int i = 1; i < N; i++) {
    dp[i][0][i] = min(dp[i][0][i], dp[i - 1][0][i - 1] +
                                       (H[i] > H[i - 1] ? H[i] - H[i - 1] : 0));
    for (int j = 1; j <= K; j++) {
      for (int k = 0; k < M; k++) {
        //	cout<<dp[i][j][k]<<endl;
        if (H[i] == H[k]) {
          for (int fr = 0; fr < M; fr++)
            dp[i][j][k] =
                min(dp[i][j][k],
                    dp[i - 1][j][fr] + (H[i] > H[fr] ? H[i] - H[fr] : 0));
        } else {
          dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - 1][k]);
          //	dp[i][j][k]=min(dp[i][j][k],dp[i-1][j-1][i-1]);
        }
        //	cout<<dp[i][j][k]<<endl;
      }
    }
  }
  ll ans = INF;
  for (int i = 0; i <= K; i++)
    for (int j = 0; j < M; j++)
      ans = min(ans, dp[N - 1][i][j]);
  printf("%lld\n", ans);
  return 0;
}

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

typedef long long ll;

const int MAX_N = 3e2 + 10;
const ll INF = 1e18 + 10;

ll dp[MAX_N][MAX_N][MAX_N], H[MAX_N];
int N, K, M;

int main() {
  for (int i = 0; i < MAX_N; i++)
    for (int j = 0; j < MAX_N; j++)
      for (int k = 0; k < MAX_N; k++)
        dp[i][j][k] = INF;
  scanf("%d%d", &N, &K);
  M = N;
  for (int i = 0; i < N; i++)
    scanf("%d", &H[i]);
  bool flag = false;
  for (int i = 0; i < N; i++)
    if (H[i] == 0)
      flag = true;
  if (!flag)
    H[M++] = 0;
  //	for(int i=0;i<M;i++) printf("%d ",H[i]); printf("\n");
  dp[0][0][0] = H[0];
  if (K > 0)
    for (int i = 1; i <= N; i++)
      dp[0][1][i] = H[i];
  for (int i = 1; i < N; i++) {
    dp[i][0][i] = min(dp[i][0][i], dp[i - 1][0][i - 1] +
                                       (H[i] > H[i - 1] ? H[i] - H[i - 1] : 0));
    for (int j = 1; j <= K; j++) {
      for (int k = 0; k < M; k++) {
        //	cout<<dp[i][j][k]<<endl;
        if (i == k) {
          for (int fr = 0; fr < M; fr++)
            dp[i][j][k] =
                min(dp[i][j][k],
                    dp[i - 1][j][fr] + (H[i] > H[fr] ? H[i] - H[fr] : 0));
        } else {
          dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - 1][k]);
          //	dp[i][j][k]=min(dp[i][j][k],dp[i-1][j-1][i-1]);
        }
        //	cout<<dp[i][j][k]<<endl;
      }
    }
  }
  ll ans = INF;
  for (int i = 0; i <= K; i++)
    for (int j = 0; j < M; j++)
      ans = min(ans, dp[N - 1][i][j]);
  printf("%lld\n", ans);
  return 0;
}
Problem p02864 (C++)

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

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = int(a); i < int(b); i++)
#define forabr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define all(X) X.begin(), X.end()

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;

const int N = 301;
const ll INF = 1e18;
ll dp[N][N][N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  n++;
  vector<ll> a(n + 1);
  forab(i, 1, n) cin >> a[i];
  forn(i, N) forn(j, N) forn(l, N) dp[i][j][l] = INF;
  dp[1][0][0] = 0;
  forab(i, 1, n) {
    forn(j, k + 1) {
      forn(l, i) {
        dp[i + 1][j][i] =
            min(dp[i + 1][j][i], dp[i][j][l] + max(a[i] - a[l], 0ll));
        if (j < k) {
          dp[i + 1][j + 1][l] = min(dp[i + 1][j + 1][l], dp[i][j][l]);
        }
      }
    }
  }
  ll ans = INF;
  forn(j, k + 1) forn(l, n) ans = min(ans, dp[n][j][l]);
  cout << ans << "\n";
  return 0;
}

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

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = int(a); i < int(b); i++)
#define forabr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define all(X) X.begin(), X.end()

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;

const int N = 310;
const ll INF = 1e18;
ll dp[N][N][N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  n++;
  vector<ll> a(n + 1);
  forab(i, 1, n) cin >> a[i];
  forn(i, N) forn(j, N) forn(l, N) dp[i][j][l] = INF;
  dp[1][0][0] = 0;
  forab(i, 1, n) {
    forn(j, k + 1) {
      forn(l, i) {
        dp[i + 1][j][i] =
            min(dp[i + 1][j][i], dp[i][j][l] + max(a[i] - a[l], 0ll));
        if (j < k) {
          dp[i + 1][j + 1][l] = min(dp[i + 1][j + 1][l], dp[i][j][l]);
        }
      }
    }
  }
  ll ans = INF;
  forn(j, k + 1) forn(l, n) ans = min(ans, dp[n][j][l]);
  cout << ans << "\n";
  return 0;
}
Problem p02864 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using std::min;
int n, m, a[205];
long long dp[305][305][305], ans;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= i; j++)
      for (int k = 0; k <= m; k++) {
        if (dp[i][j][k] > 10000000000000)
          continue;
        dp[i + 1][j][k + 1] = min(dp[i + 1][j][k + 1], dp[i][j][k]);
        dp[i + 1][i + 1][k] = min(dp[i + 1][i + 1][k],
                                  dp[i][j][k] + std::max(0, a[i + 1] - a[j]));
      }
  ans = 1000000000000;
  for (int i = 0; i <= m; i++)
    for (int j = 0; j <= n; j++)
      ans = min(ans, dp[n][j][i]);
  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using std::min;
int n, m, a[305];
long long dp[305][305][305], ans;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= i; j++)
      for (int k = 0; k <= m; k++) {
        if (dp[i][j][k] > 10000000000000)
          continue;
        dp[i + 1][j][k + 1] = min(dp[i + 1][j][k + 1], dp[i][j][k]);
        dp[i + 1][i + 1][k] = min(dp[i + 1][i + 1][k],
                                  dp[i][j][k] + std::max(0, a[i + 1] - a[j]));
      }
  ans = 1000000000000;
  for (int i = 0; i <= m; i++)
    for (int j = 0; j <= n; j++)
      ans = min(ans, dp[n][j][i]);
  printf("%lld\n", ans);
}
Problem p02864 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int N, K;
int H[300];
ll dp[301][301];

int main() {
  cin >> N >> K;
  REP(i, N) scanf("%d", H + i + 1);

  fill(dp[0], dp[N + 1], longINF);
  dp[0][0] = 0;
  REP(i, N) REP(j, N - K + 1) {
    if (dp[i][j] == longINF)
      continue;
    FOR(k, i + 1, N + 1) {
      chmin(dp[k][j + 1], dp[i][j] + max(0, H[k] - H[i]));
    }
  }

  ll ans = longINF;
  REP(i, N + 1) chmin(ans, dp[i][N - K]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int N, K;
int H[301];
ll dp[301][301];

int main() {
  cin >> N >> K;
  REP(i, N) scanf("%d", H + i + 1);

  fill(dp[0], dp[N + 1], longINF);
  dp[0][0] = 0;
  REP(i, N) REP(j, N - K + 1) {
    if (dp[i][j] == longINF)
      continue;
    FOR(k, i + 1, N + 1) {
      chmin(dp[k][j + 1], dp[i][j] + max(0, H[k] - H[i]));
    }
  }

  ll ans = longINF;
  REP(i, N + 1) chmin(ans, dp[i][N - K]);
  cout << ans << endl;
  return 0;
}
Problem p02864 (C++)

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

using namespace std;

long long D[301][301], H[302];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> H[i];
  }
  H[n + 1] = 0;
  for (int i = 0; i <= k; i++) {
    D[0][i] = 0;
  }
  for (int i = 1; i <= n + 1; i++) {
    D[i][0] = D[i - 1][0] + max(0ll, H[i] - H[i - 1]);
    for (int y = 1; y <= k; y++) {
      D[i][y] = D[i - 1][y] + max(0ll, H[i] - H[i - 1]);
      for (int u = 1; u <= y; u++) {
        if (i - u >= 0) {
          D[i][y] =
              min(D[i][y], D[i - u - 1][y - u] + max(0ll, H[i] - H[i - u - 1]));
        }
      }
    }
  }
  cout << D[n + 1][k];
  return 0;
}


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

using namespace std;

long long D[302][301], H[302];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> H[i];
  }
  H[n + 1] = 0;
  for (int i = 0; i <= k; i++) {
    D[0][i] = 0;
  }
  for (int i = 1; i <= n + 1; i++) {
    D[i][0] = D[i - 1][0] + max(0ll, H[i] - H[i - 1]);
    for (int y = 1; y <= k; y++) {
      D[i][y] = D[i - 1][y] + max(0ll, H[i] - H[i - 1]);
      for (int u = 1; u <= y; u++) {
        if (i - u >= 0) {
          D[i][y] =
              min(D[i][y], D[i - u - 1][y - u] + max(0ll, H[i] - H[i - u - 1]));
        }
      }
    }
  }
  cout << D[n + 1][k];
  return 0;
}
Problem p02864 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int BUF = 305;
const long long INF = 1LL << 62;

int nCol, nCut;
int col2height[BUF];

void read() {
  cin >> nCol >> nCut;
  for (int i = 0; i < nCol; ++i) {
    cin >> col2height[i];
  }
}

long long rec(int idx, int remainCut, int heightId, long long dp[BUF][BUF][BUF],
              vector<int> &id2height, map<int, int> height2id) {

  if (idx == nCol)
    return 0;

  long long &ret = dp[idx][remainCut][heightId];
  if (ret != -1)
    return ret;

  ret = INF;

  // cut
  if (remainCut > 0) {
    ret = min(ret,
              rec(idx + 1, remainCut - 1, heightId, dp, id2height, height2id));
  }

  // not cut
  ret = min(ret, rec(idx + 1, remainCut, height2id[col2height[idx]], dp,
                     id2height, height2id) +
                     +max(0, col2height[idx] - id2height[heightId]));

  return ret;
}

void work() {
  vector<int> id2height;
  for (int i = 0; i < nCol; ++i) {
    id2height.push_back(col2height[i]);
  }
  id2height.push_back(0);
  sort(id2height.begin(), id2height.end());
  id2height.erase(unique(id2height.begin(), id2height.end()), id2height.end());

  map<int, int> height2id;
  for (int i = 0; i < id2height.size(); ++i) {
    height2id[id2height[i]] = i;
  }

  static long long dp[BUF][BUF][BUF];
  memset(dp, -1, sizeof(dp));

  cout << rec(0, nCut, 0, dp, id2height, height2id) << endl;
}

int main() {
  read();
  work();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int BUF = 305;
const long long INF = 1LL << 62;

int nCol, nCut;
int col2height[BUF];

void read() {
  cin >> nCol >> nCut;
  for (int i = 0; i < nCol; ++i) {
    cin >> col2height[i];
  }
}

long long rec(int idx, int remainCut, int heightId, long long dp[BUF][BUF][BUF],
              vector<int> &id2height, map<int, int> &height2id) {

  if (idx == nCol)
    return 0;

  long long &ret = dp[idx][remainCut][heightId];
  if (ret != -1)
    return ret;

  ret = INF;

  // cut
  if (remainCut > 0) {
    ret = min(ret,
              rec(idx + 1, remainCut - 1, heightId, dp, id2height, height2id));
  }

  // not cut
  ret = min(ret, rec(idx + 1, remainCut, height2id[col2height[idx]], dp,
                     id2height, height2id) +
                     +max(0, col2height[idx] - id2height[heightId]));

  return ret;
}

void work() {
  vector<int> id2height;
  for (int i = 0; i < nCol; ++i) {
    id2height.push_back(col2height[i]);
  }
  id2height.push_back(0);
  sort(id2height.begin(), id2height.end());
  id2height.erase(unique(id2height.begin(), id2height.end()), id2height.end());

  map<int, int> height2id;
  for (int i = 0; i < id2height.size(); ++i) {
    height2id[id2height[i]] = i;
  }

  static long long dp[BUF][BUF][BUF];
  memset(dp, -1, sizeof(dp));

  cout << rec(0, nCut, 0, dp, id2height, height2id) << endl;
}

int main() {
  read();
  work();
  return 0;
}
Problem p02865 (C++)

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

using namespace std;

int main() {
  int N, M = 0;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      if (i + j == N) {
        M = M + 1;
      }
    }
  }
  cout << M / 2 << endl;
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  cout << (n - 1) / 2 << endl;
  return 0;
}
Problem p02865 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
typedef long long ll;
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define mod 1000000007
#define fst first
#define pb push_back
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// ユークリッドの互除法
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

int main() {
  ll n;
  cin >> n;
  ll cnt = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = i + 1; j <= n - 1; j++) {
      if (n - j == i) {
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}

Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
typedef long long ll;
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define mod 1000000007
#define fst first
#define pb push_back
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// ユークリッドの互除法
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

int main() {
  ll n;
  cin >> n;
  ll cnt = 0;
  cout << (n - 1) / 2 << endl;
}
Problem p02865 (C++)

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

int main() {
  int N;
  cin >> N;
  int ans = 0, i = 1, num;
  while (i) {
    if (N - num <= num) {
      break;
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  int ans = N / 2 - 1 * ((N + 1) % 2);
  cout << ans << endl;
  return 0;
}
Problem p02865 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
std::string line;

int main() {

  int n;
  cin >> n;

  int cnt = 0;
  for (int i = 1; i < n / 2; ++i) {
    for (int j = n / 2; j < n; ++j) {
      if (n == i + j) {
        cnt++;
        break;
      }
    }
  }

  cout << cnt << endl;

  return 0;
};


Fixed submission:
#include <bits/stdc++.h>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
std::string line;

int main() {

  int n;
  cin >> n;

  int cnt = 0;

  cnt = (n - 1) / 2;

  cout << cnt << endl;

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
int main() {
  ll k;
  cin >> k;
  ll i, j, c = 0;
  for (i = 1; i < k; i++) {
    for (j = i; j < k; j++) {
      if (i + j == k && i != j) {
        c++;
      }
    }
  }
  cout << c << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
int main() {
  int n, m, x, k, count, i;
  cin >> n;
  cout << (n - 1) / 2;
  return 0;
}
Problem p02865 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

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

  int ans = 0;
  for (int i = 1; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (i + j == N)
        ans++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

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

  int ans = 0;
  for (int i = 1; i < N; i++) {
    int n = N - i;
    if (n > i)
      ans++;
  }
  cout << ans << endl;
}
Problem p02865 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(x) (x).begin(), (x).end()
using namespace std;

// 素因数分解
vector<pair<ll, int>> factorize(ll n) {
  vector<pair<ll, int>> res;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i)
      continue;
    res.emplace_back(i, 0);
    while (n % i == 0) {
      n /= i;
      res.back().second++;
    }
  }
  if (n != 1)
    res.emplace_back(n, 1);
  return res;
}

int main() {
  int N;
  cin >> N;
  int ans = 0;
  REP(i, N) {
    REP(j, N) {
      if (i > j && i != j && N == i + j)
        ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(x) (x).begin(), (x).end()
using namespace std;

// 素因数分解
vector<pair<ll, int>> factorize(ll n) {
  vector<pair<ll, int>> res;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i)
      continue;
    res.emplace_back(i, 0);
    while (n % i == 0) {
      n /= i;
      res.back().second++;
    }
  }
  if (n != 1)
    res.emplace_back(n, 1);
  return res;
}

int main() {
  int N;
  cin >> N;
  int ans = 0;
  if (N <= 2)
    ans = 0;
  else {
    if (N % 2 == 0)
      ans = (N - 1) / 2;
    else
      ans = N / 2;
  }
  cout << ans << endl;
}
Problem p02865 (C++)

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

using namespace std;
using ll = long long;
using db = double;
using vll = vector<long long>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#define che(a, string) cout << "//" << string << "==" << (a) << "//" << endl;
#define SORT(a) sort((a).begin(), (a).end())
#define rSORT(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define leftunique(a)                                                          \
  {                                                                            \
    SORT(a);                                                                   \
    (a).erase(unique((a).begin(), (a).end()), (a).end());                      \
  }
// leftuniqueは配列から同じ要素のものを取り除く 例 4 6 2 1 3 4 1 2 なら 1 2 3 4
// 6 になる
#define debug(x) cout << #x << " = " << (x) << endl;
bool IsInt(double a) {
  int b = a / 1;
  if (a == b) {
    return true;
  } else {
    return false;
  }
}
bool coY() { cout << "Yes" << endl; }
bool coN() { cout << "No" << endl; }
const int mod = 1e9 + 7;
const ll INF = 1LL << 60;
// ここから書き始める

int main() {
  cout << setprecision(10);
  int n;
  cin >> n;
  ll ans = 0;
  rep(i, n) {
    For(j, i + 1, n) {
      if (i + j == n) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}


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

using namespace std;
using ll = long long;
using db = double;
using vll = vector<long long>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#define che(a, string) cout << "//" << string << "==" << (a) << "//" << endl;
#define SORT(a) sort((a).begin(), (a).end())
#define rSORT(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define leftunique(a)                                                          \
  {                                                                            \
    SORT(a);                                                                   \
    (a).erase(unique((a).begin(), (a).end()), (a).end());                      \
  }
// leftuniqueは配列から同じ要素のものを取り除く 例 4 6 2 1 3 4 1 2 なら 1 2 3 4
// 6 になる
#define debug(x) cout << #x << " = " << (x) << endl;
bool IsInt(double a) {
  int b = a / 1;
  if (a == b) {
    return true;
  } else {
    return false;
  }
}
bool coY() { cout << "Yes" << endl; }
bool coN() { cout << "No" << endl; }
const int mod = 1e9 + 7;
const ll INF = 1LL << 60;
// ここから書き始める

int main() {
  cout << setprecision(10);
  int n;
  cin >> n;
  ll ans = 0;
  For(i, 1, n / 2 + 1) {
    int j = n - i;
    if (j > 0 && i != j) {
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p02865 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
const long double PI = (acos(-1));
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (i + j == n) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
const long double PI = (acos(-1));
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (n - i > i && n - i > 1 && n) {
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p02865 (C++)

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

int main() {
  int n, sum, i, j;
  cin >> n;
  sum = 0;
  for (i = 1; i < n; ++i) {
    for (j = i + 1; i + j < n; ++j) {
      sum = sum + 1;
    }
  }
  cout << sum << endl;
  return 0;
}


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

int main() {
  int n, sum, i, j;
  cin >> n;
  sum = 0;
  if (n % 2 == 0) {
    sum = n / 2 - 1;
  } else {
    sum = n / 2;
  }
  cout << sum << endl;
  return 0;
}
Problem p02865 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define endl "\n"
#define pb push_back
#define ll long long
#define ld long double
#define d1(x) cerr << #x << "--> " << x << endl
#define d2(x, y) cerr << #x << "--> " << x << " | " << #y << "--> " << y << endl
#define d3(x, y, z)                                                            \
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \
       << "--> " << z << endl
#define vpll vector<pair<ll, ll>>
#define F first
#define S second
using namespace std;

ll mode = pow(10, 9) + 7;
bool cmp(pair<ll, ll> x, pair<ll, ll> y) { return x.S < y.S; }

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif

  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n;
  cin >> n;
  if (n % 2 == 0) {
    n = n / 2;
    cout << n - 1 << endl;
  } else {
    n = n / 2;
    cout << n << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define endl "\n"
#define pb push_back
#define ll long long
#define ld long double
#define d1(x) cerr << #x << "--> " << x << endl
#define d2(x, y) cerr << #x << "--> " << x << " | " << #y << "--> " << y << endl
#define d3(x, y, z)                                                            \
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \
       << "--> " << z << endl
#define vpll vector<pair<ll, ll>>
#define F first
#define S second
using namespace std;

ll mode = pow(10, 9) + 7;
bool cmp(pair<ll, ll> x, pair<ll, ll> y) { return x.S < y.S; }

int main() {

#ifndef ONLINE_JUDGE
  //	freopen("input.txt", "r", stdin);
#endif

  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n;
  cin >> n;
  if (n % 2 == 0) {
    n = n / 2;
    cout << n - 1 << endl;
  } else {
    n = n / 2;
    cout << n << endl;
  }

  return 0;
}
Problem p02865 (C++)

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

#define N 1000

int main() {
  long int n, i, j, k, cnt = 0;
  cin >> n;
  k = n;

  for (i = 1; i < n / 2; i++) {
    for (j = i + 1; j < k; j++) {
      if (n == i + j) {
        cnt++;
        k--;
        break;
      }
    }
  }

  cout << cnt << endl;

  return 0;
}

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

#define N 1000

int main() {
  long int n, i, j, k, cnt = 0;
  cin >> n;
  k = n;

  for (i = 1; i <= n / 2; i++) {
    for (j = k - 1; j > n / 2; j--) {
      if (n == i + j) {
        cnt++;
        k--;
        break;
      }
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p02866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 998244353;
int main() {
  long long int N, x = 0;
  cin >> N;
  vector<long long int> D(N);
  cin >> D.at(0);
  if (D.at(0) == 0) {
    if (N == 1)
      x = 1;
    else {
      for (long long int i = 1; i < N; i++)
        cin >> D.at(i);
      sort(D.begin(), D.end());
      if (D.at(1) != 0) {
        x = 1;
        vector<long long int> C(D.at(N - 1));
        for (long long int i = 0; i < D.at(N - 1); i++)
          C.at(i) = count(D.begin(), D.end(), i);
        for (long long int i = 1; i < N; i++)
          x = x * C.at(D.at(i) - 1) % mod;
      }
    }
  }
  printf("%lld\n", x);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 998244353;
int main() {
  long long int N, x = 0;
  cin >> N;
  vector<long long int> D(N);
  cin >> D.at(0);
  if (D.at(0) == 0) {
    if (N == 1)
      x = 1;
    else {
      for (long long int i = 1; i < N; i++)
        cin >> D.at(i);
      sort(D.begin(), D.end());
      if (D.at(1) != 0) {
        x = 1;
        vector<long long int> C(D.at(N - 1));
        for (long long int i = 0; i < D.at(N - 1); i++)
          C.at(i) = upper_bound(D.begin(), D.end(), i) -
                    lower_bound(D.begin(), D.end(), i);
        for (long long int i = 1; i < N; i++)
          x = x * C.at(D.at(i) - 1) % mod;
      }
    }
  }
  printf("%lld\n", x);
}