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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define INF (1LL << 60)
#define ll long long
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M, K, S;
  cin >> N >> M >> K >> S;
  ll P, Q;
  cin >> P >> Q;
  vector<vector<ll>> G(N + 1, vector<ll>(N + 1));
  vector<ll> cost(N + 1, P);
  vector<bool> kiken(N + 1, false);
  for (int i = 0; i < K; i++) {
    int k;
    cin >> k;
    kiken[k] = true;
    G[0].push_back(k);
    cost[k] = INF;
  }
  for (int i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  queue<pair<ll, ll>> qq;
  qq.push({0, 0});
  vector<bool> f(N + 1, false);
  while (!qq.empty()) {
    auto p = qq.front();
    qq.pop();
    ll s = p.first, n = p.second;
    if (n <= S) {
      for (auto e : G[s]) {
        if (f[e] == false) {
          f[e] = true;
          if (n != 0)
            cost[e] = Q;
          qq.push({e, n + 1});
        }
      }
    }
  }
  vector<ll> dist(N + 1, INF);

  queue<ll> q;
  q.push(1);
  dist[1] = 0;
  while (!q.empty()) {
    auto n = q.front();
    q.pop();
    for (auto e : G[n]) {
      // if (kiken[e] == true)continue;
      if (dist[e] > dist[n] + cost[e]) {
        dist[e] = dist[n] + cost[e];
        q.push(e);
      }
    }
  }
  cout << dist[N] - cost[N] << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define INF (1LL << 60)
#define ll long long
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M, K, S;
  cin >> N >> M >> K >> S;
  ll P, Q;
  cin >> P >> Q;
  vector<vector<ll>> G(N + 1);
  vector<ll> cost(N + 1, P);
  vector<bool> kiken(N + 1, false);
  for (int i = 0; i < K; i++) {
    int k;
    cin >> k;
    kiken[k] = true;
    G[0].push_back(k);
    cost[k] = INF;
  }
  for (int i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  queue<pair<ll, ll>> qq;
  qq.push({0, 0});
  vector<bool> f(N + 1, false);
  while (!qq.empty()) {
    auto p = qq.front();
    qq.pop();
    ll s = p.first, n = p.second;
    if (n <= S) {
      for (auto e : G[s]) {
        if (f[e] == false) {
          f[e] = true;
          if (n != 0)
            cost[e] = Q;
          qq.push({e, n + 1});
        }
      }
    }
  }
  vector<ll> dist(N + 1, INF);

  queue<ll> q;
  q.push(1);
  dist[1] = 0;
  while (!q.empty()) {
    auto n = q.front();
    q.pop();
    for (auto e : G[n]) {
      // if (kiken[e] == true)continue;
      if (dist[e] > dist[n] + cost[e]) {
        dist[e] = dist[n] + cost[e];
        q.push(e);
      }
    }
  }
  cout << dist[N] - cost[N] << endl;
}
Problem p00548 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LLINF = 1LL << 60;
int main() {
  ll N, M, K;
  ll dp[20005];
  ll A[20005];
  fill_n(dp, 20005, LLINF);
  cin >> N >> M >> K;
  dp[0] = 0;
  for (ll i = 0; i < N; i++)
    cin >> A[i];
  for (ll i = 0; i < N; i++) {
    ll maxv = -1, minv = LLINF;
    for (ll j = 0; j < M; j++) {
      maxv = max(A[i + j], maxv);
      minv = min(A[i + j], minv);
      dp[i + j + 1] = min(dp[i + j + 1], dp[i] + K + (j + 1) * (maxv - minv));
    }
  }
  cout << dp[N] << endl;
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LLINF = 1LL << 60;
int main() {
  ll N, M, K;
  ll dp[20005];
  ll A[20005];
  fill_n(dp, 20005, LLINF);
  cin >> N >> M >> K;
  dp[0] = 0;
  for (ll i = 0; i < N; i++)
    cin >> A[i];
  for (ll i = 0; i < N; i++) {
    ll maxv = -1, minv = LLINF;
    for (ll j = 0; j < M; j++) {
      if (i + j >= N)
        continue;
      maxv = max(A[i + j], maxv);
      minv = min(A[i + j], minv);
      dp[i + j + 1] = min(dp[i + j + 1], dp[i] + K + (j + 1) * (maxv - minv));
    }
  }
  cout << dp[N] << endl;
  return (0);
}
Problem p00548 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, m, k, a[20005], dp[20005];
long long solve(long long p) {
  if (p > n)
    return 1LL << 62;
  if (p == n)
    return 0;
  if (dp[p] >= 1)
    return dp[p];
  long long p1 = 1LL << 62, p2 = 0, ret = 1LL << 62;
  for (int i = p + 1; i <= p + m; i++) {
    p1 = min(p1, a[i - 1]);
    p2 = max(p2, a[i - 1]);
    ret = min(ret, solve(i) + (p2 - p1) * (i - p) + k);
  }
  dp[p] = ret;
  return ret;
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << solve(0) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, m, k, a[100005], dp[100005];
long long solve(long long p) {
  if (p > n)
    return 1LL << 62;
  if (p == n)
    return 0;
  if (dp[p] >= 1)
    return dp[p];
  long long p1 = 1LL << 62, p2 = 0, ret = 1LL << 62;
  for (int i = p + 1; i <= p + m; i++) {
    p1 = min(p1, a[i - 1]);
    p2 = max(p2, a[i - 1]);
    ret = min(ret, solve(i) + (p2 - p1) * (i - p) + k);
  }
  dp[p] = ret;
  return ret;
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << solve(0) << endl;
  return 0;
}
Problem p00548 (C++)

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

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
long long int dp[20001];
int main() {
  fill(dp, dp + sizeof(dp), static_cast<long long int>(1e18));
  dp[0] = 0;
  int N, M, K;
  cin >> N >> M >> K;
  vector<long long int> ws(N);
  for (int i = 0; i < N; ++i) {
    cin >> ws[i];
  }
  for (int i = 0; i < N; ++i) {
    long long int amin = 1e18;
    long long int amax = 0;
    long long int cost = K;
    for (int j = 0; j < min(N - i, M); ++j) {
      amin = min(amin, ws[i + j]);
      amax = max(amax, ws[i + j]);
      dp[i + j + 1] =
          min(dp[i + j + 1], dp[i] + cost + (j + 1) * (amax - amin));
    }
  }
  cout << dp[N] << endl;
  return 0;
}

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

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
long long int dp[20001];
int main() {
  for (int i = 0; i < 20001; ++i) {
    dp[i] = 1e18;
  }
  dp[0] = 0;
  int N, M, K;
  cin >> N >> M >> K;
  vector<long long int> ws(N);
  for (int i = 0; i < N; ++i) {
    cin >> ws[i];
  }
  for (int i = 0; i < N; ++i) {
    long long int amin = 1e18;
    long long int amax = 0;
    long long int cost = K;
    for (int j = 0; j < min(N - i, M); ++j) {
      amin = min(amin, ws[i + j]);
      amax = max(amax, ws[i + j]);
      dp[i + j + 1] =
          min(dp[i + j + 1], dp[i] + cost + (j + 1) * (amax - amin));
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p00548 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
#define inf 10e18
typedef long long ll;

int main(void) {
  ll i, j, k, n, m;
  scanf("%lld%lld%lld", &n, &m, &k);
  ll a[n], **sum, dp[n];
  sum = (ll **)malloc(sizeof(ll *) * (n + 10));
  for (i = 0; i < n; ++i)
    sum[i] = (ll *)malloc(sizeof(ll) * (m + 10));
  for (i = 0; i < n; ++i)
    scanf("%lld", &a[i]);
  for (i = 0; i < n; ++i)
    dp[i] = inf;
  dp[0] = k;
  for (i = 0; i < n; ++i) {
    ll min = inf, max = 0;
    for (j = 0; j < n; ++j) {
      if (i - j >= 0) {
        if (min > a[i - j])
          min = a[i - j];
        if (max < a[i - j])
          max = a[i - j];
        sum[i][j] = k + (j + 1) * (max - min);
      }
    }
  }
  for (i = 1; i < n; ++i) {
    for (j = 0; j < m; ++j) {
      if (i - j > 0) {
        if (dp[i] > sum[i][j] + dp[i - j - 1])
          dp[i] = sum[i][j] + dp[i - j - 1];
      } else if (!(i - j)) {
        if (dp[i] > sum[i][j])
          dp[i] = sum[i][j];
      }
    }
  }
  printf("%lld\n", dp[n - 1]);
  for (i = 0; i < n; ++i)
    free(sum[i]);
  free(sum);
  return 0;
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#define inf 10e18
typedef long long ll;

int main(void) {
  ll i, j, k, n, m;
  scanf("%lld%lld%lld", &n, &m, &k);
  ll a[n], **sum, dp[n];
  sum = (ll **)malloc(sizeof(ll *) * (n + 10));
  for (i = 0; i < n; ++i)
    sum[i] = (ll *)malloc(sizeof(ll) * (m + 10));
  for (i = 0; i < n; ++i)
    scanf("%lld", &a[i]);
  for (i = 0; i < n; ++i)
    dp[i] = inf;
  dp[0] = k;
  for (i = 0; i < n; ++i) {
    ll min = inf, max = 0;
    for (j = 0; j < m; ++j) {
      if (i - j >= 0) {
        if (min > a[i - j])
          min = a[i - j];
        if (max < a[i - j])
          max = a[i - j];
        sum[i][j] = k + (j + 1) * (max - min);
      }
    }
  }
  for (i = 1; i < n; ++i) {
    for (j = 0; j < m; ++j) {
      if (i - j > 0) {
        if (dp[i] > sum[i][j] + dp[i - j - 1])
          dp[i] = sum[i][j] + dp[i - j - 1];
      } else if (!(i - j)) {
        if (dp[i] > sum[i][j])
          dp[i] = sum[i][j];
      }
    }
  }
  printf("%lld\n", dp[n - 1]);
  for (i = 0; i < n; ++i)
    free(sum[i]);
  free(sum);
  return 0;
}
Problem p00548 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N, M, K;
ll A[100005];

ll dp[20005];

const ll INF = 1000000000000000000LL;

ll solve() {
  fill(dp, dp + N + 1, INF);
  dp[0] = 0;
  for (ll i = 1; i <= N; i++) {
    ll a = -INF, b = INF;
    for (ll j = i - 1; j >= max(0LL, i - M); j--) {
      b = min(A[j], b);
      a = max(A[j], a);
      ll c = K + (i - j) * (a - b);
      assert(c >= 0);
      dp[i] = min(dp[i], dp[j] + c);
    }
  }
  return dp[N];
}

int main() {
  cin >> N >> M >> K;
  assert(N < 20000);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  cout << solve() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N, M, K;
ll A[100005];

ll dp[20005];

const ll INF = 1000000000000000000LL;

ll solve() {
  fill(dp, dp + N + 1, INF);
  dp[0] = 0;
  for (ll i = 1; i <= N; i++) {
    ll a = -INF, b = INF;
    for (ll j = i - 1; j >= max(0LL, i - M); j--) {
      b = min(A[j], b);
      a = max(A[j], a);
      ll c = K + (i - j) * (a - b);
      assert(c >= 0);
      dp[i] = min(dp[i], dp[j] + c);
    }
  }
  return dp[N];
}

int main() {
  cin >> N >> M >> K;
  assert(N <= 20000);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  cout << solve() << endl;
}
Problem p00548 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 200001
const int inf = 1e18;

int mn[2][1010], mx[2][1010];
int a[MAX], dp[2][1010];
signed main() {
  int n, m, k;
  fill((int *)dp, (int *)(dp + 2), inf);
  fill((int *)mn, (int *)(mn + 2), inf);
  scanf("%lld%lld%lld", &n, &m, &k);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);

  dp[0][0] = k;
  mn[0][0] = mx[0][0] = a[0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n; j++) {
      mn[1][j] = inf;
      mx[1][j] = 0;
      dp[1][j] = inf;
    }
    for (int j = 1; j < m; j++) {
      if (dp[0][j - 1] == inf)
        continue;
      mn[1][j] = min(mn[0][j - 1], a[i]);
      mx[1][j] = max(mx[0][j - 1], a[i]);
      dp[1][j] = dp[0][j - 1];
    }
    for (int j = 0; j < m; j++) {
      if (dp[0][j] == inf)
        continue;
      int sum = dp[0][j] + (j + 1) * (mx[0][j] - mn[0][j]) + k;
      dp[1][0] = min(dp[1][0], sum);
      mx[1][0] = mn[1][0] = a[i];
    }
    for (int j = 0; j < m; j++) {
      mn[0][j] = mn[1][j];
      mx[0][j] = mx[1][j];
      dp[0][j] = dp[1][j];
    }
  }

  int ans = dp[0][0];
  for (int i = 1; i < m; i++) {
    int sum = dp[0][i] + (i + 1) * (mx[0][i] - mn[0][i]);
    ans = min(ans, sum);
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 200001
const int inf = 1e18;

int mn[2][1010], mx[2][1010];
int a[MAX], dp[2][1010];
signed main() {
  int n, m, k;
  fill((int *)dp, (int *)(dp + 2), inf);
  fill((int *)mn, (int *)(mn + 2), inf);
  scanf("%lld%lld%lld", &n, &m, &k);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);

  dp[0][0] = k;
  mn[0][0] = mx[0][0] = a[0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < m; j++) {
      mn[1][j] = inf;
      mx[1][j] = 0;
      dp[1][j] = inf;
    }
    for (int j = 1; j < m; j++) {
      if (dp[0][j - 1] == inf)
        continue;
      mn[1][j] = min(mn[0][j - 1], a[i]);
      mx[1][j] = max(mx[0][j - 1], a[i]);
      dp[1][j] = dp[0][j - 1];
    }
    for (int j = 0; j < m; j++) {
      if (dp[0][j] == inf)
        continue;
      int sum = dp[0][j] + (j + 1) * (mx[0][j] - mn[0][j]) + k;
      dp[1][0] = min(dp[1][0], sum);
      mx[1][0] = mn[1][0] = a[i];
    }
    for (int j = 0; j < m; j++) {
      mn[0][j] = mn[1][j];
      mx[0][j] = mx[1][j];
      dp[0][j] = dp[1][j];
    }
  }

  int ans = dp[0][0];
  for (int i = 1; i < m; i++) {
    int sum = dp[0][i] + (i + 1) * (mx[0][i] - mn[0][i]);
    ans = min(ans, sum);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p00548 (C++)

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

#include <iostream>

using namespace std;
long long N, M, K;
long long A[20001] = {};
long long dp[20001];
long long mmD[20001][1001];
const long long INF = 100000000000000000;

long long calc(long long n1) {
  if (dp[n1] > -1)
    return dp[n1];
  long long ans = INF;
  for (long long i = 1; i < M + 1 && i < n1 + 1; ++i) {
    ans = min(ans, calc(n1 - i) + K + i * mmD[n1 - i][i]);
  }
  dp[n1] = ans;
  return ans;
}

int main() {
  cin >> N >> M >> K;
  for (long long i = 1; i < N + 1; i++)
    cin >> A[i];

  fill(dp, dp + 20001, -1);
  long long maxD[1001] = {};
  long long minD[1001] = {};

  for (int i = 0; i < N + 1; i++) {
    maxD[0] = 0;
    minD[0] = INF;
    for (int j = 1; j < M + 1; j++) {
      maxD[j] = max(maxD[j - 1], A[i + j]);
      minD[j] = min(minD[j - 1], A[i + j]);
      mmD[i][j] = maxD[j] - minD[j];
    }
  }
  dp[0] = 0;
  //	long long R;
  //	for(int i=1;i<N;i++){
  //		for(int j=0;j<M+1;j++)
  //		R=calc(i,j);
  //	}
  //	R=calc(N,0);
  //	cout<<R<<endl;
  cout << calc(N) << endl;
  return 0;
}

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

#include <iostream>

using namespace std;
long long N, M, K;
long long A[20001] = {};
long long dp[20001];
long long mmD[20001][1001];
const long long INF = 100000000000000000;

long long calc(long long n1) {
  if (dp[n1] > -1)
    return dp[n1];
  long long ans = INF;
  for (long long i = 1; i < M + 1 && i < n1 + 1; ++i) {
    ans = min(ans, calc(n1 - i) + K + i * mmD[n1 - i][i]);
  }
  dp[n1] = ans;
  return ans;
}

int main() {
  cin >> N >> M >> K;
  for (long long i = 1; i < N + 1; i++)
    cin >> A[i];

  fill(dp, dp + 20001, -1);
  long long maxD[1001] = {};
  long long minD[1001] = {};

  for (int i = 0; i < N + 1; i++) {
    maxD[0] = 0;
    minD[0] = INF;
    for (int j = 1; j < M + 1 && (i + j) < N + 1; j++) {
      maxD[j] = max(maxD[j - 1], A[i + j]);
      minD[j] = min(minD[j - 1], A[i + j]);
      mmD[i][j] = maxD[j] - minD[j];
    }
  }
  dp[0] = 0;
  //	long long R;
  //	for(int i=1;i<N;i++){
  //		for(int j=0;j<M+1;j++)
  //		R=calc(i,j);
  //	}
  //	R=calc(N,0);
  //	cout<<R<<endl;
  cout << calc(N) << endl;
  return 0;
}
Problem p00548 (C++)

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

// #define DEBUG
#define int long long
#define INF 1000000000000000000
#define REP(i, n) for (int i = 1; i <= n; i++)
int n, m, k;
int a[20002];
int cs[20002][1002];
int sm[20002];

signed main() {
  cin >> n >> m >> k;
  REP(i, n) cin >> a[i];
  REP(i, n) {
    int mx = 0;
    int mn = INF;
    REP(j, m) {
      mx = max(mx, a[i + j - 1]);
      mn = min(mn, a[i + j - 1]);
      cs[i][j] = k + j * (mx - mn);
    }
  }
  REP(i, n) sm[i] = INF;
  REP(i, n) REP(j, m) {
    if (n < i - 1 + j)
      continue;
    sm[i - 1 + j] = min(sm[i - 1 + j], sm[i - 1] + cs[i][j]);
  }
#ifdef DEBUG
  REP(i, n) {
    REP(j, m) cout << cs[i][j] << " ";
    cout << endl;
  }
  REP(i, n) cout << sm[i] << " ";
  cout << endl;
#endif
  cout << sm[n] << endl;
  return 0;
}

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

// #define DEBUG
#define int long long
#define INF 1000000000000000000
#define REP(i, n) for (int i = 1; i <= n; i++)
int n, m, k;
int a[20002];
int cs[20002][1002];
int sm[20002];

signed main() {
  cin >> n >> m >> k;
  REP(i, n) cin >> a[i];
  REP(i, n) {
    int mx = 0;
    int mn = INF;
    REP(j, m) {
      if (n < i + j - 1)
        continue;
      mx = max(mx, a[i + j - 1]);
      mn = min(mn, a[i + j - 1]);
      cs[i][j] = k + j * (mx - mn);
    }
  }
  REP(i, n) sm[i] = INF;
  REP(i, n) REP(j, m) {
    if (n < i - 1 + j)
      continue;
    sm[i - 1 + j] = min(sm[i - 1 + j], sm[i - 1] + cs[i][j]);
  }
#ifdef DEBUG
  REP(i, n) {
    REP(j, m) cout << cs[i][j] << " ";
    cout << endl;
  }
  REP(i, n) cout << sm[i] << " ";
  cout << endl;
#endif
  cout << sm[n] << endl;
  return 0;
}
Problem p00548 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define INF 1e+18
#define int long long
using namespace std;

signed main() {
  int n, m, k, a[20000];
  int dp[20001][1001];
  cin >> n >> m >> k;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= 1000; j++)
      dp[i][j] = INF;
  }
  dp[0][0] = 0;
  dp[0][1000] = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    int mi = a[i - 1], ma = a[i - 1];
    for (int j = 0; j < min(m, i); j++) {
      dp[i][j] = dp[i - j - 1][1000] + k + (j + 1) * (ma - mi);
      dp[i][1000] = min(dp[i][1000], dp[i][j]);
      if (i - j - 2 >= 0) {
        mi = min(mi, a[i - j - 2]);
        ma = max(ma, a[i - j - 2]);
      }
    }
  }
  cout << dp[n][1000] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#define INF 1e+18
#define int long long
using namespace std;

signed main() {
  int n, m, k, a[20000];
  static int dp[20001][1001];
  cin >> n >> m >> k;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= 1000; j++)
      dp[i][j] = INF;
  }
  dp[0][0] = 0;
  dp[0][1000] = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    int mi = a[i - 1], ma = a[i - 1];
    for (int j = 0; j < min(m, i); j++) {
      dp[i][j] = dp[i - j - 1][1000] + k + (j + 1) * (ma - mi);
      dp[i][1000] = min(dp[i][1000], dp[i][j]);
      if (i - j - 2 >= 0) {
        mi = min(mi, a[i - j - 2]);
        ma = max(ma, a[i - j - 2]);
      }
    }
  }
  cout << dp[n][1000] << endl;
  return 0;
}
Problem p00549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
string S;
string ns;
const string JOI = "JOI#";

ll IC[100005];
ll JC[100005];

ll dp[10005][4];
ll solve(const string &s) {
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < 4; j++) {
      if (s[i] == JOI[j])
        dp[i + 1][j + 1] += dp[i][j];
      dp[i + 1][j] += dp[i][j];
    }
  }
  return dp[s.size()][3];
}

int main() {

  cin >> N;
  cin >> S;

  ll res = solve(S);

  S = "a" + S;

  for (int i = 1; i <= N; i++) {
    if (S[i] == 'J')
      JC[i] = JC[i - 1] + 1;
    else
      JC[i] = JC[i - 1];
  }

  for (int i = N; i >= 1; i--) {
    if (S[i] == 'I')
      IC[i] = IC[i + 1] + 1;
    else
      IC[i] = IC[i + 1];
  }

  ll lmax = 0;
  for (int i = 1; i <= N; i++) {
    lmax = max(lmax, JC[i] * IC[i]);
  }
  res = res + lmax;

  S = S.substr(1);
  ns = "J" + S;
  res = max(res, solve(ns));
  ns = S + "I";
  res = max(res, solve(ns));

  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
string S;
string ns;
const string JOI = "JOI#";

ll IC[100005];
ll JC[100005];

ll dp[100005][4];
ll solve(const string &s) {
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < 4; j++) {
      if (s[i] == JOI[j])
        dp[i + 1][j + 1] += dp[i][j];
      dp[i + 1][j] += dp[i][j];
    }
  }
  return dp[s.size()][3];
}

int main() {

  cin >> N;
  cin >> S;

  ll res = solve(S);

  S = "a" + S;

  for (int i = 1; i <= N; i++) {
    if (S[i] == 'J')
      JC[i] = JC[i - 1] + 1;
    else
      JC[i] = JC[i - 1];
  }

  for (int i = N; i >= 1; i--) {
    if (S[i] == 'I')
      IC[i] = IC[i + 1] + 1;
    else
      IC[i] = IC[i + 1];
  }

  ll lmax = 0;
  for (int i = 1; i <= N; i++) {
    lmax = max(lmax, JC[i] * IC[i]);
  }
  res = res + lmax;

  S = S.substr(1);
  ns = "J" + S;
  res = max(res, solve(ns));
  ns = S + "I";
  res = max(res, solve(ns));

  cout << res << endl;
}
Problem p00552 (C++)

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

int seg = 1;

class RAQ {
  vector<int> dat;

public:
  RAQ() : dat(seg * 2 - 1) {}
  void add(int a, int b, int x, int k = 0, int l = 0, int r = seg) {
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      dat[k] += x;
      return;
    }
    add(a, b, x, k * 2 + 1, l, (l + r) / 2);
    add(a, b, x, k * 2 + 2, (l + r) / 2, r);
  }
  int get(int i) {
    int s = 0;
    i += seg - 1;
    s += dat[i];
    while (i) {
      i = (i - 1) / 2;
      s += dat[i];
    }
    return s;
  }
};

signed main() {
  int n, q, x[100000], d[100000], l[100000];
  cin >> n >> q;
  while (seg < n)
    seg *= 2;
  vector<RAQ> ps(2);
  for (int i = 0; i < n; i++) {
    ps[0].add(i, i + 1, i);
    ps[1].add(i, i + 1, n - i - 1);
  }
  for (int i = 0; i < q; i++)
    cin >> x[i] >> d[i] >> l[i];
  for (int i = q - 1; i >= 0; i--) {
    int low = -1, up = n;
    while (up - low > 1) {
      int mid = (up + low) / 2;
      if (d[i] == 1) {
        if (ps[0].get(mid) < x[i])
          low = mid;
        else
          up = mid;
      } else {
        if (ps[1].get(mid) < n - x[i])
          up = mid;
        else
          low = mid;
      }
    }
    if (d[i] == 1)
      ps[1].add(0, up, l[i] * 2);
    else
      ps[0].add(up, seg, l[i] * 2);
  }
  for (int i = 0; i < n; i++)
    cout << (ps[0].get(i) + ps[1].get(i) - n + 1) / 2 << endl;
  return 0;
}


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

int seg = 1;

class RAQ {
  vector<int> dat;

public:
  RAQ() : dat(seg * 2 - 1) {}
  void add(int a, int b, int x, int k = 0, int l = 0, int r = seg) {
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      dat[k] += x;
      return;
    }
    add(a, b, x, k * 2 + 1, l, (l + r) / 2);
    add(a, b, x, k * 2 + 2, (l + r) / 2, r);
  }
  int get(int i) {
    int s = 0;
    i += seg - 1;
    s += dat[i];
    while (i) {
      i = (i - 1) / 2;
      s += dat[i];
    }
    return s;
  }
};

signed main() {
  int n, q, x[200000], d[200000], l[200000];
  cin >> n >> q;
  while (seg < n)
    seg *= 2;
  vector<RAQ> ps(2);
  for (int i = 0; i < n; i++) {
    ps[0].add(i, i + 1, i);
    ps[1].add(i, i + 1, n - i - 1);
  }
  for (int i = 0; i < q; i++)
    cin >> x[i] >> d[i] >> l[i];
  for (int i = q - 1; i >= 0; i--) {
    int low = -1, up = n;
    while (up - low > 1) {
      int mid = (up + low) / 2;
      if (d[i] == 1) {
        if (ps[0].get(mid) < x[i])
          low = mid;
        else
          up = mid;
      } else {
        if (ps[1].get(mid) < n - x[i])
          up = mid;
        else
          low = mid;
      }
    }
    if (d[i] == 1)
      ps[1].add(0, up, l[i] * 2);
    else
      ps[0].add(up, seg, l[i] * 2);
  }
  for (int i = 0; i < n; i++)
    cout << (ps[0].get(i) + ps[1].get(i) - n + 1) / 2 << endl;
  return 0;
}
Problem p00554 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n, m, a[100], b[100];
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] > a[j])
        swap(a[i], a[j]);
    }
  }
  int ans = 0;
  for (int i = 0; i < m - 1; i++) {
    if (a[i] <= n)
      ans += n - a[i];
  }
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, m, a[1001], b[1001];
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] > a[j])
        swap(a[i], a[j]);
    }
  }
  int ans = 0;
  for (int i = 0; i < m - 1; i++) {
    if (a[i] <= n)
      ans += n - a[i];
  }
  cout << ans << endl;
}
Problem p00554 (C++)

Buggy submission (status: Runtime Error):
// q2.cpp

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 10000

int main() {
  int n, m, a, b, s[SIZE];

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

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);

    s[i] = max(n - a, 0);
  }

  sort(s, s + m);

  int ans = 0;

  for (int i = 0; i < m - 1; i++) {
    ans += s[i];
    debug(s[i]);
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
// q2.cpp

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 10000

int main() {
  int n, m, a, b, s[SIZE];

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

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);

    s[i] = max(n - a, 0);
  }

  sort(s, s + m);

  int ans = 0;

  for (int i = 0; i < m - 1; i++) {
    ans += s[i];
  }

  cout << ans << endl;

  return 0;
}
Problem p00555 (C++)

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

bool b[110][110];

int main() {
  int n, m, d;
  cin >> n >> m >> d;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      b[i][j] = (c == '.');
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      bool f = true;
      for (int k = 0; k < d; k++) {
        f &= b[i + k][j];
      }

      ans += f;

      f = true;
      for (int k = 0; k < d; k++) {
        f &= b[i][j + k];
      }

      ans += f;
    }
  }

  cout << ans << endl;
}

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

bool b[256][256];

int main() {
  int n, m, d;
  cin >> n >> m >> d;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      b[i][j] = (c == '.');
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      bool f = true;
      for (int k = 0; k < d; k++) {
        f &= b[i + k][j];
      }

      ans += f;

      f = true;
      for (int k = 0; k < d; k++) {
        f &= b[i][j + k];
      }

      ans += f;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <iostream>
#define INF 1e+9
using namespace std;

int main() {
  int n, m, a[100000], cnt[20] = {}, imos[100001][20] = {};
  int dp[1 << 20] = {};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    cnt[a[i]]++;
    for (int j = 0; j < m; j++)
      imos[i + 1][j] = imos[i][j];
    imos[i + 1][a[i]]++;
  }
  for (int i = 0; i < (1 << 20); i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  for (int i = 1; i < (1 << n); i++) {
    int sum = 0;
    for (int j = 0; j < m; j++) {
      if ((i >> j) & 1)
        sum += cnt[j];
    }
    for (int j = 0; j < m; j++) {
      if ((i >> j) & 1) {
        dp[i] = min(dp[i], dp[i & ~(1 << j)] + cnt[j] -
                               (imos[sum][j] - imos[sum - cnt[j]][j]));
      }
    }
  }
  cout << dp[(1 << m) - 1] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#define INF 1e+9
using namespace std;

int main() {
  int n, m, a[100000], cnt[20] = {}, imos[100001][20] = {};
  int dp[1 << 20] = {};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    cnt[a[i]]++;
    for (int j = 0; j < m; j++)
      imos[i + 1][j] = imos[i][j];
    imos[i + 1][a[i]]++;
  }
  for (int i = 0; i < (1 << 20); i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  for (int i = 1; i < (1 << m); i++) {
    int sum = 0;
    for (int j = 0; j < m; j++) {
      if ((i >> j) & 1)
        sum += cnt[j];
    }
    for (int j = 0; j < m; j++) {
      if ((i >> j) & 1) {
        dp[i] = min(dp[i], dp[i & ~(1 << j)] + cnt[j] -
                               (imos[sum][j] - imos[sum - cnt[j]][j]));
      }
    }
  }
  cout << dp[(1 << m) - 1] << endl;
  return 0;
}
Problem p00557 (C++)

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

using namespace std;

int H, W;
int M[1000][1000];
int reach[1000][1000];
int x[100000], y[100000];
const int vy[] = {0, 1, 0, -1}, vx[] = {1, 0, -1, 0};

int main() {
  scanf("%d %d", &H, &W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%d", &M[i][j]);
      --M[i][j];
      x[M[i][j]] = j;
      y[M[i][j]] = i;
    }
  }
  int ret = 0;
  for (int i = 0; i < H * W; i++) {
    int proc = i;
    for (int j = 0; j < 4; j++) {
      int nx = x[i] + vx[j], ny = y[i] + vy[j];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (M[ny][nx] < i) {
        if (proc == i)
          proc = reach[ny][nx];
        else if (proc != reach[ny][nx])
          proc = -1;
      }
    }
    reach[y[i]][x[i]] = proc;
    ret += proc == -1;
  }

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

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

using namespace std;

int H, W;
int M[1000][1000];
int reach[1000][1000];
int x[1000000], y[1000000];
const int vy[] = {0, 1, 0, -1}, vx[] = {1, 0, -1, 0};

int main() {
  scanf("%d %d", &H, &W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%d", &M[i][j]);
      --M[i][j];
      x[M[i][j]] = j;
      y[M[i][j]] = i;
    }
  }
  int ret = 0;
  for (int i = 0; i < H * W; i++) {
    int proc = i;
    for (int j = 0; j < 4; j++) {
      int nx = x[i] + vx[j], ny = y[i] + vy[j];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (M[ny][nx] < i) {
        if (proc == i)
          proc = reach[ny][nx];
        else if (proc != reach[ny][nx])
          proc = -1;
      }
    }
    reach[y[i]][x[i]] = proc;
    ret += proc == -1;
  }

  printf("%d\n", ret);
}
Problem p00557 (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 pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
int a[1001][1001];
pint p[100002];
int b[100001], c[100001];
int main() {
  int h, w;
  cin >> h >> w;
  rep(i, h) rep(j, w) {
    cin >> a[i][j];
    p[a[i][j]] = {j, i};
  }
  int ans = 0;
  rep(l, h * w) {
    int j = p[l + 1].first, i = p[l + 1].second;
    int cnt = 0;
    rep(k, 4) {
      int ty = i + dy[k], tx = j + dx[k];
      if (ty >= 0 && tx >= 0 && ty < h && tx < w && a[ty][tx] < a[i][j]) {
        if (b[a[ty][tx]] == -1) {
          if (b[l + 1] == 0)
            c[l + 1] = a[ty][tx], b[l + 1] = 1;
          else if (b[l + 1] == 1 && c[l + 1] != a[ty][tx]) {
            ++ans, b[l + 1] = 2;
            cnt = 4;
            break;
          }
        } else if (b[a[ty][tx]] == 1) {
          if (b[l + 1] == 0) {
            c[l + 1] = c[a[ty][tx]];
            b[l + 1] = 1;
          } else {
            if (c[l + 1] != c[a[ty][tx]]) {
              b[l + 1] = 2;
              ++ans;
              cnt = 4;
              break;
            }
          }
        } else if (b[a[ty][tx]] == 2) {
          b[l + 1] = 2;
          ++ans;
          cnt = 4;
          break;
        }
      }
      if (ty >= 0 && tx >= 0 && ty < h && tx < w && a[ty][tx] < a[i][j])
        ++cnt;
    }
    if (cnt == 0)
      b[l + 1] = -1;
  }
  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 pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
int a[1001][1001];
pint p[1000002];
int b[1000002], c[1000002];
int main() {
  int h, w;
  cin >> h >> w;
  rep(i, h) rep(j, w) {
    cin >> a[i][j];
    p[a[i][j]] = {j, i};
  }
  int ans = 0;
  rep(l, h * w) {
    int j = p[l + 1].first, i = p[l + 1].second;
    int cnt = 0;
    rep(k, 4) {
      int ty = i + dy[k], tx = j + dx[k];
      if (ty >= 0 && tx >= 0 && ty < h && tx < w && a[ty][tx] < a[i][j]) {
        if (b[a[ty][tx]] == -1) {
          if (b[l + 1] == 0)
            c[l + 1] = a[ty][tx], b[l + 1] = 1;
          else if (b[l + 1] == 1 && c[l + 1] != a[ty][tx]) {
            ++ans, b[l + 1] = 2;
            cnt = 4;
            break;
          }
        } else if (b[a[ty][tx]] == 1) {
          if (b[l + 1] == 0) {
            c[l + 1] = c[a[ty][tx]];
            b[l + 1] = 1;
          } else {
            if (c[l + 1] != c[a[ty][tx]]) {
              b[l + 1] = 2;
              ++ans;
              cnt = 4;
              break;
            }
          }
        } else if (b[a[ty][tx]] == 2) {
          b[l + 1] = 2;
          ++ans;
          cnt = 4;
          break;
        }
      }
      if (ty >= 0 && tx >= 0 && ty < h && tx < w && a[ty][tx] < a[i][j])
        ++cnt;
    }
    if (cnt == 0)
      b[l + 1] = -1;
  }
  cout << ans << endl;
  return 0;
}
Problem p00558 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
struct st {
  int p, d, c, h;
  st() {}
  st(int p, int d, int c, int h) : p(p), d(d), c(c), h(h) {}
  bool operator<(const st &a) const { return d > a.d; }
};
struct edge {
  int to, cost;
  edge() {}
  edge(int to, int cost) : to(to), cost(cost) {}
};
vector<edge> G[10001];
int dp[10001][201][201];
int main() {
  int n, m, x;
  cin >> n >> m >> x;
  int s[n];
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < m; i++) {
    int a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    G[a].push_back(edge(b, d));
    G[b].push_back(edge(a, d));
  }
  memset(dp, -1, sizeof(dp));
  priority_queue<st> q;
  q.push(st(0, 0, x, 0));
  int ans = 0;
  while (!q.empty()) {
    st t = q.top();
    q.pop();
    if (~dp[t.p][t.c][t.h] && dp[t.p][t.c][t.h] <= t.d)
      continue;
    dp[t.p][t.c][t.h] = t.d;
    if (t.p == n - 1) {
      ans = t.d;
      break;
    }
    for (int i = 0; i < (int)G[t.p].size(); i++) {
      int v = G[t.p][i].to, u = G[t.p][i].cost;
      int c = max(0, t.c - u), h = max(0, t.h - u);
      if (~dp[v][c][h] && dp[v][c][h] <= t.d + u)
        continue;
      if (s[v] == 0) {
        if (h > 0)
          continue;
        q.push(st(v, t.d + u, x, h));
      }
      if (s[v] == 1) {
        q.push(st(v, t.d + u, c, h));
      }
      if (s[v] == 2) {
        if (c > 0)
          continue;
        q.push(st(v, t.d + u, c, x));
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct st {
  int p, d, c, h;
  st() {}
  st(int p, int d, int c, int h) : p(p), d(d), c(c), h(h) {}
  bool operator<(const st &a) const { return d > a.d; }
};
struct edge {
  int to, cost;
  edge() {}
  edge(int to, int cost) : to(to), cost(cost) {}
};
vector<edge> G[10001];
short dp[10001][201][201];
int main() {
  int n, m, x;
  cin >> n >> m >> x;
  int s[n];
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < m; i++) {
    int a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    G[a].push_back(edge(b, d));
    G[b].push_back(edge(a, d));
  }
  memset(dp, -1, sizeof(dp));
  priority_queue<st> q;
  q.push(st(0, 0, x, 0));
  int ans = 0;
  while (!q.empty()) {
    st t = q.top();
    q.pop();
    if (~dp[t.p][t.c][t.h] && dp[t.p][t.c][t.h] <= t.d)
      continue;
    dp[t.p][t.c][t.h] = t.d;
    if (t.p == n - 1) {
      ans = t.d;
      break;
    }
    for (int i = 0; i < (int)G[t.p].size(); i++) {
      int v = G[t.p][i].to, u = G[t.p][i].cost;
      int c = max(0, t.c - u), h = max(0, t.h - u);
      if (~dp[v][c][h] && dp[v][c][h] <= t.d + u)
        continue;
      if (s[v] == 0) {
        if (h > 0)
          continue;
        q.push(st(v, t.d + u, x, h));
      }
      if (s[v] == 1) {
        q.push(st(v, t.d + u, c, h));
      }
      if (s[v] == 2) {
        if (c > 0)
          continue;
        q.push(st(v, t.d + u, c, x));
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00559 (C++)

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

int main(void) {
  ll N, Q, S, T;
  cin >> N >> Q >> S >> T;
  vector<ll> A(N + 1);
  vector<ll> B(N);
  for (ll i = 0; i <= N; i++) {
    cin >> A[i];
  }

  for (ll i = 1; i <= N; i++) {
    B[i - 1] = A[i] - A[i - 1];
  }

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    if (B[i] > 0) {
      ans -= B[i] * S;
    } else {
      ans -= B[i] * T;
    }
  }

  for (int i = 0; i < Q; i++) {
    ll l, r, x;
    cin >> l >> r >> x;
    l--;

    if (B[l] > 0)
      ans += B[l] * S;
    else
      ans += B[l] * T;

    if (r != N) {
      if (B[r] > 0)
        ans += B[r] * S;
      else
        ans += B[r] * T;
    }

    B[l] += x;
    B[r] -= x;
    if (B[l] > 0) {
      ans -= B[l] * S;
    } else {
      ans -= B[l] * T;
    }

    if (r != N) {
      if (B[r] > 0) {
        ans -= B[r] * S;
      } else {
        ans -= B[r] * T;
      }
    }

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

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

int main(void) {
  ll N, Q, S, T;
  cin >> N >> Q >> S >> T;
  vector<ll> A(N + 1);
  vector<ll> B(N);
  for (ll i = 0; i <= N; i++) {
    cin >> A[i];
  }

  for (ll i = 1; i <= N; i++) {
    B[i - 1] = A[i] - A[i - 1];
  }

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    if (B[i] > 0) {
      ans -= B[i] * S;
    } else {
      ans -= B[i] * T;
    }
  }

  for (int i = 0; i < Q; i++) {
    ll l, r, x;
    cin >> l >> r >> x;
    l--;

    if (B[l] > 0)
      ans += B[l] * S;
    else
      ans += B[l] * T;

    if (r != N) {
      if (B[r] > 0)
        ans += B[r] * S;
      else
        ans += B[r] * T;
    }

    B[l] += x;
    if (r != N)
      B[r] -= x;
    if (B[l] > 0) {
      ans -= B[l] * S;
    } else {
      ans -= B[l] * T;
    }

    if (r != N) {
      if (B[r] > 0) {
        ans -= B[r] * S;
      } else {
        ans -= B[r] * T;
      }
    }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
vector<ll> v;

int main(void) {
  vector<ll> v;
  int n, k;
  ll t[10000];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    if (i != 0) {
      v.push_back(t[i] - t[i - 1] - 1);
    }
  }
  sort(v.begin(), v.end());
  ll ans = n;
  for (int i = 0; i < n - k; i++) {
    ans += v[i];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
vector<ll> v;

int main(void) {
  vector<ll> v;
  int n, k;
  ll t[1000000];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    if (i != 0) {
      v.push_back(t[i] - t[i - 1] - 1);
    }
  }
  sort(v.begin(), v.end());
  ll ans = n;
  for (int i = 0; i < n - k; i++) {
    ans += v[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p00570 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
vector<ll> v;

int main(void) {
  vector<ll> v;
  int n, k;
  ll t[10000];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    if (i != 0) {
      v.push_back(t[i] - t[i - 1] - 1);
    }
  }
  sort(v.begin(), v.end());
  ll ans = n;
  for (int i = 0; i < n - k; i++) {
    ans += v[i];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
vector<ll> v;

int main(void) {
  vector<ll> v;
  int n, k;
  ll t[100000];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    if (i != 0) {
      v.push_back(t[i] - t[i - 1] - 1);
    }
  }
  sort(v.begin(), v.end());
  ll ans = n;
  for (int i = 0; i < n - k; i++) {
    ans += v[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p00570 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int temp, n, k, t[100000], m, s[100000];
  cin >> n >> k;
  m = n;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < n - 1; i++) {
    s[i] = t[i + 1] - t[i] - 1;
  }
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      if (s[i] > s[j]) {
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
      }
    }
  }
  for (int i = 0; i < n - k; i++) {
    m += s[i];
  }
  cout << m << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int temp, n, k, t[100000], m, s[100000];
  cin >> n >> k;
  m = n;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < n - 1; i++) {
    s[i] = t[i + 1] - t[i] - 1;
  }

  sort(s, s + n - 1);

  for (int i = 0; i < n - k; i++) {
    m += s[i];
  }
  cout << m << endl;
  return 0;
}
Problem p00571 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info(ll arg_size, ll arg_value) {
    size = arg_size;
    value = arg_value;
  }
  ll size, value;
  bool operator<(const struct Info &arg) const { return size < arg.size; }
};

vector<Info> info;
ll ruisekiwa[100001], minus_value[100001];

int main() {

  int N;
  scanf("%d", &N);

  info.push_back(Info(0, 0));

  ll size, value;

  for (int i = 1; i <= N; i++) {
    scanf("%lld %lld", &size, &value);
    info.push_back(Info(size, value));
  }

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

  ruisekiwa[0] = 0;
  minus_value[0] = 0;

  for (int i = 1; i <= N; i++) {
    ruisekiwa[i] = info[i].value;
    ruisekiwa[i] += ruisekiwa[i - 1];
    minus_value[i] = ruisekiwa[i - 1] - info[i].size;
  }

  for (int i = 2; i <= N; i++) {
    minus_value[i] = min(minus_value[i], minus_value[i - 1]);
  }

  ll ans = -99999999999999999;

  for (int i = N; i >= 1; i--) {
    ans = max(ans, ruisekiwa[i] - info[i].size - minus_value[i]);
  }

  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info(ll arg_size, ll arg_value) {
    size = arg_size;
    value = arg_value;
  }
  ll size, value;
  bool operator<(const struct Info &arg) const { return size < arg.size; }
};

vector<Info> info;
ll ruisekiwa[500001], minus_value[500001];

int main() {

  int N;
  scanf("%d", &N);

  info.push_back(Info(0, 0));

  ll size, value;

  for (int i = 1; i <= N; i++) {
    scanf("%lld %lld", &size, &value);
    info.push_back(Info(size, value));
  }

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

  ruisekiwa[0] = 0;
  minus_value[0] = 0;

  for (int i = 1; i <= N; i++) {
    ruisekiwa[i] = info[i].value;
    ruisekiwa[i] += ruisekiwa[i - 1];
    minus_value[i] = ruisekiwa[i - 1] - info[i].size;
  }

  for (int i = 2; i <= N; i++) {
    minus_value[i] = min(minus_value[i], minus_value[i - 1]);
  }

  ll ans = -99999999999999999;

  for (int i = N; i >= 1; i--) {
    ans = max(ans, ruisekiwa[i] - info[i].size - minus_value[i]);
  }

  printf("%lld\n", ans);

  return 0;
}
Problem p00586 (C++)

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

int main() {
  int a, b;
  while (1) {
    cin >> a >> b;
    cout << a + b << endl;
  }
  return 0;
}

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

int main() {
  int a, b;
  while (cin >> a >> b) {
    cout << a + b << endl;
  }
  return 0;
}
Problem p00586 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;
int main(int argc, const char *argv[]) {

  // insert code here...
  int a, b;
  while (scanf("%d %d", &a, &b) != 0) {
    printf("%d\n", a + b);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;
int main(int argc, const char *argv[]) {

  // insert code here...
  int a, b;
  while (scanf("%d %d", &a, &b) != EOF) {
    printf("%d\n", a + b);
  }
  return 0;
}
Problem p00586 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main(void) {
  int a, b;
  while (cin >> a >> b, a + b)
    cout << a + b << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  int a, b;
  while (cin >> a >> b)
    cout << a + b << endl;
}
Problem p00586 (C++)

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

int main() {
  int a, b;
  while (scanf("%d %d", &a, &b)) {
    printf("%d\n", a + b);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

int main() {
  int a, b;
  while (scanf("%d %d", &a, &b) != EOF) {
    printf("%d\n", a + b);
  }
  return 0;
}
Problem p00586 (C++)

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

int main() {
  int a, b;
  while (true) {
    cin >> a >> b;
    cout << a + b << endl;
  }
  return 0;
}

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

int main() {
  int a, b;
  while (cin >> a >> b) {
    cout << a + b << endl;
  }
  return 0;
}
Problem p00586 (C++)

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

using namespace std;

int main() {
  int a, b;
  while (scanf("%d %d", &a, &b)) {
    for (int i = 0; i < b; i++)
      a++;
    cout << a << endl;
  }

  return 0;
}

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

using namespace std;

int main() {
  int a, b, c;
  while (scanf("%d %d", &a, &b) == 2) {
    cout << a + b << endl;
  }

  return 0;
}