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

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

typedef long long ll;
const int MOD = 1e9 + 7;
int dp[2001];
int fact[200001];
int tcaf[200001];
int pow1(int n, int p) {
  ll cur, res;
  cur = n;
  res = 1;
  while (p) {
    if (p & 1) {
      res *= cur;
      res %= MOD;
    }
    p /= 2;
    cur *= cur;
    cur %= MOD;
  }
  return res;
}
void init() {
  fact[0] = tcaf[0] = 1;
  for (int i = 1; i < 200001; i++) {
    fact[i] = 1LL * fact[i - 1] * i % MOD;
    tcaf[i] = pow1(fact[i], MOD - 2);
  }
}
int ways(pair<int, int> p1, pair<int, int> p2) {
  int n, m;
  n = p2.first - p1.first;
  m = p2.second - p1.second;
  if (n < 0 || m < 0)
    return 0;
  return 1LL * fact[n + m] * tcaf[n] % MOD * tcaf[m] % MOD;
}
int main() {
  //  freopen("in.txt", "r", stdin);
  //  freopen("out.txt", "w", stdout);
  init();
  int h, w, n;
  cin >> h >> w >> n;
  dp[0] = 1;
  vector<pair<int, int>> points(n + 2);
  points[n + 1] = {h - 1, w - 1};
  for (int i = 1; i <= n; i++) {
    cin >> points[i].first >> points[i].second;
    points[i].first--;
    points[i].second--;
  }

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

  for (int i = 1; i <= n + 1; i++) {
    dp[i] = ways(points[0], points[i]);
    for (int j = 1; j < i; j++) {
      dp[i] -= 1LL * dp[j] * ways(points[j], points[i]) % MOD;
      dp[i] %= MOD;
    }
    dp[i] = (dp[i] + MOD) % MOD;
  }
  cout << dp[n + 1];
  return 0;
}


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

typedef long long ll;
const int MOD = 1e9 + 7;
int dp[3001];
int fact[200001];
int tcaf[200001];
int pow1(int n, int p) {
  ll cur, res;
  cur = n;
  res = 1;
  while (p) {
    if (p & 1) {
      res *= cur;
      res %= MOD;
    }
    p /= 2;
    cur *= cur;
    cur %= MOD;
  }
  return res;
}
void init() {
  fact[0] = tcaf[0] = 1;
  for (int i = 1; i < 200001; i++) {
    fact[i] = 1LL * fact[i - 1] * i % MOD;
    tcaf[i] = pow1(fact[i], MOD - 2);
  }
}
int ways(pair<int, int> p1, pair<int, int> p2) {
  int n, m;
  n = p2.first - p1.first;
  m = p2.second - p1.second;
  if (n < 0 || m < 0)
    return 0;
  return 1LL * fact[n + m] * tcaf[n] % MOD * tcaf[m] % MOD;
}
int main() {
  //  freopen("in.txt", "r", stdin);
  //  freopen("out.txt", "w", stdout);
  init();
  int h, w, n;
  cin >> h >> w >> n;
  dp[0] = 1;
  vector<pair<int, int>> points(n + 2);
  points[n + 1] = {h - 1, w - 1};
  for (int i = 1; i <= n; i++) {
    cin >> points[i].first >> points[i].second;
    points[i].first--;
    points[i].second--;
  }

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

  for (int i = 1; i <= n + 1; i++) {
    dp[i] = ways(points[0], points[i]);
    for (int j = 1; j < i; j++) {
      dp[i] -= 1LL * dp[j] * ways(points[j], points[i]) % MOD;
      dp[i] %= MOD;
    }
    dp[i] = (dp[i] + MOD) % MOD;
  }
  cout << dp[n + 1];
  return 0;
}
Problem p03185 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define INF 1000000000000000
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;

ll n, c;
vector<ll> h;
vector<p> lines;
vector<ll> dp;

bool check(p p1, p p2, p p3) {
  return (p2.second - p1.second) * (p3.first - p2.first) >
         (p3.second - p2.second) * (p2.first - p1.first);
}

ll get_y(p line, ll x) { return line.first * x + line.second; }

void add(p line) {
  if (lines.size() >= 2) {
    ll t = lines.size() - 1;
    while (!check(lines[t - 1], lines[t], line)) {
      lines.pop_back();
      t--;
    }
  }
  lines.push_back(line);
}

ll query(ll x) {
  ll l = -1, r = lines.size() - 1, mid;
  while (r - l > 1) {
    mid = (l + r) / 2;
    if (get_y(lines[mid], x) >= get_y(lines[mid + 1], x)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  return get_y(lines[r], x);
}

int main() {
  cin >> n >> c;
  REP(i, n) {
    ll tmp;
    cin >> tmp;
    h.push_back(tmp);
  }
  ll dp;
  add(p(-2 * h[0], c + h[0] * h[0]));
  for (ll i = 1; i < n; i++) {
    dp = query(h[i]) + h[i] * h[i];
    add(p(-2 * h[i], h[i] * h[i] + c + dp));
  }
  cout << dp << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define INF 1000000000000000
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;

ll n, c;
vector<ll> h;
vector<p> lines;
vector<ll> dp;

bool check(p p1, p p2, p p3) {
  return (p2.second - p1.second) * (p3.first - p2.first) >
         (p3.second - p2.second) * (p2.first - p1.first);
}

ll get_y(p line, ll x) { return line.first * x + line.second; }

void add(p line) {
  if (lines.size() >= 2) {
    ll t = lines.size() - 1;
    while (t - 1 >= 0 && !check(lines[t - 1], lines[t], line)) {
      lines.pop_back();
      t--;
    }
  }
  lines.push_back(line);
}

ll query(ll x) {
  ll l = -1, r = lines.size() - 1, mid;
  while (r - l > 1) {
    mid = (l + r) / 2;
    if (get_y(lines[mid], x) >= get_y(lines[mid + 1], x)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  return get_y(lines[r], x);
}

int main() {
  cin >> n >> c;
  REP(i, n) {
    ll tmp;
    cin >> tmp;
    h.push_back(tmp);
  }
  ll dp;
  add(p(-2 * h[0], c + h[0] * h[0]));
  for (ll i = 1; i < n; i++) {
    dp = query(h[i]) + h[i] * h[i];
    add(p(-2 * h[i], h[i] * h[i] + c + dp));
  }
  cout << dp << endl;
  return 0;
}
Problem p03185 (C++)

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

bool query = 0;
struct line {
  long long m, b;
  mutable function<const line *()> succ;
  bool operator<(const line &rhs) const {
    if (!query)
      return m < rhs.m;
    const line *s = succ();
    return s ? (b - s->b) < rhs.m * (s->m - m) : 0;
  }
};
struct hull : multiset<line> {
  bool bad(iterator y) {
    auto z = next(y);
    if (y == begin()) {
      if (z == end())
        return 0;
      return (y->m == z->m) && (y->b <= z->b);
    }
    auto x = prev(y);
    if (z == end())
      return (y->m == x->m) && (y->b <= x->b);
    return (1.0) * (x->b - y->b) * (z->m - y->m) >=
           (1.0) * (y->b - z->b) * (y->m - x->m);
  }
  void add(long long m, long long b) {
    auto it = insert({-m, -b});
    it->succ = [=] { return next(it) == end() ? 0 : &*next(it); };
    if (bad(it)) {
      erase(it);
      return;
    }
    while (next(it) != end() && bad(next(it)))
      erase(next(it));
    while (it != begin() && bad(prev(it)))
      erase(prev(it));
  }
  long long eval(long long x) {
    query = 1;
    auto l = *lower_bound({x, 0});
    query = 0;
    return -(l.m * x + l.b);
  }
};

#define MAX_N 100005

int n;
long long c, s[MAX_N];
hull h;

int main() {
  scanf("%d%lld", &n, &c);
  for (int i = 0; i < n; i++)
    scanf("%lld", &s[i]);

  vector<long long> dp(n);
  h.add(-2 * s[0], s[0] * s[0]);
  for (int i = 1; i < n; i++) {
    dp[i] = s[i] * s[i] + c + h.eval(s[i]);
    h.add(-2 * s[i], s[i] * s[i] + dp[i]);
  }
  printf("%lld\n", dp[n - 1]);
}


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

bool query = 0;
struct line {
  long long m, b;
  mutable function<const line *()> succ;
  bool operator<(const line &rhs) const {
    if (!query)
      return m < rhs.m;
    const line *s = succ();
    return s ? (b - s->b) < rhs.m * (s->m - m) : 0;
  }
};
struct hull : multiset<line> {
  bool bad(iterator y) {
    auto z = next(y);
    if (y == begin()) {
      if (z == end())
        return 0;
      return (y->m == z->m) && (y->b <= z->b);
    }
    auto x = prev(y);
    if (z == end())
      return (y->m == x->m) && (y->b <= x->b);
    return (1.0) * (x->b - y->b) * (z->m - y->m) >=
           (1.0) * (y->b - z->b) * (y->m - x->m);
  }
  void add(long long m, long long b) {
    auto it = insert({-m, -b});
    it->succ = [=] { return next(it) == end() ? 0 : &*next(it); };
    if (bad(it)) {
      erase(it);
      return;
    }
    while (next(it) != end() && bad(next(it)))
      erase(next(it));
    while (it != begin() && bad(prev(it)))
      erase(prev(it));
  }
  long long eval(long long x) {
    query = 1;
    auto l = *lower_bound({x, 0});
    query = 0;
    return -(l.m * x + l.b);
  }
};

#define MAX_N 200005

int n;
long long c, s[MAX_N];
hull h;

int main() {
  scanf("%d%lld", &n, &c);
  for (int i = 0; i < n; i++)
    scanf("%lld", &s[i]);

  vector<long long> dp(n);
  h.add(-2 * s[0], s[0] * s[0]);
  for (int i = 1; i < n; i++) {
    dp[i] = s[i] * s[i] + c + h.eval(s[i]);
    h.add(-2 * s[i], s[i] * s[i] + dp[i]);
  }
  printf("%lld\n", dp[n - 1]);
}
Problem p03185 (C++)

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

bool Q;
struct Line {
  mutable ll k, m, p;
  bool operator<(const Line &o) const { return Q ? p < o.p : k < o.k; }
};

struct LineContainer : multiset<Line> {
  const ll inf = LLONG_MAX;
  ll div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }

  bool isect(iterator x, iterator y) {
    if (y == end()) {
      x->p = inf;
      return false;
    }
    if (x->k == y->k)
      x->p = x->m > y->m ? inf : -inf;
    else
      x->p = div(y->m - x->m, x->k - y->k);
    return x->p >= y->p;
  }

  void add(ll k, ll m) {
    auto z = insert({k, m, 0}), y = z++, x = y;
    while (isect(y, z))
      z = erase(z);
    if (x != begin() && isect(--x, y))
      isect(x, y = erase(y));
    while ((y = x) != begin() && (--x)->p >= y->p)
      isect(x, erase(y));
  }

  ll query(ll x) {
    assert(!empty());
    Q = 1;
    auto l = *lower_bound({0, 0, x});
    Q = 0;
    return l.k * x + l.m;
  }
};

const int MAXN = 1e5 + 5;
ll dp[MAXN], h[MAXN];

int main() {
  LineContainer cht;
  int n;
  ll c;
  cin >> n >> c;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  dp[1] = 0;
  cht.add(2ll * h[1], -h[1] * h[1]);
  for (int m = 2; m <= n; m++) {
    dp[m] = h[m] * h[m] + c - cht.query(h[m]);
    cht.add(2ll * h[m], -h[m] * h[m] - dp[m]);
  }
  cout << dp[n] << endl;
}

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

bool Q;
struct Line {
  mutable ll k, m, p;
  bool operator<(const Line &o) const { return Q ? p < o.p : k < o.k; }
};

struct LineContainer : multiset<Line> {
  const ll inf = LLONG_MAX;
  ll div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }

  bool isect(iterator x, iterator y) {
    if (y == end()) {
      x->p = inf;
      return false;
    }
    if (x->k == y->k)
      x->p = x->m > y->m ? inf : -inf;
    else
      x->p = div(y->m - x->m, x->k - y->k);
    return x->p >= y->p;
  }

  void add(ll k, ll m) {
    auto z = insert({k, m, 0}), y = z++, x = y;
    while (isect(y, z))
      z = erase(z);
    if (x != begin() && isect(--x, y))
      isect(x, y = erase(y));
    while ((y = x) != begin() && (--x)->p >= y->p)
      isect(x, erase(y));
  }

  ll query(ll x) {
    assert(!empty());
    Q = 1;
    auto l = *lower_bound({0, 0, x});
    Q = 0;
    return l.k * x + l.m;
  }
};

const int MAXN = 2e5 + 5;
ll dp[MAXN], h[MAXN];

int main() {
  LineContainer cht;
  int n;
  ll c;
  cin >> n >> c;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  dp[1] = 0;
  cht.add(2ll * h[1], -h[1] * h[1]);
  for (int m = 2; m <= n; m++) {
    dp[m] = h[m] * h[m] + c - cht.query(h[m]);
    cht.add(2ll * h[m], -h[m] * h[m] - dp[m]);
  }
  cout << dp[n] << endl;
}
Problem p03185 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAXN 2001000
#define lc (x << 1)
#define rc (x << 1 | 1)
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

ll n, C, h[MAXN], dp[MAXN];
int vis[MAXN << 1];

struct line {
  double k, b;
  line(double _k = 0, double _b = 0) {
    k = _k;
    b = _b;
  }
  double get(double x) { return k * x + b; }
} c[MAXN << 1];

void modify(int x, int l, int r, line v) {
  if (!vis[x]) {
    vis[x] = 1;
    c[x] = v;
    return;
  }
  if (c[x].get(l) > v.get(l) && c[x].get(r) > v.get(r))
    return;
  if (c[x].get(l) < v.get(l) && c[x].get(r) < v.get(r)) {
    c[x] = v;
    return;
  }
  int m = (l + r) >> 1;
  if (c[x].get(l) < v.get(l))
    swap(c[x], v);
  if (c[x].get(m) > v.get(m))
    modify(rc, m + 1, r, v);
  else {
    swap(c[x], v);
    modify(lc, l, m, v);
  }
}

double get(int x, int l, int r, int pos) {
  if (l == r)
    return c[x].get(l);
  int m = (l + r) >> 1;
  double ans = c[x].get(pos);
  if (pos <= m)
    ans = max(ans, get(lc, l, m, pos));
  else
    ans = max(ans, get(rc, m + 1, r, pos));
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> C;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    if (i > 0)
      dp[i] = -get(1, 1, 1e6 + 10, h[i]) + h[i] * h[i] + C;
    line l = line(2 * h[i], -(dp[i] + h[i] * h[i]));
    modify(1, 1, 1e6 + 10, l);
    // cout << dp[i] << " ";
  }
  cout << dp[n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
#define MAXN 3001000
#define lc (x << 1)
#define rc (x << 1 | 1)
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

ll n, C, h[MAXN], dp[MAXN];
int vis[MAXN << 1];

struct line {
  double k, b;
  line(double _k = 0, double _b = 0) {
    k = _k;
    b = _b;
  }
  double get(double x) { return k * x + b; }
} c[MAXN << 1];

void modify(int x, int l, int r, line v) {
  if (!vis[x]) {
    vis[x] = 1;
    c[x] = v;
    return;
  }
  if (c[x].get(l) > v.get(l) && c[x].get(r) > v.get(r))
    return;
  if (c[x].get(l) < v.get(l) && c[x].get(r) < v.get(r)) {
    c[x] = v;
    return;
  }
  int m = (l + r) >> 1;
  if (c[x].get(l) < v.get(l))
    swap(c[x], v);
  if (c[x].get(m) > v.get(m))
    modify(rc, m + 1, r, v);
  else {
    swap(c[x], v);
    modify(lc, l, m, v);
  }
}

double get(int x, int l, int r, int pos) {
  if (l == r)
    return c[x].get(l);
  int m = (l + r) >> 1;
  double ans = c[x].get(pos);
  if (pos <= m)
    ans = max(ans, get(lc, l, m, pos));
  else
    ans = max(ans, get(rc, m + 1, r, pos));
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> C;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    if (i > 0)
      dp[i] = -get(1, 1, 1e6 + 10, h[i]) + h[i] * h[i] + C;
    line l = line(2 * h[i], -(dp[i] + h[i] * h[i]));
    modify(1, 1, 1e6 + 10, l);
    // cout << dp[i] << " ";
  }
  cout << dp[n - 1];
}
Problem p03185 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 50005;
typedef long long ll;
struct line {
  ll m, n;
  line(ll _m = 0, ll _n = 0) { m = _m, n = _n; }
} A[maxn];
ll get_y(const line &a, const int &x) { return a.m * x + a.n; }
bool ok(const line &a, const line &b, const line &c) {
  return (a.m - b.m) * (c.n - a.n) <= (a.m - c.m) * (b.n - a.n);
}
int l, r;
void add(const line f) {
  while (l < r && ok(A[r - 1], A[r], f)) {
    r--;
  }
  r++;
  A[r] = f;
}
line query(const int x) {
  while (l < r && get_y(A[l], x) > get_y(A[l + 1], x))
    l++;
  return A[l];
}
ll sq(int x) { return (ll)x * x; }
int h[maxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen(".in","r",stdin);
  int N;
  ll C;
  cin >> N >> C;
  for (int i = 1; i <= N; i++) {
    cin >> h[i];
  }
  A[1] = line(-2 * h[1], sq(h[1]));
  l = r = 1;
  ll mx = 0;
  for (int i = 2; i <= N; i++) {
    ll q = get_y(query(h[i]), h[i]);
    mx = q + C + sq(h[i]);
    add(line(-2 * h[i], mx + sq(h[i])));
  }
  cout << mx;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
typedef long long ll;
struct line {
  ll m, n;
  line(ll _m = 0, ll _n = 0) { m = _m, n = _n; }
} A[maxn];
ll get_y(const line &a, const int &x) { return a.m * x + a.n; }
bool ok(const line &a, const line &b, const line &c) {
  return (a.m - b.m) * (c.n - a.n) <= (a.m - c.m) * (b.n - a.n);
}
int l, r;
void add(const line f) {
  while (l < r && ok(A[r - 1], A[r], f)) {
    r--;
  }
  r++;
  A[r] = f;
}
line query(const int x) {
  while (l < r && get_y(A[l], x) > get_y(A[l + 1], x))
    l++;
  return A[l];
}
ll sq(int x) { return (ll)x * x; }
int h[maxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen(".in","r",stdin);
  int N;
  ll C;
  cin >> N >> C;
  for (int i = 1; i <= N; i++) {
    cin >> h[i];
  }
  A[1] = line(-2 * h[1], sq(h[1]));
  l = r = 1;
  ll mx = 0;
  for (int i = 2; i <= N; i++) {
    ll q = get_y(query(h[i]), h[i]);
    mx = q + C + sq(h[i]);
    add(line(-2 * h[i], mx + sq(h[i])));
  }
  cout << mx;
  return 0;
}
Problem p03185 (C++)

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

#define DIM 100007
typedef long long ll;
typedef long double ld;
using namespace std;

struct ev {
  ll k, b;
};
ll n, i, j, m, nlines;
ev lines[DIM];
ll a[DIM];
ll benz[DIM];
ll dp[DIM];

ll check(ev l1, ev l2, ev l3) {
  return (ld)(l3.b - l1.b) / (ld)(l1.k - l3.k) >=
         (ld)(l3.b - l2.b) / (ld)(l2.k - l3.k);
}

void add(ll k, ll b) {
  ev l = {k, b};
  while (nlines > 1 && check(lines[nlines - 1], lines[nlines], l))
    nlines--;
  nlines++;
  lines[nlines] = l;
}

ll f(ll num, ll x) { return lines[num].k * x + lines[num].b; }

ll getm(ll x) {
  ll l = 0, r = nlines, m;
  while (r - l > 1) {
    m = (l + r) / 2;
    if (f(m, x) < f(m + 1, x))
      r = m;
    else
      l = m;
  }
  return f(r, x);
}

int main() {
  ll sss;
  cin >> n >> sss;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  add(-2 * a[1], a[1] * a[1]);
  for (i = 2; i <= n; i++) {
    dp[i] = getm(a[i]) + a[i] * a[i] + sss;
    add(-2 * a[i], dp[i] + a[i] * a[i]);
  }
  cout << dp[n] << endl;
  return 0;
}


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

#define DIM 200007
typedef long long ll;
typedef long double ld;
using namespace std;

struct ev {
  ll k, b;
};
ll n, i, j, m, nlines;
ev lines[DIM];
ll a[DIM];
ll benz[DIM];
ll dp[DIM];

ll check(ev l1, ev l2, ev l3) {
  return (ld)(l3.b - l1.b) / (ld)(l1.k - l3.k) >=
         (ld)(l3.b - l2.b) / (ld)(l2.k - l3.k);
}

void add(ll k, ll b) {
  ev l = {k, b};
  while (nlines > 1 && check(lines[nlines - 1], lines[nlines], l))
    nlines--;
  nlines++;
  lines[nlines] = l;
}

ll f(ll num, ll x) { return lines[num].k * x + lines[num].b; }

ll getm(ll x) {
  ll l = 0, r = nlines, m;
  while (r - l > 1) {
    m = (l + r) / 2;
    if (f(m, x) < f(m + 1, x))
      r = m;
    else
      l = m;
  }
  return f(r, x);
}

int main() {
  ll sss;
  cin >> n >> sss;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  add(-2 * a[1], a[1] * a[1]);
  for (i = 2; i <= n; i++) {
    dp[i] = getm(a[i]) + a[i] * a[i] + sss;
    add(-2 * a[i], dp[i] + a[i] * a[i]);
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03185 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<ll, ll> pii;
#define endl '\n'
#define F first
#define S second
const int MAX_N = 2e5 + 5;

int n;
ll c;
ll h[MAX_N];
ll dp[MAX_N];
set<int> st;
priority_queue<pii, vector<pii>, greater<pii>> pq;

ll step(int x, int y) { return dp[x] + (h[y] - h[x]) * (h[y] - h[x]) + c; }
ll calc_limit(int x, int y) {
  return (dp[y] + h[y] * h[y] - dp[x] - h[x] * h[x] - 1) / (2 * (h[y] - h[x])) +
         1ll;
}

int main() {
  scanf("%d%lld", &n, &c);
  for (int i = 0; i < n; i++)
    scanf("%lld", &h[i]);
  st.insert(0);
  for (int i = 1; i < n; i++) {
    while (!pq.empty() && pq.top().F <= h[i]) {
      auto now = pq.top();
      pq.pop();
      auto it1 = st.find(now.S), it2 = next(it1);
      if (step((*it1), i) < step((*it2), i))
        continue;
      if (it1 != st.begin()) {
        auto it0 = prev(it1);
        pq.push({calc_limit((*it0), (*it2)), (*it0)});
      }
      st.erase(it1);
    }
    dp[i] = step((*st.begin()), i);
    pq.push({calc_limit((*st.rbegin()), i), (*st.rbegin())});
    st.insert(i);
  }
  printf("%lld\n", dp[n - 1]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<ll, ll> pii;
#define endl '\n'
#define F first
#define S second
const int MAX_N = 2e5 + 5;

int n;
ll c;
ll h[MAX_N];
ll dp[MAX_N];
set<int> st;
priority_queue<pii, vector<pii>, greater<pii>> pq;

ll step(int x, int y) { return dp[x] + (h[y] - h[x]) * (h[y] - h[x]) + c; }
ll calc_limit(int x, int y) {
  return (dp[y] + h[y] * h[y] - dp[x] - h[x] * h[x] - 1) / (2 * (h[y] - h[x])) +
         1ll;
}

int main() {
  scanf("%d%lld", &n, &c);
  for (int i = 0; i < n; i++)
    scanf("%lld", &h[i]);
  st.insert(0);
  for (int i = 1; i < n; i++) {
    while (!pq.empty() && pq.top().F <= h[i]) {
      auto now = pq.top();
      pq.pop();
      auto it1 = st.find(now.S);
      if (it1 == st.end())
        continue;
      auto it2 = next(it1);
      if (it2 == st.end())
        continue;
      if (step((*it1), i) < step((*it2), i))
        continue;
      if (it1 != st.begin()) {
        auto it0 = prev(it1);
        pq.push({calc_limit((*it0), (*it2)), (*it0)});
      }
      st.erase(it1);
    }
    dp[i] = step((*st.begin()), i);
    pq.push({calc_limit((*st.rbegin()), i), (*st.rbegin())});
    st.insert(i);
  }
  printf("%lld\n", dp[n - 1]);
  return 0;
}
Problem p03185 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define inf 100000000000000LL
#define ll long long
#define mx 100007
using namespace std;

vector<vector<ll>> tree(4LL * mx, {0LL, inf});

ll f(vector<ll> line, int x) { return line[0] * x + line[1]; }

void insert(vector<ll> line, int lo = 1, int hi = mx, int i = 1) {
  int m = (lo + hi) / 2;
  bool left = f(line, lo) < f(tree[i], lo);
  bool mid = f(line, m) < f(tree[i], m);

  if (mid)
    swap(tree[i], line);

  if (hi - lo == 1)
    return;
  else if (left != mid)
    insert(line, lo, m, 2 * i);
  else
    insert(line, m, hi, 2 * i + 1);
}

ll query(int x, int lo = 1, int hi = mx, int i = 1) {
  int m = (lo + hi) / 2;
  ll curr = f(tree[i], x);
  if (hi - lo == 1)
    return curr;
  if (x < m)
    return min(curr, query(x, lo, m, 2 * i));
  else
    return min(curr, query(x, m, hi, 2 * i + 1));
}

ll dp[mx];

int main() {
  ll n, c;
  cin >> n >> c;

  ll h[n + 5];

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

    if (i > 1)
      dp[i] = h[i] * h[i] + c + query(h[i]);

    insert({-2 * h[i], h[i] * h[i] + dp[i]});
  }

  cout << dp[n] << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#define inf 100000000000000LL
#define ll long long
#define mx 1000007
using namespace std;

vector<vector<ll>> tree(4LL * mx, {0LL, inf});

ll f(vector<ll> line, int x) { return line[0] * x + line[1]; }

void insert(vector<ll> line, int lo = 1, int hi = mx, int i = 1) {
  int m = (lo + hi) / 2;
  bool left = f(line, lo) < f(tree[i], lo);
  bool mid = f(line, m) < f(tree[i], m);

  if (mid)
    swap(tree[i], line);

  if (hi - lo == 1)
    return;
  else if (left != mid)
    insert(line, lo, m, 2 * i);
  else
    insert(line, m, hi, 2 * i + 1);
}

ll query(int x, int lo = 1, int hi = mx, int i = 1) {
  int m = (lo + hi) / 2;
  ll curr = f(tree[i], x);
  if (hi - lo == 1)
    return curr;
  if (x < m)
    return min(curr, query(x, lo, m, 2 * i));
  else
    return min(curr, query(x, m, hi, 2 * i + 1));
}

ll dp[mx];

int main() {
  ll n, c;
  cin >> n >> c;

  ll h[n + 5];

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

    if (i > 1)
      dp[i] = h[i] * h[i] + c + query(h[i]);

    insert({-2 * h[i], h[i] * h[i] + dp[i]});
  }

  cout << dp[n] << "\n";
}
Problem p03185 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef complex<ll> reta;
const ll INF = 1e18 + 7;
const int MAXN = 2e5 + 7, MAXX = 1e6 + 7;
int n, node_counter = 0, raiz, fe[4 * MAXN], fd[4 * MAXN];
reta f[4 * MAXN], inicial;
ll h[MAXN], c, dp[MAXN];

inline int create() {
  node_counter++;
  fe[node_counter] = -1;
  fd[node_counter] = -1;
  f[node_counter] = inicial;
  return node_counter;
}
inline ll calc(reta r, ll val) { return r.real() * val + r.imag(); }
void upt(int node, int l, int r, reta nova) {
  if (l > r)
    return;
  int mid = (l + r) / 2;
  if (calc(nova, mid) >= calc(f[node], mid)) {
    if (fd[node] == -1)
      fd[node] = create();
    upt(fd[node], mid + 1, r, nova);
  } else {
    swap(f[node], nova);
    if (nova == inicial)
      return;
    if (fe[node] == -1)
      fe[node] = create();
    upt(fe[node], l, mid, nova);
  }
}
ll query(int node, int l, int r, ll x) {
  if (node == -1)
    return INF;
  ll v_no = calc(f[node], x);
  int mid = (l + r) / 2;
  if (x <= mid)
    return min(v_no, query(fe[node], l, mid, x));
  return min(v_no, query(fd[node], mid + 1, r, x));
}
int main() {
  inicial.real(0);
  inicial.imag(INF);
  raiz = create();
  scanf("%d %lld", &n, &c);

  scanf("%lld", &h[1]);

  for (int i = 2; i <= n; i++) {
    reta ri;
    ri.real(-2 * h[i - 1]);
    ri.imag(dp[i - 1] + h[i - 1] * h[i - 1]);
    upt(raiz, 1, MAXX, ri);

    scanf("%lld", &h[i]);
    dp[i] = c + h[i] * h[i] + query(raiz, 1, MAXX, h[i]);
  }
  printf("%lld\n", dp[n]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef complex<ll> reta;
const ll INF = 1e18 + 7;
const int MAXN = 2e5 + 7, MAXX = 1e6 + 7;
int n, node_counter = 0, raiz, fe[4 * MAXN], fd[4 * MAXN];
reta f[4 * MAXN], inicial;
ll h[MAXN], c, dp[MAXN];

inline int create() {
  node_counter++;
  fe[node_counter] = -1;
  fd[node_counter] = -1;
  f[node_counter] = inicial;
  return node_counter;
}
inline ll calc(reta r, ll val) { return r.real() * val + r.imag(); }
void upt(int node, int l, int r, reta nova) {
  if (l > r)
    return;
  int mid = (l + r) / 2;
  if (calc(nova, mid) >= calc(f[node], mid)) {
    if (fd[node] == -1)
      fd[node] = create();
    upt(fd[node], mid + 1, r, nova);
  } else {
    swap(f[node], nova);
    if (nova == inicial || l == r)
      return;
    if (fe[node] == -1)
      fe[node] = create();
    upt(fe[node], l, mid, nova);
  }
}
ll query(int node, int l, int r, ll x) {
  if (node == -1)
    return INF;
  ll v_no = calc(f[node], x);
  int mid = (l + r) / 2;
  if (x <= mid)
    return min(v_no, query(fe[node], l, mid, x));
  return min(v_no, query(fd[node], mid + 1, r, x));
}
int main() {
  inicial.real(0);
  inicial.imag(INF);
  raiz = create();
  scanf("%d %lld", &n, &c);

  scanf("%lld", &h[1]);

  for (int i = 2; i <= n; i++) {
    reta ri;
    ri.real(-2 * h[i - 1]);
    ri.imag(dp[i - 1] + h[i - 1] * h[i - 1]);
    upt(raiz, 1, MAXX, ri);

    scanf("%lld", &h[i]);
    dp[i] = c + h[i] * h[i] + query(raiz, 1, MAXX, h[i]);
  }
  printf("%lld\n", dp[n]);
}
Problem p03185 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
using namespace std;
const int MAXN = 100005;
const LL MOD = 998244353;

LL N, C, s, t, h[MAXN], dp[MAXN];
int deq[MAXN];

bool useless(int a, int b, int c) {
  LL m1 = -2 * h[a], b1 = dp[a] + h[a] * h[a];
  LL m2 = -2 * h[b], b2 = dp[b] + h[b] * h[b];
  LL m3 = -2 * h[c], b3 = dp[c] + h[c] * h[c];
  if ((b2 - b3) / (m3 - m2) < (b1 - b2) / (m2 - m1))
    return true;
  if ((b2 - b3) / (m3 - m2) > (b1 - b2) / (m2 - m1))
    return false;
  return (b2 - b3) % (m3 - m2) * (m2 - m1) <= (b1 - b2) % (m2 - m1) * (m3 - m2);
}

LL f(int j, LL x) { return dp[j] + h[j] * h[j] - 2 * h[j] * x; }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> N >> C;
  for (int i = 1; i <= N; i++)
    cin >> h[i];

  dp[1] = 0;
  for (int i = 2; i <= N; i++) {
    while (t - s >= 2 && useless(deq[t - 2], deq[t - 1], i - 1))
      t--;
    deq[t++] = i - 1;

    while (t - s >= 2 && f(deq[s], h[i]) >= f(deq[s + 1], h[i]))
      s++;
    dp[i] = f(deq[s], h[i]) + h[i] * h[i] + C;
  }

  cout << dp[N] << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
using namespace std;
const int MAXN = 200005;
const LL MOD = 998244353;

LL N, C, s, t, h[MAXN], dp[MAXN];
int deq[MAXN];

bool useless(int a, int b, int c) {
  LL m1 = -2 * h[a], b1 = dp[a] + h[a] * h[a];
  LL m2 = -2 * h[b], b2 = dp[b] + h[b] * h[b];
  LL m3 = -2 * h[c], b3 = dp[c] + h[c] * h[c];
  if ((b2 - b3) / (m3 - m2) < (b1 - b2) / (m2 - m1))
    return true;
  if ((b2 - b3) / (m3 - m2) > (b1 - b2) / (m2 - m1))
    return false;
  return (b2 - b3) % (m3 - m2) * (m2 - m1) <= (b1 - b2) % (m2 - m1) * (m3 - m2);
}

LL f(int j, LL x) { return dp[j] + h[j] * h[j] - 2 * h[j] * x; }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> N >> C;
  for (int i = 1; i <= N; i++)
    cin >> h[i];

  dp[1] = 0;
  for (int i = 2; i <= N; i++) {
    while (t - s >= 2 && useless(deq[t - 2], deq[t - 1], i - 1))
      t--;
    deq[t++] = i - 1;

    while (t - s >= 2 && f(deq[s], h[i]) >= f(deq[s + 1], h[i]))
      s++;
    dp[i] = f(deq[s], h[i]) + h[i] * h[i] + C;
  }

  cout << dp[N] << "\n";
}
Problem p03186 (C++)

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

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  int ans = 0;
  bool health = true;
  while (true) {
    if (health) {
      if (c) {
        ans++;
        c--;
        health = false;
      } else if (b) {
        b--;
        ans++;
      } else if (a) {
        a--;
      } else {
        break;
      }
    } else {
      if (b) {
        ans++;
        b--;
      } else if (a) {
        a--;
      } else {
        break;
      }
      health = true;
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  int ans = 0;
  if (a + b >= c - 1) {
    ans = b + c;
  } else {
    ans = a + b + b;
    if (b != 0 || c != 0)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03186 (C++)

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

int gcd(int x, int y) {
  if (y == 0)
    return x;

  return gcd(y, x % y);
}
int lcm(int x, int y) { return x * y / gcd(x, y); }

int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}

int main() {

  int A, B, C, ans = 0, f = 0;

  cin >> A >> B >> C;

  while (1) {

    if (C == 0) {
      ans += B;
      break;
    }

    if (f == 0) {
      if (C > 0) {
        C--;
        ans++;
        f = 1;
      }
    } else {
      if (B > 0) {
        B--;
        ans++;
      } else if (A > 0) {
        A--;
      } else {
        break;
      }
      f = 0;
    }
  }

  cout << ans << endl;

  return 0;
}

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

int gcd(int x, int y) {
  if (y == 0)
    return x;

  return gcd(y, x % y);
}
int lcm(int x, int y) { return x * y / gcd(x, y); }

int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}

int main() {

  int A, B, C, ans = 0, f = 0;

  cin >> A >> B >> C;

  ans = min(A + B + 1, C) + B;

  cout << ans << endl;

  return 0;
}
Problem p03186 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  int a, b, c;
  std::cin >> a >> b >> c;
  bool x = true;
  int count = 0;
  while (true) {
    if (x && c != 0) {
      c--;
      count++;
      x = false;
      continue;
    }
    if (b != 0) {
      b--;
      count++;
      x = true;
      continue;
    }
    if (a != 0) {
      a--;
      x = true;
      continue;
    }
    break;
  }
  std::cout << count << std::endl;
}


Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  int a, b, c;
  std::cin >> a >> b >> c;
  bool x = true;
  int count = 0;
  while (true) {
    if (x && c != 0) {
      c--;
      count++;
      x = false;
      continue;
    }
    if (b != 0) {
      b--;
      count++;
      x = true;
      continue;
    }
    if (b == 0 && c == 0) {
      break;
    }
    if (a != 0) {
      a--;
      x = true;
      continue;
    }
    break;
  }
  std::cout << count << std::endl;
}
Problem p03186 (C++)

Buggy submission (status: Time Limit Exceeded):
/*#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#include<cmath>
#include<cstdio>
#include<deque>
#include<fstream>
#include<iomanip>
#include<map>
#include <numeric>*/
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define ok1 printf("ok1\n");
#define ok2 printf("ok2\n");
#define M 1000000000000000000LL
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = (s); i < (n); i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define REPR(i, s, n) for (int i = (s); i >= (n); (i)--)
#define all(a) (a).begin(), (a).end()
#define reall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define pf push_front
#define MIN(a, b) a = min((a), (b))
#define MAX(a, b) a = max((a), (b))
#define SIZE(v) (int)v.size()
const double pi = acos(-1.0);

typedef vector<int> vi;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef deque<ll> dll;
typedef pair<ll, ll> P;
#define mod 1e9 + 7;

int main() {
  ll a = 0, b = 0, c = 0, d = 0, ans = 0, ret = M, n, m;
  string s;
  bool x = false;
  cin >> a >> b >> c;
  while (1) {
    if (!x && c != 0) {
      ans++;
      c--;
      x ^= 1;
    } else if (b != 0) {
      ans++;
      b--;
      x ^= 1;
    } else if (a != 0) {
      a--;
      x ^= 1;
    } else
      break;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
/*#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#include<cmath>
#include<cstdio>
#include<deque>
#include<fstream>
#include<iomanip>
#include<map>
#include <numeric>*/
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define ok1 printf("ok1\n");
#define ok2 printf("ok2\n");
#define M 1000000000000000000LL
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = (s); i < (n); i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define REPR(i, s, n) for (int i = (s); i >= (n); (i)--)
#define all(a) (a).begin(), (a).end()
#define reall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define pf push_front
#define MIN(a, b) a = min((a), (b))
#define MAX(a, b) a = max((a), (b))
#define SIZE(v) (int)v.size()
const double pi = acos(-1.0);

typedef vector<int> vi;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef deque<ll> dll;
typedef pair<ll, ll> P;
#define mod 1e9 + 7;

int main() {
  ll a = 0, b = 0, c = 0, d = 0, ans = 0, ret = M, n, m;
  string s;
  bool x = false;
  cin >> a >> b >> c;
  if (a + b == c) {
    ans = c + b;
  } else if (a + b < c) {
    ans = (a + b) * 2 - a + 1;
  } else
    ans = b + c;
  cout << ans << endl;
  return 0;
}
Problem p03186 (C++)

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

int main() {
  int a, b, c;
  cin >> a >> b >> c;

  int cnt = 0;
  bool stomach = true;

  while (1) {
    if (stomach) {
      if (c > 0) {
        c--;
        cnt++;
        stomach = false;
      }
    }

    if (a == 0 && b == 0)
      break;

    if (b > 0) {
      b--;
      cnt++;
      stomach = true;
    } else if (a > 0) {
      a--;
      stomach = true;
    }
  }

  cout << cnt << endl;
}

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

int main() {
  int a, b, c;
  cin >> a >> b >> c;

  if (a + b + 1 >= c)
    cout << b + c << endl;
  else
    cout << (a + b + 1) + b << endl;
} // 解説参考.
Problem p03186 (C++)

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

int eat = 0;
bool poison = false;

int cookies(int a, int b, int c) {
  if (poison) {
    if (b > 0) {
      b--;
      eat++;
      poison = false;
      cookies(a, b, c);
    } else if (a > 0) {
      a--;
      poison = false;
      cookies(a, b, c);
    }
  } else if (c > 0) {
    c--;
    eat++;
    poison = true;
    cookies(a, b, c);
  } else if (b > 0) {
    b--;
    eat++;
    poison = false;
    cookies(a, b, c);
  }
}

int main() {
  int A, B, C;
  cin >> A >> B >> C;

  cookies(A, B, C);
  cout << eat << endl;
}


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

int eat = 0;
bool poison = false;

int cookies(int a, int b, int c) {
  if (poison) {
    if (b > 0) {
      b--;
      eat++;
      poison = false;
      cookies(a, b, c);
    } else if (a > 0) {
      a--;
      poison = false;
      cookies(a, b, c);
    }
  } else if (c > 0) {
    c--;
    eat++;
    poison = true;
    cookies(a, b, c);
  } else if (b > 0) {
    b--;
    eat++;
    cookies(a, b, c);
  }
}

int main() {
  int A, B, C;
  cin >> A >> B >> C;

  cookies(A, B, C);
  cout << eat << endl;
}
Problem p03186 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void file() {
#ifndef ONLINE_JUDGE
  freopen("a_input.txt", "r", stdin);
  freopen("a_output.txt", "w", stdout);
#endif
}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  file();
  fast();
  int a, b, c;
  cin >> a >> b >> c;
  int mn = min(b + 1, c);
  c -= mn;
  b += mn;
  mn = min(c, a);
  b += mn;
  cout << b << endl;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void file() {
#ifndef ONLINE_JUDGE
  freopen("a_input.txt", "r", stdin);
  freopen("a_output.txt", "w", stdout);
#endif
}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  //	file();
  //	fast();
  int a, b, c;
  cin >> a >> b >> c;
  int mn = min(b + 1, c);
  c -= mn;
  b += mn;
  mn = min(c, a);
  b += mn;
  cout << b << endl;
}
Problem p03186 (Python)

Buggy submission (status: Time Limit Exceeded):
A, B, C = map(int, input().split())
ans = 0
c = C
for i in range(C):
    if c > 0 and B > 0:
        ans += 2
        c -= 1
        B -= 1
    elif c > 0 and A > 0:
        ans += 1
        c -= 1
        A -= 1
if c > 0:
    ans += 1
print(ans + B)


Fixed submission:
A, B, C = map(int, input().split())
ans = 0
if B >= C:
    print(B + C)
    exit()
ans = 2 * B
C -= B
if A >= C:
    print(ans + C)
    exit()
print(ans + A + 1)
Problem p03186 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define INF 1e9
#define LINF 1e18
#define ld double
#define ll long long
#define ull unsigned long long

namespace io {
const int SZ = 1 << 13;
char buff[SZ], *pos = buff + SZ - 1;
char gc() {
  if (++pos == buff + SZ)
    fread(pos = buff, 1, SZ, stdin);
  return *pos;
}
} // namespace io

template <typename T> inline void read(T &x) {
  x = 0;
  int f = 1, c = io ::gc();
  for (; !isdigit(c); c = io ::gc())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = io ::gc())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  int a, b, c;
  read(a);
  read(b);
  read(c);
  printf("%d\n", b + min(a + b + 1, c));

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define INF 1e9
#define LINF 1e18
#define ld double
#define ll long long
#define ull unsigned long long

namespace io {
const int SZ = 1 << 13;
char buff[SZ], *pos = buff + SZ - 1;
char gc() {
  if (++pos == buff + SZ)
    fread(pos = buff, 1, SZ, stdin);
  return *pos;
}
} // namespace io

template <typename T> inline void read(T &x) {
  x = 0;
  int f = 1, c = io ::gc();
  for (; !isdigit(c); c = io ::gc())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = io ::gc())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}

int main() {
  int a, b, c;
  read(a);
  read(b);
  read(c);
  printf("%d\n", b + min(a + b + 1, c));

  return 0;
}
Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

const int N = 100005;

LL s1[N], s2[N], a[N], L, Ans;
int n;

LL f(int l, int r) { return s1[r] - a[l] * (r - l) - s1[l]; }

void Calc() {
  for (int i = 1; i <= n; ++i)
    s1[i] = s1[i - 1] + a[i], s2[i] = s2[i - 1] + (L - a[n - i + 1]);
  for (int i = 0; i < n; ++i) {
    LL now = a[i] * (n - i) + a[i];
    int cnt = (n - i) / 2;
    if ((n - i) % 2 == 0)
      now += s2[cnt] * 2 + f(i, i + cnt) * 2 - (a[i + cnt] - a[i]);
    else
      now += s2[cnt + 1] * 2 + f(i, i + cnt) * 2 - (L - a[n - cnt]);
    //		puts("-------------------------------");
    //		if (n & 1) {
    //			cout << s2[cnt + 1] * 2 << " " << f(i, i + cnt) * 2 << " " <<  (L -
    //a[n - cnt]) << " ";
    //		}
    //		else {
    //			cout << s2[cnt] * 2 << " " <<  f(i, i + cnt) * 2 << " " << (a[i +
    //cnt] - a[i]) << " ";
    //		}
    //		cout << now << "\n";

    Ans = max(Ans, now);
  }
}
int main() {
  L = read();
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  Calc();
  for (int i = 1; i <= n; ++i)
    a[i] = L - a[i];
  reverse(a + 1, a + n + 1);
  Calc();
  cout << Ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

const int N = 200005;

LL s1[N], s2[N], a[N], L, Ans;
int n;

LL f(int l, int r) { return s1[r] - a[l] * (r - l) - s1[l]; }

void Calc() {
  for (int i = 1; i <= n; ++i)
    s1[i] = s1[i - 1] + a[i], s2[i] = s2[i - 1] + (L - a[n - i + 1]);
  for (int i = 0; i < n; ++i) {
    LL now = a[i] * (n - i) + a[i];
    int cnt = (n - i) / 2;
    if ((n - i) % 2 == 0)
      now += s2[cnt] * 2 + f(i, i + cnt) * 2 - (a[i + cnt] - a[i]);
    else
      now += s2[cnt + 1] * 2 + f(i, i + cnt) * 2 - (L - a[n - cnt]);
    //		puts("-------------------------------");
    //		if (n & 1) {
    //			cout << s2[cnt + 1] * 2 << " " << f(i, i + cnt) * 2 << " " <<  (L -
    //a[n - cnt]) << " ";
    //		}
    //		else {
    //			cout << s2[cnt] * 2 << " " <<  f(i, i + cnt) * 2 << " " << (a[i +
    //cnt] - a[i]) << " ";
    //		}
    //		cout << now << "\n";

    Ans = max(Ans, now);
  }
}
int main() {
  L = read();
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  Calc();
  for (int i = 1; i <= n; ++i)
    a[i] = L - a[i];
  reverse(a + 1, a + n + 1);
  Calc();
  cout << Ans;
  return 0;
}
Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
#define SF scanf
#define PF printf
#define MAXN 100010
using namespace std;
typedef long long ll;
int n;
ll ans, a[MAXN], L, lft, rit, nstl, nstr, num;
int main() {
  SF("%lld%d", &L, &n);
  for (int i = 1; i <= n; i++)
    SF("%lld", &a[i]);
  ans = a[n];
  nstl = n;
  nstr = n + 1;
  for (int i = n; i >= 1; i--) { // block between i and i-1
    if (i != n)
      ans = max(ans, a[i] + 2 * (L - a[i + 1]));
    else
      ans = max(ans, L - a[i] + 2 * a[i - 1]);
    num = min(n - i, i - 1);
    rit -= a[i];
    while (i - nstr < num && nstr > 1) {
      nstr--;
      rit += (a[nstr]);
    }
    while (nstl - i > num && nstl > 0) {
      lft -= (L - a[nstl]);
      nstl--;
    }
    //		PF("[%d %lld %lld
    //%lld]\n",i,lft,rit,(lft+rit)*2+max(L-a[i],a[i]));
    int lastl, lastr;
    if (nstl == n)
      lastl = 0;
    else
      lastl = (L - a[nstl + 1]) * 2;
    if (nstr == 0)
      lastr = 0;
    else
      lastr = a[nstr - 1] * 2;
    ans = max(ans, (lft + rit) * 2 + max(a[i] + lastl, L - a[i] + lastr));
    lft += (L - a[i]);
  }
  PF("%lld", ans);
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
#define SF scanf
#define PF printf
#define MAXN 200010
using namespace std;
typedef long long ll;
int n;
ll ans, a[MAXN], L, lft, rit, nstl, nstr, num;
int main() {
  SF("%lld%d", &L, &n);
  for (int i = 1; i <= n; i++)
    SF("%lld", &a[i]);
  ans = a[n];
  nstl = n;
  nstr = n + 1;
  for (int i = n; i >= 1; i--) { // block between i and i-1
    if (i != n)
      ans = max(ans, a[i] + 2 * (L - a[i + 1]));
    else
      ans = max(ans, L - a[i] + 2 * a[i - 1]);
    num = min(n - i, i - 1);
    rit -= a[i];
    while (i - nstr < num && nstr > 1) {
      nstr--;
      rit += (a[nstr]);
    }
    while (nstl - i > num && nstl > 0) {
      lft -= (L - a[nstl]);
      nstl--;
    }
    //		PF("[%d %lld %lld
    //%lld]\n",i,lft,rit,(lft+rit)*2+max(L-a[i],a[i]));
    int lastl, lastr;
    if (nstl == n)
      lastl = 0;
    else
      lastl = (L - a[nstl + 1]) * 2;
    if (nstr == 0)
      lastr = 0;
    else
      lastr = a[nstr - 1] * 2;
    ans = max(ans, (lft + rit) * 2 + max(a[i] + lastl, L - a[i] + lastr));
    lft += (L - a[i]);
  }
  PF("%lld", ans);
}