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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef deque<bool> db;
template <class T> using vv = vector<vector<T>>;

int main() {
  int n;
  scanf("%d", &n);
  vi a(n);
  rep(i, n) { scanf("%d", &a[i]); }
  int m = 112345;
  vi b(m, 0);
  rep(i, n) {
    b[a[i] - 1] += 1;
    b[a[i]] += 1;
    b[a[i] + 1] += 1;
  }
  int ans = 0;
  rep(i, m) { ans = max(ans, b[i]); }
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef deque<bool> db;
template <class T> using vv = vector<vector<T>>;

int main() {
  int n;
  scanf("%d", &n);
  vi a(n);
  rep(i, n) { scanf("%d", &a[i]); }
  int m = 112345;
  vi b(m, 0);
  rep(i, n) {
    if (a[i] > 0) {
      b[a[i] - 1] += 1;
    }
    b[a[i]] += 1;
    b[a[i] + 1] += 1;
  }
  int ans = 0;
  rep(i, m) { ans = max(ans, b[i]); }
  printf("%d\n", ans);

  return 0;
}
Problem p03613 (C++)

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

int main() {
  int n, a, r = 0, s = 9999, b;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    f[a]++;
    f[a + 1]++;
    if (a != 0)
      f[a - 1]++;

    r = max(r, a);

    b = min(a, s);
    s = b;
  }
  int p = 0;
  if (b != 0)
    b--;

  for (int i = b; i < r + 1; i++) {
    p = max(p, f[i]);
  }
  printf("%d\n", p);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int f[100010];

int main() {
  int n, a, r = 0, s = 9999, b;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    f[a]++;
    f[a + 1]++;
    if (a != 0)
      f[a - 1]++;

    r = max(r, a);

    b = min(a, s);
    s = b;
  }
  int p = 0;
  if (b != 0)
    b--;

  for (int i = b; i < r + 1; i++) {
    p = max(p, f[i]);
  }
  printf("%d\n", p);

  return 0;
}
Problem p03613 (C++)

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

using namespace std;

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

  int max = 0;
  int ans = 0;
  sort(a.begin(), a.end());
  vector<int> b(1 + a[N]);
  for (int i = 0; i < N; i++) {
    b[1 + a[i]] += 1;
    b[1 + a[i] - 1] += 1;
    b[1 + a[i] + 1] += 1;
  }
  for (int i = 0; i < 1 + a[N - 1]; i++) {
    // cout<<b[i]<<endl;
    if (b[i] > max) {
      max = b[i];
      // ans=i-1;
    }
  }
  cout << max << endl;
  return 0;
}


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

using namespace std;

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

  int max = 0;
  int ans = 0;
  sort(a.begin(), a.end());
  vector<int> b(2 + a[N - 1]);
  for (int i = 0; i < N; i++) {
    b[1 + a[i]] += 1;
    b[1 + a[i] - 1] += 1;
    b[1 + a[i] + 1] += 1;
  }
  for (int i = 0; i < 1 + a[N - 1]; i++) {
    // cout<<b[i]<<endl;
    if (b[i] > max) {
      max = b[i];
      // ans=i-1;
    }
  }
  cout << max << endl;
  return 0;
}
Problem p03613 (C++)

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

using namespace std;

void solve() {
  int n;
  cin >> n;
  map<int, int> cnt;
  for (int _ = 0; _ < n; _++) {
    int x;
    cin >> x;
    cnt[x]++;
  }
  int res = 0;
  for (auto &p : cnt) {
    int tmp = p.second;
    tmp += cnt[p.first - 1] + cnt[p.first + 1];
    res = max(res, tmp);
  }
  cout << res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}


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

using namespace std;

void solve() {
  int n;
  cin >> n;
  map<int, int> cnt;
  for (int _ = 0; _ < n; _++) {
    int x;
    cin >> x;
    cnt[x]++;
  }
  int res = 0;
  for (auto &p : cnt) {
    int tmp = p.second;
    if (cnt.find(p.first - 1) != cnt.end())
      tmp += cnt[p.first - 1];
    if (cnt.find(p.first + 1) != cnt.end())
      tmp += cnt[p.first + 1];
    res = max(res, tmp);
  }
  cout << res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}
Problem p03613 (C++)

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

using namespace std;

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

  vector<int> cnt(100002, 0);

  for (int i = 0; i < N; i++) {
    cnt[A[i]]++;
    cnt[A[i] + 1]++;
    cnt[A[i] - 1]++;
  }

  int ret = 0;
  for (int i = 0; i < 100002; i++) {
    ret = max(ret, cnt[i]);
  }
  cout << ret << endl;
  return 0;
}

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

using namespace std;

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

  vector<int> cnt(100002, 0);

  for (int i = 0; i < N; i++) {
    cnt[A[i]]++;
    cnt[A[i] + 1]++;
    cnt[A[i] + 2]++;
  }

  int ret = 0;
  for (int i = 0; i < 100002; i++) {
    ret = max(ret, cnt[i]);
  }
  cout << ret << endl;
  return 0;
}
Problem p03613 (C++)

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> s(n + 2, 0);
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    s[a]++;
    s[a + 1]++;
    s[a + 2]++;
  }
  int ans = 0;
  for (int i = 0; i < s.size(); i++) {
    if (ans < s[i])
      ans = s[i];
  }
  cout << ans << endl;
}

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> s(100002, 0);
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    s[a]++;
    s[a + 1]++;
    s[a + 2]++;
  }
  int ans = 0;
  for (int i = 0; i < s.size(); i++) {
    if (ans < s[i])
      ans = s[i];
  }
  cout << ans << endl;
}
Problem p03614 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ul;

const int iinf = 1 << 29;
const long long linf = 1l << 61;

int N;
int p[10800];

int main(int argc, char *argv[]) {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &p[i]);

  int ans = 0;
  int j = 0;
  for (int i = 0; i < N; i++) {
    if (p[i] == i + 1) {
      j++;
    } else {
      ans += j / 2 + j % 2;
      j = 0;
    }
  }
  ans += j / 2 + j % 2;
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ul;

const int iinf = 1 << 29;
const long long linf = 1l << 61;

int N;
int p[100800];

int main(int argc, char *argv[]) {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &p[i]);

  int ans = 0;
  int j = 0;
  for (int i = 0; i < N; i++) {
    if (p[i] == i + 1) {
      j++;
    } else {
      ans += j / 2 + j % 2;
      j = 0;
    }
  }
  ans += j / 2 + j % 2;
  printf("%d\n", ans);

  return 0;
}
Problem p03614 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
#define inf 0x3f3f3f3f;
set<int> s;
int gh[1000][1000], w[2000], dis[1000][1000], pre[2000];
bool vis[20000] = {0};

int main() {
  int i, j, k, l, n, m, r, num;
  int sum;
  int a[10000];
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = 1;
  num = 0;
  for (i = 1; i <= n; i++)
    if (a[i] == i) {
      if (a[i - 1] == i - 1) {
        a[i] = i - 1;
        a[i - 1] = i;
      } else
        num++;
    }
  cout << num << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
#define inf 0x3f3f3f3f;
set<int> s;
int gh[1000][1000], w[2000], dis[1000][1000], pre[2000];
bool vis[20000] = {0};

int main() {
  int i, j, k, l, n, m, r, num;
  int sum;
  int a[110000];
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = 1;
  num = 0;
  for (i = 1; i <= n; i++)
    if (a[i] == i) {
      if (a[i - 1] == i - 1) {
        a[i] = i - 1;
        a[i - 1] = i;
      } else
        num++;
    }
  cout << num << endl;

  return 0;
}
Problem p03614 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
using namespace std;

int main(void) {
  int n;
  int low[n + 1];
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> low[i];
  int count = 0;
  for (int i = 1; i <= n; i++) {
    if (low[i] == i) {
      count++;
      i++;
    }
  }
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
using namespace std;

int main(void) {
  int n;
  int low[100005];
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> low[i];
  int count = 0;
  for (int i = 1; i <= n; i++) {
    if (low[i] == i) {
      count++;
      i++;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p03614 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define F first
#define S second
#define pb push_back
#define sd(x) scanf("%d", &x)
#define sd2(x, y) scanf("%d%d", &x, &y);
#define sdl(x) scanf("%lld", &x)
#define sd2l(x, y) scanf("%lld%lld", &x, &y);
#define sz(x) (int)(x.size())
#define nax 20110
#define inf 1001000100
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define MOD 1000000007
#define mp make_pair
#define pi pair<int, int>
#define pii pair<pi, pi>
using namespace std;
int arr[nax];
int brr[nax];
int main(int argc, char const *argv[]) {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int n;
  sd(n);
  for (int i = 1; i <= n; i++) {
    sd(arr[i]);
    brr[i] = i;
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (arr[i] == i) {
      swap(arr[i], arr[i + 1]);
      ++cnt;
    }
  }
  cout << cnt << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define F first
#define S second
#define pb push_back
#define sd(x) scanf("%d", &x)
#define sd2(x, y) scanf("%d%d", &x, &y);
#define sdl(x) scanf("%lld", &x)
#define sd2l(x, y) scanf("%lld%lld", &x, &y);
#define sz(x) (int)(x.size())
#define nax 100110
#define inf 1001000100
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define MOD 1000000007
#define mp make_pair
#define pi pair<int, int>
#define pii pair<pi, pi>
using namespace std;
int arr[nax];
int brr[nax];
int main(int argc, char const *argv[]) {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int n;
  sd(n);
  for (int i = 1; i <= n; i++) {
    sd(arr[i]);
    brr[i] = i;
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (arr[i] == i) {
      swap(arr[i], arr[i + 1]);
      ++cnt;
    }
  }
  cout << cnt << '\n';
  return 0;
}
Problem p03614 (C++)

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

int main() {
  long long n;

  std::cin >> n;

  std::vector<long long> v(n);

  for (int i = 0; i < n; i++) {
    std::cin >> v[i];
  }

  long long count = 0;

  for (int i = 0; i < v.size(); i++) {
    if (v[i] == i + 1) {
      if (i == 0) {
        std::swap(v[0], v[1]);
      } else {
        if (v[i + 1] != i + 1) {
          std::swap(v[i + 1], v[i]);
        } else {
          std::swap(v[i - 1], v[i]);
        }
      }
      count++;
    }
  }

  std::cout << count << std::endl;

  return 0;
}

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

int main() {
  long long n;

  std::cin >> n;

  std::vector<long long> v(n);

  for (int i = 0; i < n; i++) {
    std::cin >> v[i];
  }

  long long count = 0;

  for (int i = 0; i < v.size(); i++) {
    if (v[i] == i + 1) {
      if (i == 0) {
        std::swap(v[0], v[1]);
      } else {
        if (i + 1 != n && v[i + 1] != i + 1) {
          std::swap(v[i + 1], v[i]);
        } else {
          std::swap(v[i - 1], v[i]);
        }
      }
      count++;
    }
  }

  std::cout << count << std::endl;

  return 0;
}
Problem p03615 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter
from math import gcd

n = int(input())
xys = [tuple(map(int, input().split())) for _ in range(n)]
MOD = 998244353

excludes = 0
for i, (x1, y1) in enumerate(xys):
    slopes = []
    for x2, y2 in xys[i + 1 :]:
        dx, dy = x2 - x1, y2 - y1
        if dx == 0:
            slopes.append(1j)
        elif dy == 0:
            slopes.append(1)
        else:
            m = gcd(dx, dy)
            slopes.append(dx // m + dy // m * 1j)
    for c in Counter(slopes).values():
        if c > 1:
            excludes += 2**c - c - 1
    excludes %= MOD

print((pow(2, n, MOD) - excludes - (n * (n - 1) // 2) - n - 1) % MOD)


Fixed submission:
from collections import Counter
from fractions import gcd

n = int(input())
xys = [tuple(map(int, input().split())) for _ in range(n)]
MOD = 998244353

excludes = 0
for i, (x1, y1) in enumerate(xys):
    slopes = []
    for x2, y2 in xys[i + 1 :]:
        dx, dy = x2 - x1, y2 - y1
        if dx == 0:
            slopes.append(1j)
        elif dy == 0:
            slopes.append(1)
        else:
            m = gcd(dx, dy)
            slopes.append(dx // m + dy // m * 1j)
    for c in Counter(slopes).values():
        if c > 1:
            excludes += 2**c - c - 1
    excludes %= MOD

print((pow(2, n, MOD) - excludes - (n * (n - 1) // 2) - n - 1) % MOD)
Problem p03615 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 222, mod = 998244353;
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
inline int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
struct line {
  double k, b;
  bool operator<(const line &l) const {
    if (fabs(k - l.k) > 1e-8)
      return k < l.k;
    return b < l.b;
  }
} l[maxn];
int n, ans, pt[maxn], x[maxn], y[maxn], llen, cnt[25555];
inline int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int main() {
  n = read();
  FOR(i, 1, n) x[i] = read(), y[i] = read();
  pt[0] = 1;
  FOR(i, 1, n) pt[i] = 2 * pt[i - 1] % mod;
  ans = (pt[n] - (n + 1) + mod) % mod;
  FOR(i, 1, n) FOR(j, i + 1, n) {
    double k, b;
    if (x[i] == x[j])
      k = 1e9, b = x[i];
    else
      k = 1.0 * (y[j] - y[i]) / (x[j] - x[i]), b = y[i] - k * x[i];
    l[++llen] = (line){k, b};
  }
  sort(l + 1, l + llen + 1);
  FOR(i, 2, n) cnt[i * (i - 1) / 2] = i;
  FOR(i, 1, llen) {
    int j = i;
    while (j <= llen && fabs(l[i].k - l[j].k) < 1e-8 &&
           fabs(l[i].b - l[j].b) < 1e-8)
      j++;
    j--;
    int x = cnt[j - i + 1];
    ans = (ans - (pt[x] - (x + 1) + mod) % mod + mod) % mod;
    i = j;
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 222, mod = 998244353;
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
inline int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
struct line {
  double k, b;
  bool operator<(const line &l) const {
    if (fabs(k - l.k) > 1e-8)
      return k < l.k;
    return b < l.b;
  }
} l[25555];
int n, ans, pt[maxn], x[maxn], y[maxn], llen, cnt[25555];
inline int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int main() {
  n = read();
  FOR(i, 1, n) x[i] = read(), y[i] = read();
  pt[0] = 1;
  FOR(i, 1, n) pt[i] = 2 * pt[i - 1] % mod;
  ans = (pt[n] - (n + 1) + mod) % mod;
  FOR(i, 1, n) FOR(j, i + 1, n) {
    double k, b;
    if (x[i] == x[j])
      k = 1e9, b = x[i];
    else
      k = 1.0 * (y[j] - y[i]) / (x[j] - x[i]), b = y[i] - k * x[i];
    l[++llen] = (line){k, b};
  }
  sort(l + 1, l + llen + 1);
  FOR(i, 2, n) cnt[i * (i - 1) / 2] = i;
  FOR(i, 1, llen) {
    int j = i;
    while (j <= llen && fabs(l[i].k - l[j].k) < 1e-8 &&
           fabs(l[i].b - l[j].b) < 1e-8)
      j++;
    j--;
    int x = cnt[j - i + 1];
    ans = (ans - (pt[x] - (x + 1) + mod) % mod + mod) % mod;
    i = j;
  }
  printf("%d\n", ans);
}
Problem p03616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 100005
using namespace std;
int X, n, m, q, op, ans[N];
struct node {
  int t, a, num;
  node(int _t = 0, int _a = 0, int _num = 0) { t = _t, a = _a, num = _num; }
} a[N];
bool cmp(const node &p, const node &q) {
  return p.t < q.t || (p.t == q.t && p.num < q.num);
}
int main() {
  int i, x, y, p, l, r, v;
  scanf("%d", &X);
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &x), a[++m] = node(x, 0, 0);
  scanf("%d", &q);
  for (i = 1; i <= q; i++)
    scanf("%d %d", &x, &y), a[++m] = node(x, y, i);
  sort(a + 1, a + m + 1, cmp);
  op = -1, p = l = v = 0, r = X;
  for (i = 1; i <= m; i++) {
    y = (a[i].t - p) * op;
    if (a[i].num != 0) {
      x = a[i].a + v;
      x = min(x, r);
      x = max(x, l);
      x += y;
      x = min(x, X);
      x = max(x, 0);
      ans[a[i].num] = x;
    } else {
      r += y, l += y, v += y;
      r = min(r, X), r = max(r, 0);
      l = min(l, X), l = max(l, 0);
      p = a[i].t, op = -op;
    }
  }
  for (i = 1; i <= q; i++)
    printf("%d\n", ans[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define N 300005
using namespace std;
int X, n, m, q, op, ans[N];
struct node {
  int t, a, num;
  node(int _t = 0, int _a = 0, int _num = 0) { t = _t, a = _a, num = _num; }
} a[N];
bool cmp(const node &p, const node &q) {
  return p.t < q.t || (p.t == q.t && p.num < q.num);
}
int main() {
  int i, x, y, p, l, r, v;
  scanf("%d", &X);
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &x), a[++m] = node(x, 0, 0);
  scanf("%d", &q);
  for (i = 1; i <= q; i++)
    scanf("%d %d", &x, &y), a[++m] = node(x, y, i);
  sort(a + 1, a + m + 1, cmp);
  op = -1, p = l = v = 0, r = X;
  for (i = 1; i <= m; i++) {
    y = (a[i].t - p) * op;
    if (a[i].num != 0) {
      x = a[i].a + v;
      x = min(x, r);
      x = max(x, l);
      x += y;
      x = min(x, X);
      x = max(x, 0);
      ans[a[i].num] = x;
    } else {
      r += y, l += y, v += y;
      r = min(r, X), r = max(r, 0);
      l = min(l, X), l = max(l, 0);
      p = a[i].t, op = -op;
    }
  }
  for (i = 1; i <= q; i++)
    printf("%d\n", ans[i]);
  return 0;
}
Problem p03616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MN 100005
int t, r[MN], a, v[MN], x, n, q;
int main() {
  cin >> x >> n;
  for (int i = 1; i <= n; ++i)
    scanf("%d", r + i);
  for (int i = 1; i <= n; ++i)
    v[i] = r[i] - r[i - 1];
  cin >> q;
  int tl = 0, h = x, l = 0, hv = x, lv = 0;
  v[n + 1] = 1000000008;
  while (q--) {
    scanf("%d%d", &t, &a);
    while (r[tl + 1] <= t) {
      if ((++tl) & 1) {
        if (lv < v[tl])
          l += v[tl] - lv;
        hv = max(0, hv - v[tl]);
        lv = max(0, lv - v[tl]);
      } else {
        if (hv + v[tl] > x)
          h -= v[tl] + hv - x;
        hv = min(x, hv + v[tl]);
        lv = min(x, lv + v[tl]);
      }
      h = max(0, h);
      l = min(l, x);
    }
    t -= r[tl];
    if (l >= a)
      a = lv;
    else if (h <= a)
      a = hv;
    else
      a += lv - l;
    if (tl & 1)
      printf("%d\n", min(x, a + t));
    else
      printf("%d\n", max(0, a - t));
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MN 100005
int t, r[MN], a, v[MN], x, n, q;
int main() {
  cin >> x >> n;
  for (int i = 1; i <= n; ++i)
    scanf("%d", r + i);
  for (int i = 1; i <= n; ++i)
    v[i] = r[i] - r[i - 1];
  cin >> q;
  int tl = 0, h = x, l = 0, hv = x, lv = 0;
  r[n + 1] = v[n + 1] = 1000000008;
  while (q--) {
    scanf("%d%d", &t, &a);
    while (r[tl + 1] <= t) {
      if ((++tl) & 1) {
        if (lv < v[tl])
          l += v[tl] - lv;
        hv = max(0, hv - v[tl]);
        lv = max(0, lv - v[tl]);
      } else {
        if (hv + v[tl] > x)
          h -= v[tl] + hv - x;
        hv = min(x, hv + v[tl]);
        lv = min(x, lv + v[tl]);
      }
      h = max(0, h);
      l = min(l, x);
    }
    t -= r[tl];
    if (l >= a)
      a = lv;
    else if (h <= a)
      a = hv;
    else
      a += lv - l;
    if (tl & 1)
      printf("%d\n", min(x, a + t));
    else
      printf("%d\n", max(0, a - t));
  }
  return 0;
}
Problem p03616 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 2e5;
int gi() {
  int w = 0;
  bool q = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-')
    q = 0, c = getchar();
  while (c >= '0' && c <= '9')
    w = w * 10 + c - '0', c = getchar();
  return q ? w : -w;
}
int R[N], ans[N];
struct P {
  int t, a, id;
  inline bool operator<(const P &b) const { return t < b.t; }
} p[N];
int main() {
  int s = gi(), n = gi(), m, i, t, k, l = 0, r = s, d, sum = 0;
  for (i = 1; i <= n; i++)
    R[i] = gi();
  for (i = 1, m = gi(); i <= m; i++)
    p[i].t = gi(), p[i].a = gi(), p[i].id = i;
  sort(p + 1, p + 1 + m);
  for (i = k = 1, d = -1; i <= m; i++) {
    for (; R[k] <= p[i].t; k++, d *= -1) {
      l = max(0, l + d * (R[k] - R[k - 1]));
      l = min(l, s);
      r = max(0, r + d * (R[k] - R[k - 1]));
      r = min(r, s);
      sum += d * (R[k] - R[k - 1]);
    }
    t = max(p[i].a + sum, l);
    t = min(t, r);
    t = max(0, t + d * (p[i].t - R[k - 1]));
    t = min(t, s);
    ans[p[i].id] = t;
  }
  for (i = 1; i <= m; i++)
    printf("%d\n", ans[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 2e5;
int gi() {
  int w = 0;
  bool q = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-')
    q = 0, c = getchar();
  while (c >= '0' && c <= '9')
    w = w * 10 + c - '0', c = getchar();
  return q ? w : -w;
}
int R[N], ans[N];
struct P {
  int t, a, id;
  inline bool operator<(const P &b) const { return t < b.t; }
} p[N];
int main() {
  int s = gi(), n = gi(), m, i, t, k, l = 0, r = s, d, sum = 0;
  for (i = 1; i <= n; i++)
    R[i] = gi();
  for (i = 1, m = gi(); i <= m; i++)
    p[i].t = gi(), p[i].a = gi(), p[i].id = i;
  sort(p + 1, p + 1 + m);
  for (i = k = 1, d = -1; i <= m; i++) {
    for (; k <= n && R[k] <= p[i].t; k++, d *= -1) {
      l = max(0, l + d * (R[k] - R[k - 1]));
      l = min(l, s);
      r = max(0, r + d * (R[k] - R[k - 1]));
      r = min(r, s);
      sum += d * (R[k] - R[k - 1]);
    }
    t = max(p[i].a + sum, l);
    t = min(t, r);
    t = max(0, t + d * (p[i].t - R[k - 1]));
    t = min(t, s);
    ans[p[i].id] = t;
  }
  for (i = 1; i <= m; i++)
    printf("%d\n", ans[i]);
  return 0;
}
Problem p03616 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int MX = 205;
const int MM = 998244353;

struct EV {
  int o, t, a, ad;
  EV(int o, int t, int a, int ad) : o(o), t(t), a(a), ad(ad) {}
  bool operator<(EV l) const { return t < l.t; }
};

int N;
int ans[MX], D[MX];

int main() {
  vector<EV> L;
  int X, K, Q, t, a;
  scanf("%d%d", &X, &K);
  for (int i = 1; i <= K; i++) {
    scanf("%d", D + i);
    L.emplace_back(0, D[i], 0, 0);
  }
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    scanf("%d%d", &t, &a);
    L.emplace_back(1, t, a, i);
  }

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

  int l = 0, r = X, u = 0, ch = 0, b = 0;
  for (EV c : L) {
    if (c.o == 0) {
      int d = c.t - b;
      b = c.t;
      if (ch) {
        int v = X - (u + (r - l));
        r = max(l, r - max(0, d - v));
        u = min(X, u + d);
      } else {
        l = min(r, l + max(0, d - u));
        u = max(0, u - d);
      }
      ch = !ch;
    } else {
      int d = c.t - b, a = c.a;
      int v = u + min(r - l, max(0, a - l));
      if (!ch) {
        ans[c.ad] = max(0, v - d);
      } else
        ans[c.ad] = min(X, v + d);
    }
  }

  for (int i = 1; i <= Q; i++)
    printf("%d\n", ans[i]);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int MX = 100005;
const int MM = 998244353;

struct EV {
  int o, t, a, ad;
  EV(int o, int t, int a, int ad) : o(o), t(t), a(a), ad(ad) {}
  bool operator<(EV l) const { return t < l.t; }
};

int N;
int ans[MX], D[MX];

int main() {
  vector<EV> L;
  int X, K, Q, t, a;
  scanf("%d%d", &X, &K);
  for (int i = 1; i <= K; i++) {
    scanf("%d", D + i);
    L.emplace_back(0, D[i], 0, 0);
  }
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    scanf("%d%d", &t, &a);
    L.emplace_back(1, t, a, i);
  }

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

  int l = 0, r = X, u = 0, ch = 0, b = 0;
  for (EV c : L) {
    if (c.o == 0) {
      int d = c.t - b;
      b = c.t;
      if (ch) {
        int v = X - (u + (r - l));
        r = max(l, r - max(0, d - v));
        u = min(X, u + d);
      } else {
        l = min(r, l + max(0, d - u));
        u = max(0, u - d);
      }
      ch = !ch;
    } else {
      int d = c.t - b, a = c.a;
      int v = u + min(r - l, max(0, a - l));
      if (!ch) {
        ans[c.ad] = max(0, v - d);
      } else
        ans[c.ad] = min(X, v + d);
    }
  }

  for (int i = 1; i <= Q; i++)
    printf("%d\n", ans[i]);
}
Problem p03617 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long ll;
double n;
struct Cup {
  double cupp;
  int val;
  bool operator<(const Cup &rhs) const {
    return double(rhs.val) / rhs.cupp > double(val) / cupp;
  }
} a[5];
ll ans = 0;
int main() {
  a[1].cupp = 0.25;
  a[2].cupp = 0.5;
  a[3].cupp = 1;
  a[4].cupp = 2;
  scanf("%d%d%d%d%lf", &a[1].val, &a[2].val, &a[3].val, &a[4].val, &n);
  sort(a + 1, a + 1 + 4);
  while (n) {
    for (int i = 1; i <= 4; i++) {
      if (n >= a[i].cupp) {
        ans += a[i].val;
        n -= a[i].cupp;
        break;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long ll;
double n;
struct Cup {
  double cupp;
  int val;
  bool operator<(const Cup &rhs) const {
    return double(rhs.val) / rhs.cupp > double(val) / cupp;
  }
} a[5];
ll ans = 0;
int main() {
  a[1].cupp = 0.25;
  a[2].cupp = 0.5;
  a[3].cupp = 1;
  a[4].cupp = 2;
  scanf("%d%d%d%d%lf", &a[1].val, &a[2].val, &a[3].val, &a[4].val, &n);
  sort(a + 1, a + 1 + 4);
  for (int i = 1; i <= 4; i++) {
    if (n >= a[i].cupp) {
      ll tmp = floor(n / a[i].cupp);
      ans += ll(a[i].val) * tmp;
      n -= a[i].cupp * tmp;
    }
  }
  cout << ans << endl;
}
Problem p03617 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define VI vector<int>
#define debug(x) cout << #x << ": " << x << endl
#define DEBUG(x) cout << #x << ": " << x << endl
double Q, H, S, D;
int N;
vector<pair<double, int>> q;

int main() {

  cin >> Q >> H >> S >> D >> N;

  q.push_back({Q / 0.25, 0});
  q.push_back({H / 0.5, 1});
  q.push_back({S, 2});
  q.push_back({D / 2, 3});

  double li[4] = {0.25, 0.5, 1, 2};
  double cost[4] = {Q, H, S, D};

  sort(q.begin(), q.end());
  int a = (int)N / li[q[0].second];
  int b = li[q[1].second];

  long double ans = (long double)a * cost[q[0].second];

  if (q[0].second == 3) {
    int c = (N % 2) / b;
    ans += (long double)c * cost[q[1].second];
  }
  cout << fixed << setprecision(0) << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define VI vector<int>
#define debug(x) cout << #x << ": " << x << endl
#define DEBUG(x) cout << #x << ": " << x << endl
double Q, H, S, D;
int N;
vector<pair<double, int>> q;

int main() {

  cin >> Q >> H >> S >> D >> N;

  q.push_back({Q / 0.25, 0});
  q.push_back({H / 0.5, 1});
  q.push_back({S, 2});
  q.push_back({D / 2, 3});

  double li[4] = {0.25, 0.5, 1, 2};
  double cost[4] = {Q, H, S, D};

  sort(q.begin(), q.end());
  int a = (int)N / li[q[0].second];
  double b = li[q[1].second];

  long double ans = (long double)a * cost[q[0].second];

  if (q[0].second == 3) {
    int c = (N % 2) / b;
    ans += (long double)c * cost[q[1].second];
  }
  cout << fixed << setprecision(0) << ans << endl;
}
Problem p03617 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ALL(A) (A).begin(), (A).end()
#define ll long long
const ll mod = 1e9 + 7;
const ll INF = 2 * 1e18;
const int inf = 1e9 + 7;

int main(void) {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  long long N;
  cin >> N;
  if (b > 2 * a)
    b = 2 * a;
  if (c > 2 * b)
    c = 2 * b;
  if (c > 4 * a)
    c = 4 * a;
  if (d > 8 * a)
    d = 8 * a;
  if (d > 4 * b)
    d = 4 * b;
  if (d > 2 * c)
    d = 2 * c;
  long long ans = INF;
  // c を i 個購入すると考える
  for (int i = 0; i <= N; i++) {
    long long now = i * c;
    int rest = N - i;
    if (rest > 2) {
      now += (rest / 2) * d;
      rest -= (rest / 2) * 2;
    }
    if (rest > 0) {
      now += (rest * 2) * b;
    }
    ans = min(now, ans);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(A) (A).begin(), (A).end()
#define ll long long
const ll mod = 1e9 + 7;
const ll INF = 2 * 1e18;
const int inf = 1e9 + 7;

int main(void) {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  long long N;
  cin >> N;
  if (b > 2 * a)
    b = 2 * a;
  if (c > 2 * b)
    c = 2 * b;
  if (c > 4 * a)
    c = 4 * a;
  if (d > 8 * a)
    d = 8 * a;
  if (d > 4 * b)
    d = 4 * b;
  if (d > 2 * c)
    d = 2 * c;
  long long ans = 0;
  int dc = N / 2;
  ans += dc * d;
  int cc = N - dc * 2;
  ans += cc * c;
  cout << ans << endl;
}
Problem p03617 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_DEPRECATE
#define rep(i, b, e) for (auto i = b; i < e; ++i)
#include <cmath>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#ifndef __GNUC__
#include <intrin.h>
#endif // !__GNUC__
#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

int bsf(int x) {
#ifdef __GNUC__
  return __builtin_ctz(x);
#else
  unsigned long i;
  _BitScanForward(&i, x);
  return i;
#endif
}
int bsr(int x) {
#ifdef __GNUC__
  return 31 - __builtin_clz(x);
#else
  unsigned long i;
  _BitScanReverse(&i, x);
  return i;
#endif
}

using namespace std;
typedef long long int64;
const double pi = 3.14159265358979323846;
const int inf = (int)2e+9 + 2;
const int mod = (int)1e+9;
const double eps = 1e-8;
const int N = 1 << 20;

int n, m, k;
int c[4];

void solve() {
  rep(i, 0, 4) cin >> c[i];
  cin >> n;
  int best1 = c[2], best2 = c[3];
  best1 = min(best1, c[0] * 4);
  best1 = min(best1, c[1] * 2);
  best2 = min(best2, best1 * 2);
  if (n & 1)
    cout << (int64)(n / 2) * best2 + best1;
  else
    cout << (int64)(n / 2) * best2;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif // !ONLINE_JUDGE
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int test = 1;
  // cin >> test;
  for (; test; --test)
    solve();
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_DEPRECATE
#define rep(i, b, e) for (auto i = b; i < e; ++i)
#include <cmath>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#ifndef __GNUC__
#include <intrin.h>
#endif // !__GNUC__
#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

int bsf(int x) {
#ifdef __GNUC__
  return __builtin_ctz(x);
#else
  unsigned long i;
  _BitScanForward(&i, x);
  return i;
#endif
}
int bsr(int x) {
#ifdef __GNUC__
  return 31 - __builtin_clz(x);
#else
  unsigned long i;
  _BitScanReverse(&i, x);
  return i;
#endif
}

using namespace std;
typedef long long int64;
const double pi = 3.14159265358979323846;
const int inf = (int)2e+9 + 2;
const int mod = (int)1e+9;
const double eps = 1e-8;
const int N = 1 << 20;

int n, m, k;
int c[4];

void solve() {
  rep(i, 0, 4) cin >> c[i];
  cin >> n;
  int best1 = c[2], best2 = c[3];
  best1 = min(best1, c[0] * 4);
  best1 = min(best1, c[1] * 2);
  best2 = min(best2, best1 * 2);
  if (n & 1)
    cout << (int64)(n / 2) * best2 + best1;
  else
    cout << (int64)(n / 2) * best2;
}

int main() {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif // !ONLINE_JUDGE
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int test = 1;
  // cin >> test;
  for (; test; --test)
    solve();
  return 0;
}
Problem p03617 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct maps {
  long long w;
  int v, vv;
} a[5];
bool cmp(maps a, maps b) { return a.w < b.w; }
int main() {
  int i, j, w[5], v[5] = {25, 50, 100, 200}, vv[5] = {8, 4, 2, 1};
  for (i = 0; i < 4; i++) {
    scanf("%lld", &a[i].w);
    a[i].w = a[i].w * vv[i];
    a[i].v = v[i];
    a[i].vv = vv[i];
  }
  long long s = 0, n;
  sort(a, a + 4, cmp);
  scanf("%lld", &n);
  n = n * 100;
  for (i = 0; i < 4 && n > 0;) {
    if (n >= a[i].v) {
      s += a[i].w / a[i].vv;
      n -= a[i].v;
    } else {
      i++;
    }
  }
  printf("%lld\n", s);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct maps {
  long long w;
  int v, vv;
} a[5];
bool cmp(maps a, maps b) { return a.w < b.w; }
int main() {
  int i, j, w[5], v[5] = {25, 50, 100, 200}, vv[5] = {8, 4, 2, 1};
  for (i = 0; i < 4; i++) {
    scanf("%lld", &a[i].w);
    a[i].w = a[i].w * vv[i];
    a[i].v = v[i];
    a[i].vv = vv[i];
  }
  long long s = 0, n;
  sort(a, a + 4, cmp);
  scanf("%lld", &n);
  n = n * 100;
  for (i = 0; i < 4 && n > 0;) {
    if (n >= a[i].v) {
      int xx = n / a[i].v;
      // printf("** %d\n",xx);
      s += (a[i].w * xx) / a[i].vv;
      n %= a[i].v;
    } else {
      i++;
    }
  }
  printf("%lld\n", s);
  return 0;
}
Problem p03617 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
const ll MOD = 1000000007;

int main() {
  ll Q, H, S, D, N;
  cin >> Q >> H >> S >> D >> N;
  int w[4] = {1, 2, 4, 8};
  ll v[4] = {Q, H, S, D};
  vector<ll> dp(4 * N + 1, 1000000000000000000);
  dp[0] = 0;
  for (int i = 1; i <= 4 * N; i++) {
    for (int j = 0; j < 4; j++) {
      if (i >= w[j]) {
        dp[i] = min(dp[i], dp[i - w[j]] + v[j]);
      }
    }
  }
  cout << dp[4 * N] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
const ll MOD = 1000000007;

int main() {
  ll Q, H, S, D, N;
  cin >> Q >> H >> S >> D >> N;
  ll x = min(min(4 * Q, 2 * H), S);
  ll y = min(2 * x, D);
  ll ans = N / 2 * y + N % 2 * x;
  cout << ans << endl;
}
Problem p03617 (Python)

Buggy submission (status: Runtime Error):
def main(q: int, h: int, s: int, d: int, n: int):
    print((n // 2) * min(q * 8, h * 4, s * 2, d) + (n % 2) * min(q * 4, h * 2, s))


if __name__ == "__main__":
    q, h, s, d = map(int, input().split())
    n = int(input)

    main(q, h, s, d, n)


Fixed submission:
def main(q: int, h: int, s: int, d: int, n: int):
    print((n // 2) * min(q * 8, h * 4, s * 2, d) + (n % 2) * min(q * 4, h * 2, s))


if __name__ == "__main__":
    q, h, s, d = map(int, input().split())
    n = int(input())

    main(q, h, s, d, n)
Problem p03617 (C++)

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

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i <= n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define pb(x) push_back(x)
#define eb(x, y) emplace_back(x, y)
#define MOD 1000000007
#define MAX 1000000001
#define INF 1410065408
#define EPS 1e-9
#define DEBUG 0
#define ll long long
#define Pll pair<ll, ll>
#define Pii pair<int, int>
#define Pdd pair<double, double>

using namespace std;
struct edge {
  int from, to;
  ll cost;
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  vector<Pdd> v;
  double n, tmp, second = 0.25;
  rep(4) {
    cin >> tmp;
    v.pb(Pdd(tmp * (2.0 / second), second));
    second *= 2;
  }
  cin >> n;

  sort(all(v));
  ll ans = 0;
  rep(4) {
    Pdd p = v[i];
    // cout << p.first << " " << p.second << endl;
    double price = p.first * (p.second / 2.0);
    while (n >= p.second) {
      n -= p.second;
      ans += (ll)price;
    }
    if (n == 0)
      break;
  }
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i <= n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define pb(x) push_back(x)
#define eb(x, y) emplace_back(x, y)
#define MOD 1000000007
#define MAX 1000000001
#define INF 1410065408
#define EPS 1e-9
#define DEBUG 0
#define ll long long
#define Pll pair<ll, ll>
#define Pii pair<int, int>
#define Pdd pair<double, double>

using namespace std;
struct edge {
  int from, to;
  ll cost;
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  vector<Pdd> v;
  double n, tmp, second = 0.25;
  rep(4) {
    cin >> tmp;
    v.pb(Pdd(tmp * (2.0 / second), second));
    second *= 2;
  }
  cin >> n;

  sort(all(v));
  ll ans = 0;
  rep(4) {
    Pdd p = v[i];
    // cout << p.first << " " << p.second << endl;
    double price = p.first * (p.second / 2.0);
    ll tmp = n / p.second;
    n -= (double)tmp * p.second;
    ans += (ll)price * tmp;
    if (n == 0)
      break;
  }
  cout << ans;
}
Problem p03617 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 1000000000000000000
#define int long long int

signed main(void) {
  int flag = 0;
  int Q, H, S, D;
  int N;
  int sum = 0;
  int cost = 0;
  cin >> Q >> H >> S >> D;
  cin >> N;
  N *= 4;
  int A[4];
  A[0] = Q * 8;
  A[1] = H * 4;
  A[2] = S * 2;
  A[3] = D;
  int B[4] = {Q, H, S, D};
  int C[4] = {1, 2, 4, 8};

  do {
    for (int i = 3; i > 0; i--) {
      for (int j = i - 1; j >= 0; j--) {
        if (A[i] > A[j])
          A[i] = INF;
      }
    }
    int min = 0;
    for (int i = 1; i < 4; i++) {
      if (A[min] > A[i])
        min = i;
    }
    while (N > 100 && (N > sum && flag == 0)) {
      sum += C[min] * (N / C[min]);
      cost += B[min] * (N / C[min]);
    }
    flag = 1;
    while (N > sum) {
      sum += C[min];
      cost += B[min];
    }
    if (N == sum)
      break;
    sum -= C[min];
    cost -= B[min];

    A[min] = INF;
  } while (N != sum);

  cout << cost << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 1000000000000000000
#define int long long int

signed main(void) {
  int flag = 0;
  int Q, H, S, D;
  int N;
  int sum = 0;
  int cost = 0;
  cin >> Q >> H >> S >> D;
  cin >> N;
  N *= 4;
  int A[4];
  A[0] = Q * 8;
  A[1] = H * 4;
  A[2] = S * 2;
  A[3] = D;
  int B[4] = {Q, H, S, D};
  int C[4] = {1, 2, 4, 8};

  do {
    for (int i = 3; i > 0; i--) {
      for (int j = i - 1; j >= 0; j--) {
        if (A[i] > A[j])
          A[i] = INF;
      }
    }
    int min = 0;
    for (int i = 1; i < 4; i++) {
      if (A[min] > A[i])
        min = i;
    }
    if (N > 2000 && flag == 0) {
      sum += C[min] * ((N - 1000) / C[min]);
      cost += B[min] * ((N - 1000) / C[min]);
    }
    flag = 1;
    while (N > sum) {
      sum += C[min];
      cost += B[min];
    }
    if (N == sum)
      break;
    sum -= C[min];
    cost -= B[min];

    A[min] = INF;
  } while (N != sum);

  cout << cost << endl;

  return 0;
}
Problem p03617 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  ll q, h, s, d, n;
  cin >> q >> h >> s >> d >> n;
  n <<= 2;
  vector<ll> dp(n + 1, 1e18);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    if (i - 1 >= 0)
      dp[i] = min(dp[i], dp[i - 1] + q);
    if (i - 2 >= 0)
      dp[i] = min(dp[i], dp[i - 2] + h);
    if (i - 4 >= 0)
      dp[i] = min(dp[i], dp[i - 4] + s);
    if (i - 8 >= 0)
      dp[i] = min(dp[i], dp[i - 8] + d);
  }
  cout << dp[n] << endl;
}

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

int main() {
  ll q, h, s, d, n;
  cin >> q >> h >> s >> d >> n;
  ll ans = (n >> 1) * min({(q << 3), (h << 2), (s << 1), d}) +
           (n & 1) * min({(q << 2), (h << 1), (s << 0)});
  cout << ans << endl;
}
Problem p03617 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll q, h, s, d;
  double n;
  cin >> q >> h >> s >> d >> n;
  vector<tuple<double, ll, double>> vec;
  vec.push_back(make_tuple(4 * q, q, 0.25));
  vec.push_back(make_tuple(2 * h, h, 0.5));
  vec.push_back(make_tuple(s, s, 1));
  vec.push_back(make_tuple(d / 2, d, 2));
  sort(vec.begin(), vec.end());
  ll cost = 0;
  while (true) {
    ll f = get<1>(vec[0]);
    double s = get<2>(vec[0]);
    if (n < s) {
      vec.erase(vec.begin());
      continue;
    }
    n -= s;
    cost += f;
    if (n == 0) {
      cout << cost;
      return 0;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll q, h, s, d;
  double n;
  cin >> q >> h >> s >> d >> n;
  vector<tuple<double, ll, double>> vec;
  vec.push_back(make_tuple(4 * q, 4 * q, 1));
  vec.push_back(make_tuple(2 * h, 2 * h, 1));
  vec.push_back(make_tuple(s, s, 1));
  vec.push_back(make_tuple(d / 2, d, 2));
  sort(vec.begin(), vec.end());
  ll cost = 0;
  while (true) {
    ll f = get<1>(vec[0]);
    double s = get<2>(vec[0]);
    if (n < s) {
      vec.erase(vec.begin());
      continue;
    }
    n -= s;
    cost += f;
    if (n == 0) {
      cout << cost;
      return 0;
    }
  }
}