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

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

using namespace std;

int main() {
  int N, X;
  int m[N];
  cin >> N >> X;
  int res = X;
  int min = 1000;
  for (int i = 0; i < N; i++) {
    cin >> m[i];
  }
  for (int i = 0; i < N; i++) {
    res = res - m[i];
    if (min > m[i]) {
      min = m[i];
    }
  }
  int n = res / min;
  cout << N + n << endl;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int N, X;
  int m[110];
  cin >> N >> X;
  int res = X;
  int min = 1000;
  for (int i = 0; i < N; i++) {
    cin >> m[i];
  }
  for (int i = 0; i < N; i++) {
    res = res - m[i];
    if (min > m[i]) {
      min = m[i];
    }
  }
  int n = res / min;
  cout << N + n << endl;
}
Problem p03371 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
a = 1
b = 1
mx = 10**10
while a <= b and a * a <= n:
    if n % a == 0:
        b = int(n / a)
        mx = min(mx, max(a, b))
    a += 1
print(len(str(mx)))


Fixed submission:
[a, b, ab, x, y] = [int(i) for i in input().split()]
price = []
price.append(a * x + b * y)
if x > y:
    price.append(2 * ab * y + a * (x - y))
else:
    price.append(2 * ab * x + b * (y - x))
price.append(ab * 2 * max(x, y))
print(min(price))
Problem p03371 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  int a, b, c, x, y, ans = 1e9;
  cin >> a >> b >> c >> x >> y;
  rep(i, x + 1) {
    rep(j, y + 1) {
      int d = max(2 * (y - j), 2 * (x - i));
      ans = min(i * a + j * b + c * d, ans);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  int a, b, c, x, y, ans = 1e9;
  cin >> a >> b >> c >> x >> y;
  rep(i, max(2 * x, 2 * y) + 1) {
    int d = 0, e = 0;
    if (x - i / 2 > 0)
      d = a * (x - i / 2);
    if (y - i / 2 > 0)
      e = b * (y - i / 2);
    ans = min(ans, c * i + d + e);
  }
  cout << ans << endl;
}
Problem p03371 (C++)

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

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

  int ans = 5000 * 2 * 1E+5;
  for (int a = 0; a <= X; a++) {
    for (int b = 0; b <= Y; b++) {
      int c = max(2 * (X - a), 2 * (Y - b));
      int cost = a * A + b * B + c * C;
      ans = min(ans, cost);
    }
  }

  cout << ans << endl;

  return 0;
}


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

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

  int ans = 5000 * 2 * 1E+5;
  for (int c = 0; c <= max(X, Y) * 2; c += 2) {
    int a = max(X - c / 2, 0);
    int b = max(Y - c / 2, 0);
    int cost = a * A + b * B + c * C;
    ans = min(ans, cost);
  }

  cout << ans << endl;

  return 0;
}
Problem p03371 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  int A, B, C, X, Y;
  cin >> A >> B >> C >> X >> Y;
  int min = A * X + B * Y;
  for (int i = 0; i <= X; i++) {
    for (int j = 0; j <= Y; j++) {
      for (int k = 0; k <= max(A, B) * 2; k += 2) {
        if (i + k / 2 >= X && j + k / 2 >= Y) {
          if (min > A * i + C * k / 2 + B * j + C * k / 2) {
            min = A * i + C * k / 2 + A * j + C * k / 2;
            // cout << i << j << k << endl;  //確認
          }
        }
      }
    }
  }
  cout << min << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  int A, B, C, X, Y;
  cin >> A >> B >> C >> X >> Y;
  int min = A * X + B * Y;
  for (int k = 0; k <= 2 * max(X, Y); k += 2) {
    if (A * (X - k / 2) < 0)
      A = 0;
    if (B * (Y - k / 2) < 0)
      B = 0;
    if (min > C * k + A * (X - k / 2) + B * (Y - k / 2)) {
      min = C * k + A * (X - k / 2) + B * (Y - k / 2);
    }
  }
  cout << min << endl;
}
Problem p03371 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define POSL(x, v)                                                             \
  (lower_bound(x.begin(), x.end(), v) - x.begin()) // ai>=v  x is sorted
#define POSU(x, v)                                                             \
  (upper_bound(x.begin(), x.end(), v) - x.begin()) // ai>v  x is sorted
#define NUM(x, v) (POSU(x, v) - POSL(x, v))        // x is sorted
#define SORT(x) (sort(x.begin(), x.end()))         // 0 2 2 3 4 5 8 9
#define REV(x) (reverse(x.begin(), x.end()))       // reverse
#define TO(x, t, f) ((x) ? (t) : (f))
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NEXTP(x) next_permutation(x.begin(), x.end())
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll c = gcd(a, b);
  return ((a / c) * (b / c) * c);
} // saisyo kobaisu

const ll INF = 1e9 + 7;
const ll N = 10002;
ll a, b, c, ans = INF, x, y;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> a >> b >> c >> x >> y;
  FOR(i, 0, x + 1) {
    FOR(j, 0, y + 1) {
      ll k = max((x - i), (y - j)) * 2;
      ll w;
      w = i * a + j * b + k * c;
      ans = min(ans, w);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define POSL(x, v)                                                             \
  (lower_bound(x.begin(), x.end(), v) - x.begin()) // ai>=v  x is sorted
#define POSU(x, v)                                                             \
  (upper_bound(x.begin(), x.end(), v) - x.begin()) // ai>v  x is sorted
#define NUM(x, v) (POSU(x, v) - POSL(x, v))        // x is sorted
#define SORT(x) (sort(x.begin(), x.end()))         // 0 2 2 3 4 5 8 9
#define REV(x) (reverse(x.begin(), x.end()))       // reverse
#define TO(x, t, f) ((x) ? (t) : (f))
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NEXTP(x) next_permutation(x.begin(), x.end())
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll c = gcd(a, b);
  return ((a / c) * (b / c) * c);
} // saisyo kobaisu

const ll INF = 1e9 + 7;
const ll N = 10002;
ll a, b, c, ans = INF, x, y;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> a >> b >> c >> x >> y;
  for (ll i = 0; i < 200001; i += 2) {
    ll w = i * c;
    ll aa = x - i / 2;
    ll bb = y - i / 2;
    if (aa > 0)
      w += aa * a;
    if (bb > 0)
      w += bb * b;
    ans = min(ans, w);
  }
  cout << ans << endl;
  return 0;
}
Problem p03371 (C++)

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

int main(int argc, char *argv[]) {
  int A, B, C, X, Y;
  cin >> A >> B >> C >> X >> Y;
  int countA = 0, countB = 0;
  long long sum = 0;
  int mi, ch;
  mi = min(X, Y);
  while (true) {
    if (countA < mi || countB < mi) {
      if (A + B < 2 * C) {
        sum += A + B;
        countA++;
        countB++;
      } else {
        sum += 2 * C;
        countA++;
        countB++;
      }
    } else {
      if (mi == X) {
        if (B < 2 * C) {
          sum += B;
          countB++;

        } else {
          sum += 2 * C;
          countB++;
        }
        if (countB == Y)
          break;

      } else {
        if (A < 2 * C) {
          sum += A;
          countA++;
        } else {
          sum += 2 * C;
          countA++;
        }
        if (countA == X)
          break;
      }
    }
    // cout<<countB<<" "<<countA<<endl;
  }
  cout << sum << endl;
}


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

int main(int argc, char *argv[]) {
  int A, B, C, X, Y;
  cin >> A >> B >> C >> X >> Y;
  int countA = 0, countB = 0;
  long long sum = 0;
  int mi, ch;
  mi = min(X, Y);
  while (true) {
    if (countA < mi || countB < mi) {
      if (A + B < 2 * C) {
        sum += A + B;
        countA++;
        countB++;
      } else {
        sum += 2 * C;
        countA++;
        countB++;
      }
      if (countA == X && countB == Y)
        break;
    } else {
      if (mi == X) {
        if (B < 2 * C) {
          sum += B;
          countB++;

        } else {
          sum += 2 * C;
          countB++;
        }
        if (countB == Y)
          break;

      } else {
        if (A < 2 * C) {
          sum += A;
          countA++;
        } else {
          sum += 2 * C;
          countA++;
        }
        if (countA == X)
          break;
      }
    }
    // cout<<countB<<" "<<countA<<endl;
  }
  cout << sum << endl;
}
Problem p03371 (Python)

Buggy submission (status: Runtime Error):
A, B, C, X, Y = list(map(int, input().split()))

# A 1枚、B 1枚を2C円で買うと捉え直す。
# ピザは、最大でも10**5枚（A, Bそれぞれに対して）
# A, Bのセットを、1~10**セットまで動かして一番安い組を考える

cheapest = float("inf")

for i in range(max(X, Y) + 1):
    price = 2 * C * i + A * max(0, X - i) + B * max(Y - i)

    cheapest = min(cheapest, price)

print(cheapest)


Fixed submission:
A, B, C, X, Y = list(map(int, input().split()))

# A 1枚、B 1枚を2C円で買うと捉え直す。
# ピザは、最大でも10**5枚（A, Bそれぞれに対して）
# A, Bのセットを、1~10**セットまで動かして一番安い組を考える

cheapest = float("inf")

for i in range(max(X, Y) + 1):
    price = 2 * C * i + A * max(0, X - i) + B * max(0, Y - i)

    cheapest = min(cheapest, price)

print(cheapest)
Problem p03371 (C++)

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

using namespace std;

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

  int min_price = x * a + y * b;

  for (int i = 0; i <= x; i++) {
    for (int j = 0; j <= y; j++) {
      int k = 2 * max((x - i), (y - j));
      price = i * a + j * b + k * c;
      if (price < min_price)
        min_price = price;
    }
  }

  cout << min_price << endl;

  return 0;
}


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

using namespace std;

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

  int min_price = x * a + y * b;

  for (int i = 0; i <= 100000; i++) {
    price = 2 * c * i + a * max(0, x - i) + b * max(0, y - i);
    if (price < min_price)
      min_price = price;
  }

  cout << min_price << endl;

  return 0;
}
Problem p03371 (C++)

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

int A, B, C, X, Y;

int main() {
  scanf("%d %d %d %d %d", &A, &B, &C, &X, &Y);
  int cost = 1000000000;
  for (int i = 0; i <= X; i++) {
    for (int j = 0; j <= Y; j++) {
      int k = 2 * max(X - i, Y - j);
      int AP = i + k / 2;
      int BP = j + k / 2;
      if (AP >= X && BP >= Y) {
        int c = A * i + B * j + C * k;
        if (c < cost) {
          cost = c;
        }
      }
    }
  }

  printf("%d\n", cost);
  return 0;
}


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

int A, B, C, X, Y;

int main() {
  scanf("%d %d %d %d %d", &A, &B, &C, &X, &Y);

  int cost = 0;
  int k = 2 * min(X, Y);
  cost += C * k;
  cost += A * max(X - k / 2, 0);
  cost += B * max(Y - k / 2, 0);

  int cost2 = 0;
  k = 2 * max(X, Y);
  cost2 += C * k;
  cost2 += A * max(X - k / 2, 0);
  cost2 += B * max(Y - k / 2, 0);
  if (cost2 < cost)
    cost = cost2;

  cost2 = 0;
  k = 0;
  cost2 += A * X;
  cost2 += B * Y;
  if (cost2 < cost)
    cost = cost2;

  printf("%d\n", cost);
  return 0;
}
Problem p03371 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
long long a, b, c, x, y, count = 1000000000000;
int main() {
  cin >> a >> b >> c >> x >> y;

  for (int i = 0; i <= x; i++) {
    for (int j = 0; j <= y; j++) {
      count = min(count, a * i + b * j + 2 * c * max(x - i, y - j));
    }
  }
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
long long a, b, c, x, y, count = 1000000000000;
int main() {
  cin >> a >> b >> c >> x >> y;

  for (int i = 0; i <= 2 * max(x, y); i++) {
    int ma = (x - i / 2) * a > 0 ? (x - i / 2) * a : 0;
    int mb = (y - i / 2) * b > 0 ? (y - i / 2) * b : 0;
    count = min(count, c * i + ma + mb);
  }
  cout << count << endl;
  return 0;
}
Problem p03371 (C++)

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

int main() {
  int A, B, C, X, Y;
  cin >> A >> B >> C >> X >> Y;
  long m = 1000000000, sum;
  for (int i = 0; i <= X; i++)
    for (int j = 0; j <= Y; j++) {
      sum = i * A + j * B + max(X - i, Y - j) * 2 * C;
      m = min(sum, m);
    }
  cout << m;
}

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

int main() {
  int A, B, C, X, Y;
  cin >> A >> B >> C >> X >> Y;
  long m = 1000000000;
  int s1 = A * X + B * Y;
  int s2 = C * 2 * max(X, Y);
  int s3;
  if (X > Y) {
    s3 = C * 2 * Y + A * (X - Y);
  } else
    s3 = C * 2 * X + B * (Y - X);
  s2 = min(s3, s2);

  cout << min(s2, s1);
}
Problem p03371 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

struct p {
  int h;
  int w;
  int n;
  bool flag = false;
};
int main() {
  ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
#else
  freopen("1.txt", "r", stdin);
#endif
  int a, b, c, x, y;
  cin >> a >> b >> c >> x >> y;
  int commont_part = (x < y ? x : y);
  int sum = 0;
  if (a + b > 2 * c)
    sum += 2 * c * commont_part;
  else
    sum += commont_part * (a + b);
  if ((x - commont_part) * a + (y - commont_part) * b <
      2 * c * (x - commont_part + y - commont_part)) {
    sum += (x - commont_part) * a + (y - commont_part) * b;

  } else {
    sum += 2 * c * (x - commont_part + y - commont_part);
  }
  cout << sum;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

struct p {
  int h;
  int w;
  int n;
  bool flag = false;
};
int main() {
  ios::sync_with_stdio(false);
  /***
  #ifdef ONLINE_JUDGE
  #else
      freopen("1.txt", "r", stdin);
  #endif
  ***/
  int a, b, c, x, y;
  cin >> a >> b >> c >> x >> y;
  int commont_part = (x < y ? x : y);
  int sum = 0;
  if (a + b > 2 * c)
    sum += 2 * c * commont_part;
  else
    sum += commont_part * (a + b);
  if ((x - commont_part) * a + (y - commont_part) * b <
      2 * c * (x - commont_part + y - commont_part)) {
    sum += (x - commont_part) * a + (y - commont_part) * b;

  } else {
    sum += 2 * c * (x - commont_part + y - commont_part);
  }
  cout << sum;

  return 0;
}
Problem p03371 (C++)

Buggy submission (status: Runtime Error):
/*
  Author: Hamza Hasbi
*/
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define uld unsigned long double
#define ui unsigned int
#define ud unsigned double
#define uf unsigned float
#define pi 2 * acos(0.0)
#define module cin.ignore()
#define rep(i, l, r) for (auto i = l; i <= r; i++)
#define range(x, y) for (auto x : y)
// inline long long lcm(ll a,ll b) {return a*b/__gcd(a,b);}
// inline long long gcd(ll a,ll b) {return 1LL*b == 0 ? a : gcd(1LL*b, a*1LL %
// b*1LL);}
using namespace std;
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

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

  int a, b, c, x, y;
  cin >> a >> b >> c >> x >> y;
  int ans = 0;
  int curr = min(x, y);

  if (a + b <= 2 * c) {
    ans += curr * (a);
    ans += curr * (b);
    x -= curr;
    y -= curr;
  } else {
    ans += 2 * curr * c;
    x -= curr;
    y -= curr;
  }

  ans += min(2 * c, a) * x;
  ans += min(2 * c, b) * y;

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


Fixed submission:
/*
  Author: Hamza Hasbi
*/
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define uld unsigned long double
#define ui unsigned int
#define ud unsigned double
#define uf unsigned float
#define pi 2 * acos(0.0)
#define module cin.ignore()
#define rep(i, l, r) for (auto i = l; i <= r; i++)
#define range(x, y) for (auto x : y)
// inline long long lcm(ll a,ll b) {return a*b/__gcd(a,b);}
// inline long long gcd(ll a,ll b) {return 1LL*b == 0 ? a : gcd(1LL*b, a*1LL %
// b*1LL);}
using namespace std;
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int a, b, c, x, y;
  cin >> a >> b >> c >> x >> y;
  int ans = 0;
  int curr = min(x, y);

  if (a + b <= 2 * c) {
    ans += curr * (a);
    ans += curr * (b);
    x -= curr;
    y -= curr;
  } else {
    ans += 2 * curr * c;
    x -= curr;
    y -= curr;
  }

  ans += min(2 * c, a) * x;
  ans += min(2 * c, b) * y;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second

const int mx = 100010;
const ll mod = 1e9 + 7;

int main() {
  int n;
  ll c;
  cin >> n >> c;
  vector<ll> migi(n + 2, 0);
  vector<ll> hidari(n + 2, 0);
  vector<ll> m_migi(n + 2, 0);
  vector<ll> m_hidari(n + 2, 0);
  vector<ll> x(n + 1, 0);
  vector<ll> v(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> v[i];
  }
  x[n + 1] = c;
  for (int i = 1; i <= n; i++) {
    migi[i] = migi[i - 1] + v[i] - (x[i] - x[i - 1]);
    hidari[i] = hidari[i - 1] + v[n + 1 - i] - (x[n + 2 - i] - x[n + 1 - i]);
    m_migi[i] = max(migi[i], m_migi[i - 1]);
    m_hidari[i] = max(hidari[i], m_hidari[i - 1]);
  }
  ll ans = max(m_migi[n], m_hidari[n]);
  for (int i = 1; i <= n; i++) {
    ans = max(ans, migi[i] + m_hidari[n - i] - x[i]);
    ans = max(ans, hidari[i] + m_migi[n - i] - (c - x[n + 1 - i]));
    //  cout << ans << endl;
  }
  //  cout << endl;

  /*  cout << m_migi[0] << endl;
    cout << m_migi[1] << endl;
    cout << m_migi[2] << endl;
    cout << m_migi[3] << endl;
    cout << endl;
    cout << m_hidari[0] << endl;
    cout << m_hidari[1] << endl;
    cout << m_hidari[2] << endl;
    cout << m_hidari[3] << endl;
    cout << endl;*/
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second

const int mx = 100010;
const ll mod = 1e9 + 7;

int main() {
  int n;
  ll c;
  cin >> n >> c;
  vector<ll> migi(mx, 0);
  vector<ll> hidari(mx, 0);
  vector<ll> m_migi(mx, 0);
  vector<ll> m_hidari(mx, 0);
  vector<ll> x(mx, 0);
  vector<ll> v(mx, 0);
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> v[i];
  }
  x[n + 1] = c;
  for (int i = 1; i <= n; i++) {
    migi[i] = migi[i - 1] + v[i] - (x[i] - x[i - 1]);
    hidari[i] = hidari[i - 1] + v[n + 1 - i] - (x[n + 2 - i] - x[n + 1 - i]);
    m_migi[i] = max(migi[i], m_migi[i - 1]);
    m_hidari[i] = max(hidari[i], m_hidari[i - 1]);
  }
  ll ans = max(m_migi[n], m_hidari[n]);
  for (int i = 1; i <= n; i++) {
    ans = max(ans, migi[i] + m_hidari[n - i] - x[i]);
    ans = max(ans, hidari[i] + m_migi[n - i] - (c - x[n + 1 - i]));
    //  cout << ans << endl;
  }
  //  cout << endl;

  /*  cout << m_migi[0] << endl;
    cout << m_migi[1] << endl;
    cout << m_migi[2] << endl;
    cout << m_migi[3] << endl;
    cout << endl;
    cout << m_hidari[0] << endl;
    cout << m_hidari[1] << endl;
    cout << m_hidari[2] << endl;
    cout << m_hidari[3] << endl;
    cout << endl;*/
  cout << ans << endl;
  return 0;
}
Problem p03372 (Python)

Buggy submission (status: Runtime Error):
import sys

readline = sys.stdin.readline
MOD = 10**9 + 7
INF = float("INF")
sys.setrecursionlimit(10**5)


def main():
    n, c = list(map(int, readline().split()))

    x = [0] * (n + 1)
    v = [0] * (n + 1)

    for i in range(1, n + 1):
        x[i], v[i] = map(int, readline().split())

    right_cum = [0] * (n + 1)
    left_cum = [0] * (n + 1)

    for i in range(1, n + 1):
        right_cum[i] = right_cum[i - 1] + v[i]
        left_cum[i] = left_cum[i - 1] + v[n - i + 1]

    right_nutritive = [0] * (n + 1)
    left_nutritive = [0] * (n + 1)
    right_max_cum = [0] * (n + 1)
    left_max_cum = [0] * (n + 1)

    for i in range(1, n + 1):
        right_nutritive[i] = right_cum[i] - x[i]
        left_nutritive[i] = left_cum[i] - (c - x[n - i + 1])
        right_max_cum[i] = max(right_max_cum[i - 1], right_nutritive[i])
        left_max_cum[i] = max(left_max_cum[i - 1], left_nutritive[i])

    ans = max(*right_nutritive, *left_nutritive)

    for i in range(1, n + 1):
        rl = right_nutritive[i] - x[i] + left_max_cum[n - i]
        lr = left_nutritive[i] - (c - x[n - i + 1]) + right_max_cum[n - i]
        ans = max(ans, rl, lr)

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys

readline = sys.stdin.readline
MOD = 10**9 + 7
INF = float("INF")
sys.setrecursionlimit(10**5)


def main():
    n, c = list(map(int, readline().split()))

    x = [0] * (n + 1)
    v = [0] * (n + 1)

    for i in range(1, n + 1):
        x[i], v[i] = map(int, readline().split())

    right_cum = [0] * (n + 1)
    left_cum = [0] * (n + 1)

    for i in range(1, n + 1):
        right_cum[i] = right_cum[i - 1] + v[i]
        left_cum[i] = left_cum[i - 1] + v[n - i + 1]

    right_nutritive = [0] * (n + 1)
    left_nutritive = [0] * (n + 1)
    right_max_cum = [0] * (n + 1)
    left_max_cum = [0] * (n + 1)

    for i in range(1, n + 1):
        right_nutritive[i] = right_cum[i] - x[i]
        left_nutritive[i] = left_cum[i] - (c - x[n - i + 1])
        right_max_cum[i] = max(right_max_cum[i - 1], right_nutritive[i])
        left_max_cum[i] = max(left_max_cum[i - 1], left_nutritive[i])

    r_temp = max(right_nutritive)
    l_temp = max(left_nutritive)
    ans = max(r_temp, l_temp)

    for i in range(1, n + 1):
        rl = right_nutritive[i] - x[i] + left_max_cum[n - i]
        lr = left_nutritive[i] - (c - x[n - i + 1]) + right_max_cum[n - i]
        ans = max(ans, rl, lr)

    print(ans)


if __name__ == "__main__":
    main()
Problem p03372 (C++)

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

using namespace std;

int main(void) {
  long long N, C;
  long long x[10010];
  long long v[10010];
  long long f[10010];
  long long fmx[10010];
  long long g[10010];
  long long gmx[10010];

  cin >> N >> C;
  x[0] = 0;
  f[0] = 0;
  fmx[0] = 0;
  g[0] = 0;
  gmx[0] = 0;
  for (int i = 1; i <= N; i++) {
    cin >> x[i] >> v[i];
  }

  // 時計周りに計算、maxも計算
  for (int i = 1; i <= N; i++) {
    f[i] = f[i - 1] + v[i] - (x[i] - x[i - 1]);
    fmx[i] = max(fmx[i - 1], f[i]);
  }
  x[N + 1] = C;
  // 逆時計周りに計算
  for (int i = N; i >= 1; i--) {
    g[N - i + 1] = g[N - i] + v[i] - (x[i + 1] - x[i]);
    gmx[N - i + 1] = max(gmx[N - i], g[N - i + 1]);
  }
  long long ans = 0;

  for (int i = 0; i < N; i++) {
    ans = max(ans, f[i] - x[i] + gmx[N - i]);
    ans = max(ans, g[i] - (C - x[N - i + 1]) + fmx[N - i]);
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

int main(void) {
  long long N, C;
  long long x[100100];
  long long v[100100];
  long long f[100100];
  long long fmx[100100];
  long long g[100100];
  long long gmx[100100];

  cin >> N >> C;
  x[0] = 0;
  f[0] = 0;
  fmx[0] = 0;
  g[0] = 0;
  gmx[0] = 0;
  for (int i = 1; i <= N; i++) {
    cin >> x[i] >> v[i];
  }

  // 時計周りに計算、maxも計算
  for (int i = 1; i <= N; i++) {
    f[i] = f[i - 1] + v[i] - (x[i] - x[i - 1]);
    fmx[i] = max(fmx[i - 1], f[i]);
  }
  x[N + 1] = C;
  // 逆時計周りに計算
  for (int i = N; i >= 1; i--) {
    g[N - i + 1] = g[N - i] + v[i] - (x[i + 1] - x[i]);
    gmx[N - i + 1] = max(gmx[N - i], g[N - i + 1]);
  }
  long long ans = 0;

  for (int i = 0; i < N; i++) {
    ans = max(ans, f[i] - x[i] + gmx[N - i]);
    ans = max(ans, g[i] - (C - x[N - i + 1]) + fmx[N - i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03372 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using VI = vector<long>;

long lefts[101010];
long leftback[101010];
long rights[101010];
long rightback[101010];

int main() {
  long n, c;
  cin >> n >> c;
  VI xs, vs;
  VI kankaku;
  for (int i = 0; i < n; i++) {
    long x, v;
    cin >> x >> v;
    if (i == 0) {
      kankaku.push_back(x);
    } else {
      kankaku.push_back(x - xs.back());
    }
    if (i == n - 1) {
      kankaku.push_back(c - x);
    }
    xs.push_back(x);
    vs.push_back(v);
    lefts[i + 1] = v - kankaku[i] + lefts[i];
    leftback[i + 1] = v - 2 * kankaku[i] + leftback[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    int j = n - 1 - i;
    long v = vs[i];
    rights[j + 1] = v - kankaku[i + 1] + rights[j];
    rightback[j + 1] = v - 2 * kankaku[i + 1] + rightback[j];
  }

  long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, lefts[i]);
    lefts[i] = max(lefts[i], lefts[i - 1]);
    ans = max(ans, rights[i]);
    rights[i] = max(rights[i], rights[i - 1]);
  }
  for (int i = 1; i < n; i++) {
    //        if (rightback[i] + lefts[n - 1 - i] > ans) {
    //            return 1;
    //        }
    if (leftback[i] + rights[n - 1 - i] > ans) {
      return 1;
    }
    ans = max(ans, rightback[i] + lefts[n - 1 - i]);
    //        ans = max(ans, leftback[i] + rights[n - 1 - i]);
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using VI = vector<long>;

long lefts[101010];
long leftback[101010];
long rights[101010];
long rightback[101010];

int main() {
  long n, c;
  cin >> n >> c;
  VI xs, vs;
  VI kankaku;
  for (int i = 0; i < n; i++) {
    long x, v;
    cin >> x >> v;
    if (i == 0) {
      kankaku.push_back(x);
    } else {
      kankaku.push_back(x - xs.back());
    }
    if (i == n - 1) {
      kankaku.push_back(c - x);
    }
    xs.push_back(x);
    vs.push_back(v);
    lefts[i + 1] = v - kankaku[i] + lefts[i];
    leftback[i + 1] = v - 2 * kankaku[i] + leftback[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    int j = n - 1 - i;
    long v = vs[i];
    rights[j + 1] = v - kankaku[i + 1] + rights[j];
    rightback[j + 1] = v - 2 * kankaku[i + 1] + rightback[j];
  }

  long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, lefts[i]);
    lefts[i] = max(lefts[i], lefts[i - 1]);
    ans = max(ans, rights[i]);
    rights[i] = max(rights[i], rights[i - 1]);
  }
  for (int i = 1; i < n; i++) {
    ans = max(ans, rightback[i] + lefts[n - i]);
    ans = max(ans, leftback[i] + rights[n - i]);
  }

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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  ll N, C;
  cin >> N >> C;
  vector<ll> x(N), v(N);
  REP(i, N) cin >> x[i] >> v[i];

  vector<ll> csushi(N), ccsushi(N), csushi_max(N), ccsushi_max(N);
  csushi[0] = v[0];
  ccsushi[0] = v[N - 1];
  for (int i = 0; i < N - 1; ++i)
    csushi[i + 1] = csushi[i] + v[i + 1];
  for (int i = 0; i < N - 1; ++i)
    ccsushi[i + 1] = ccsushi[i] + v[N - 2 - i];
  for (int i = 0; i < N; ++i)
    csushi_max[i + 1] = max(csushi_max[i], csushi[i] - x[i]);
  for (int i = 0; i < N; ++i)
    ccsushi_max[i + 1] = max(ccsushi_max[i], ccsushi[i] - (C - x[N - 1 - i]));

  ll ans = 0;
  REP(i, N) {
    ans = max(ans, csushi[i] - x[i]);
    // return from i
    ans = max(ans, csushi[i] - x[i] * 2 + ccsushi_max[N - 1 - i]);
  }

  REP(i, N) {
    ans = max(ans, ccsushi[i] - (C - x[N - 1 - i]));
    ans = max(ans, ccsushi[i] - (C - x[N - 1 - i]) * 2 + csushi_max[N - 1 - i]);
  }

  cout << ans << '\n';

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  ll N, C;
  cin >> N >> C;
  vector<ll> x(N), v(N);
  REP(i, N) cin >> x[i] >> v[i];

  vector<ll> csushi(N), ccsushi(N), csushi_max(N + 1), ccsushi_max(N + 1);
  csushi[0] = v[0];
  ccsushi[0] = v[N - 1];
  for (int i = 0; i < N - 1; ++i)
    csushi[i + 1] = csushi[i] + v[i + 1];
  for (int i = 0; i < N - 1; ++i)
    ccsushi[i + 1] = ccsushi[i] + v[N - 2 - i];
  for (int i = 0; i < N; ++i)
    csushi_max[i + 1] = max(csushi_max[i], csushi[i] - x[i]);
  for (int i = 0; i < N; ++i)
    ccsushi_max[i + 1] = max(ccsushi_max[i], ccsushi[i] - (C - x[N - 1 - i]));

  ll ans = 0;
  REP(i, N) {
    ans = max(ans, csushi[i] - x[i]);
    // return from i
    ans = max(ans, csushi[i] - x[i] * 2 + ccsushi_max[N - 1 - i]);
  }

  REP(i, N) {
    ans = max(ans, ccsushi[i] - (C - x[N - 1 - i]));
    ans = max(ans, ccsushi[i] - (C - x[N - 1 - i]) * 2 + csushi_max[N - 1 - i]);
  }

  cout << ans << '\n';

  return 0;
}
Problem p03372 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long INF = 2E17;
typedef pair<int, int> Pair;
typedef long long int LL;
const int MAX = 1000;

int main() {
  LL N, C;
  LL x[MAX] = {};
  LL v[MAX] = {};
  cin >> N >> C;
  for (LL i = 0; i < N; i++) {
    cin >> x[i + 1] >> v[i + 1];
  }
  LL CW[MAX] = {};
  LL CCW[MAX] = {};
  x[N + 1] = C;
  for (LL i = 0; i < N; i++) {
    CW[i + 1] = CW[i] + v[i + 1] - x[i + 1] + x[i];
    CCW[i + 1] = CCW[i] + v[N - i] - (x[N - i + 1] - x[N - i]);
  }
  LL CWM[MAX] = {};
  LL CCWM[MAX] = {};
  for (LL i = 0; i < N; i++) {
    CCWM[i + 1] = max(CCWM[i], CCW[i + 1]);
    CWM[i + 1] = max(CWM[i], CW[i + 1]);
  }
  LL ans = 0;
  for (LL i = 0; i <= N; i++) {
    ans = max(ans, CW[i] + CCWM[N - i] - x[i]);
    ans = max(ans, CW[i]);
  }
  for (LL i = 0; i <= N; i++) {
    ans = max(ans, CCW[i] + CWM[N - i] - (C - x[N - i + 1]));
    ans = max(ans, CCW[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long INF = 2E17;
typedef pair<int, int> Pair;
typedef long long int LL;
const int MAX = 100010;

int main() {
  LL N, C;
  LL x[MAX] = {};
  LL v[MAX] = {};
  cin >> N >> C;
  for (LL i = 0; i < N; i++) {
    cin >> x[i + 1] >> v[i + 1];
  }
  LL CW[MAX] = {};
  LL CCW[MAX] = {};
  x[N + 1] = C;
  for (LL i = 0; i < N; i++) {
    CW[i + 1] = CW[i] + v[i + 1] - x[i + 1] + x[i];
    CCW[i + 1] = CCW[i] + v[N - i] - (x[N - i + 1] - x[N - i]);
  }
  LL CWM[MAX] = {};
  LL CCWM[MAX] = {};
  for (LL i = 0; i < N; i++) {
    CCWM[i + 1] = max(CCWM[i], CCW[i + 1]);
    CWM[i + 1] = max(CWM[i], CW[i + 1]);
  }
  LL ans = 0;
  for (LL i = 0; i <= N; i++) {
    ans = max(ans, CW[i] + CCWM[N - i] - x[i]);
    ans = max(ans, CW[i]);
  }
  for (LL i = 0; i <= N; i++) {
    ans = max(ans, CCW[i] + CWM[N - i] - (C - x[N - i + 1]));
    ans = max(ans, CCW[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03372 (C++)

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

#define REP(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;

template <class T, class U> void amax(T &x, U y) {
  if (x < y)
    x = y;
}

int main() {
  int N;
  ll C;
  cin >> N >> C;
  ll *x = new ll[N + 2] + 1;
  vector<ll> v(N);
  x[-1] = 0;
  x[N] = C;
  REP(i, N) cin >> x[i] >> v[i];

  vector<ll> accl_v(N + 1), accr_v(N + 1), accl_max_cal(N + 1),
      accr_max_cal(N + 1);
  accl_v[0] = accr_v[0] = accl_max_cal[0] = accr_max_cal[0] = 0;
  REP(i, N) {
    accl_v[i + 1] = accl_v[i] + v[i];
    accr_v[i + 1] = accr_v[i] + v[N - 1 - i];
    accl_max_cal[i + 1] = max(accl_max_cal[i], accl_v[i + 1] - x[i]);
    accr_max_cal[i + 1] =
        max(accr_max_cal[i], accr_v[i + 1] - (C - x[N - 1 - i]));
  }

  ll cal_max = 0;
  REP(l, N + 1) {
    ll cal = accl_v[l] - 2 * x[l - 1] + accr_max_cal[N - l];
    amax(cal_max, cal);
  }
  REP(r, N + 1) {
    ll cal = accr_v[r] - 2 * (C - x[N - r]) + accl_max_cal[N - r];
    amax(cal_max, cal);
  }
  cout << cal_max << endl;

  delete[] x;
  return 0;
}

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

#define REP(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;

template <class T, class U> void amax(T &x, U y) {
  if (x < y)
    x = y;
}

int main() {
  int N;
  ll C;
  cin >> N >> C;
  ll *x = new ll[N + 2] + 1;
  vector<ll> v(N);
  x[-1] = 0;
  x[N] = C;
  REP(i, N) cin >> x[i] >> v[i];

  vector<ll> accl_v(N + 1), accr_v(N + 1), accl_max_cal(N + 1),
      accr_max_cal(N + 1);
  accl_v[0] = accr_v[0] = accl_max_cal[0] = accr_max_cal[0] = 0;
  REP(i, N) {
    accl_v[i + 1] = accl_v[i] + v[i];
    accr_v[i + 1] = accr_v[i] + v[N - 1 - i];
    accl_max_cal[i + 1] = max(accl_max_cal[i], accl_v[i + 1] - x[i]);
    accr_max_cal[i + 1] =
        max(accr_max_cal[i], accr_v[i + 1] - (C - x[N - 1 - i]));
  }

  ll cal_max = 0;
  REP(l, N + 1) {
    ll cal = accl_v[l] - 2 * x[l - 1] + accr_max_cal[N - l];
    amax(cal_max, cal);
  }
  REP(r, N + 1) {
    ll cal = accr_v[r] - 2 * (C - x[N - r]) + accl_max_cal[N - r];
    amax(cal_max, cal);
  }
  cout << cal_max << endl;

  --x;
  delete[] x;
  return 0;
}
Problem p03372 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target(                                                            \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define N 1000005
#define ll long long
#define debug(a)                                                               \
  cout << #a << ": ";                                                          \
  for (auto i : a)                                                             \
    cout << i << " ";                                                          \
  cout << endl;
#define trace(a) cout << #a << ": " << a << endl;
#define int ll
int a[N];
int b[N];
ll ans[N], v[N];
int n, c;
ll solve() {
  int i;
  ll tot = 0;
  for (i = n - 1; i >= 0; i--) {
    ans[i] = ans[i + 1];
    tot += v[i];
    ans[i] = max(ans[i], -b[i] + tot);
  }
  tot = 0;
  ll an = 0;
  for (i = 0; i < n; i++) {
    tot += v[i];
    an = max({an, tot - a[i], tot - a[i] * 2 + ans[i + 1]});
  }
  return an;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int i, j, k, l, m, t;
  cin >> n >> c;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> v[i];
    b[i] = c - a[i];
  }
  ll an = solve();
  for (i = 0; i < n; i++) {
    a[i] = c - a[i];
    b[i] = c - b[i];
  }
  reverse(a, a + n);
  reverse(b, b + n);
  reverse(v, v + n);
  an = max(an, solve());
  cout << an << endl;
}

Fixed submission:
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define N 1000005
#define ll long long
#define debug(a)                                                               \
  cout << #a << ": ";                                                          \
  for (auto i : a)                                                             \
    cout << i << " ";                                                          \
  cout << endl;
#define trace(a) cout << #a << ": " << a << endl;
#define int ll
int a[N];
int b[N];
ll ans[N], v[N];
int n, c;
ll solve() {
  int i;
  ll tot = 0;
  for (i = n - 1; i >= 0; i--) {
    ans[i] = ans[i + 1];
    tot += v[i];
    ans[i] = max(ans[i], -b[i] + tot);
  }
  tot = 0;
  ll an = 0;
  for (i = 0; i < n; i++) {
    tot += v[i];
    an = max({an, tot - a[i], tot - a[i] * 2 + ans[i + 1]});
  }
  return an;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int i, j, k, l, m, t;
  cin >> n >> c;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> v[i];
    b[i] = c - a[i];
  }
  ll an = solve();
  for (i = 0; i < n; i++) {
    a[i] = c - a[i];
    b[i] = c - b[i];
  }
  reverse(a, a + n);
  reverse(b, b + n);
  reverse(v, v + n);
  an = max(an, solve());
  cout << an << endl;
}
Problem p03372 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
long long n, m, x[10005], y[10005], ans, sum[200005], maxx[200005],
    maxn[200005];
int main() {
  scanf("%lld%lld", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &x[i], &y[i]);
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + y[i];
  }
  for (int i = n; i >= 1; i--) {
    maxx[i] = max(maxx[i + 1], (sum[n] - sum[i - 1]) - (m - x[i]));
  }
  for (int i = 1; i <= n; i++) {
    maxn[i] = max(maxn[i - 1], sum[i] - x[i]);
  }
  for (int i = 1; i <= n; i++) {
    ans = max(ans, max(sum[i] - 2 * x[i] + maxx[i + 1], sum[i] - x[i]));
  }
  for (int i = n; i >= 1; i--) {
    //	cout<<sum[n]<<" "<<sum[i-1]<<" "<<m-x[i]<<" "<<maxn[i-1]<<endl;
    ans = max(ans, (sum[n] - sum[i - 1]) - (m - x[i]) * 2 + maxn[i - 1]);
    ans = max(ans, sum[n] - sum[i - 1] - (m - x[i]));
  }
  cout << ans << endl;
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
long long n, m, x[100005], y[100005], ans, sum[200005], maxx[200005],
    maxn[200005];
int main() {
  scanf("%lld%lld", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &x[i], &y[i]);
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + y[i];
  }
  for (int i = n; i >= 1; i--) {
    maxx[i] = max(maxx[i + 1], (sum[n] - sum[i - 1]) - (m - x[i]));
  }
  for (int i = 1; i <= n; i++) {
    maxn[i] = max(maxn[i - 1], sum[i] - x[i]);
  }
  for (int i = 1; i <= n; i++) {
    ans = max(ans, max(sum[i] - 2 * x[i] + maxx[i + 1], sum[i] - x[i]));
  }
  for (int i = n; i >= 1; i--) {
    //	cout<<sum[n]<<" "<<sum[i-1]<<" "<<m-x[i]<<" "<<maxn[i-1]<<endl;
    ans = max(ans, (sum[n] - sum[i - 1]) - (m - x[i]) * 2 + maxn[i - 1]);
    ans = max(ans, sum[n] - sum[i - 1] - (m - x[i]));
  }
  cout << ans << endl;
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  return 0;
}
Problem p03372 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define IO                                                                     \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, int> li;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const int nmax = 1e5 + 10;
int n;
ll C;
ll x[nmax];
ll v[nmax];
ll f1[nmax];
ll f2[nmax];
int main() {
  IO;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  cin >> n >> C;
  v[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> v[i];
    v[i] += v[i - 1];
  }
  v[n + 1] = v[n];
  f1[0] = 0;
  for (int i = 1; i <= n; i++)
    f1[i] = max(f1[i - 1], v[i] - x[i]);
  f2[n + 1] = 0;
  for (int i = n; i >= 1; i--)
    f2[i] = max(f2[i + 1], v[n] - v[i - 1] - (C - x[i]));
  ll ans = 0;
  for (int i = 1; i <= n + 1; i++) {
    int j = i - 1;
    ll value = f1[j] + v[n] - v[i - 1];
    ll dis;
    if (i == n + 1)
      dis = 0;
    else
      dis = 2 * (C - x[i]);
    ans = max(ans, value - dis);
  }
  for (int i = 0; i <= n; i++) {
    int j = i + 1;
    ll value = f2[j] + v[i];
    ll dis;
    if (i == 0)
      dis = 0;
    else
      dis = 2 * x[i];
    ans = max(ans, value - dis);
  }
  cout << ans;
  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define IO                                                                     \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, int> li;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const int nmax = 1e5 + 10;
int n;
ll C;
ll x[nmax];
ll v[nmax];
ll f1[nmax];
ll f2[nmax];
int main() {
  IO;
  cin >> n >> C;
  v[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> v[i];
    v[i] += v[i - 1];
  }
  v[n + 1] = v[n];
  f1[0] = 0;
  for (int i = 1; i <= n; i++)
    f1[i] = max(f1[i - 1], v[i] - x[i]);
  f2[n + 1] = 0;
  for (int i = n; i >= 1; i--)
    f2[i] = max(f2[i + 1], v[n] - v[i - 1] - (C - x[i]));
  ll ans = 0;
  for (int i = 1; i <= n + 1; i++) {
    int j = i - 1;
    ll value = f1[j] + v[n] - v[i - 1];
    ll dis;
    if (i == n + 1)
      dis = 0;
    else
      dis = 2 * (C - x[i]);
    ans = max(ans, value - dis);
  }
  for (int i = 0; i <= n; i++) {
    int j = i + 1;
    ll value = f2[j] + v[i];
    ll dis;
    if (i == 0)
      dis = 0;
    else
      dis = 2 * x[i];
    ans = max(ans, value - dis);
  }
  cout << ans;
  return 0;
}
Problem p03372 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <vector>

#define FOR(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define FOReq(i, a, b) for (int(i) = (a); (i) <= (b); ++(i))
#define REP(i, n) FOR(i, 0, n)

using lli = long long int;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

std::vector<lli> S, T;
std::vector<lli> SB, TB;
std::vector<lli> X;

int main(void) {
  int n;
  lli C;
  scanf("%d%lld", &n, &C);
  S.resize(n + 1), T.resize(n + 1), X.resize(n + 2);
  X[0] = 0, X[n + 1] = C;
  S[0] = 0;
  T[0] = 0;
  FOReq(i, 1, n) {
    scanf("%lld%lld", &X[i], &S[i]);
    T[n - i + 1] = S[i];
  }

  REP(i, n) {
    S[i + 1] += S[i];
    T[i + 1] += T[i];
  }
  SB.resize(n);
  TB.resize(n);
  FOReq(i, 0, n) {
    SB[i] = S[i] - 2 * X[i];
    TB[i] = T[i] - 2 * (C - X[n - i + 1]);
  }
  REP(i, n) {
    if (SB[i] > SB[i + 1])
      SB[i + 1] = SB[i];
    if (TB[i] > TB[i + 1])
      TB[i + 1] = TB[i];
  }
  lli res = -1;
  FOReq(i, 0, n) {
    chmax(res, S[i] - X[i] + TB[n - i]);
    chmax(res, T[i] - (C - X[n - i + 1]) + SB[n - i]);
  }
  printf("%lld\n", res);

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <vector>

#define FOR(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define FOReq(i, a, b) for (int(i) = (a); (i) <= (b); ++(i))
#define REP(i, n) FOR(i, 0, n)

using lli = long long int;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

std::vector<lli> S, T;
std::vector<lli> SB, TB;
std::vector<lli> X;

int main(void) {
  int n;
  lli C;
  scanf("%d%lld", &n, &C);
  S.resize(n + 1), T.resize(n + 1), X.resize(n + 2);
  X[0] = 0, X[n + 1] = C;
  S[0] = 0;
  T[0] = 0;
  FOReq(i, 1, n) {
    scanf("%lld%lld", &X[i], &S[i]);
    T[n - i + 1] = S[i];
  }

  REP(i, n) {
    S[i + 1] += S[i];
    T[i + 1] += T[i];
  }
  SB.resize(n + 1);
  TB.resize(n + 1);
  FOReq(i, 0, n) {
    SB[i] = S[i] - 2 * X[i];
    TB[i] = T[i] - 2 * (C - X[n - i + 1]);
  }
  REP(i, n) {
    if (SB[i] > SB[i + 1])
      SB[i + 1] = SB[i];
    if (TB[i] > TB[i + 1])
      TB[i + 1] = TB[i];
  }
  lli res = -1;
  FOReq(i, 0, n) {
    chmax(res, S[i] - X[i] + TB[n - i]);
    chmax(res, T[i] - (C - X[n - i + 1]) + SB[n - i]);
  }
  printf("%lld\n", res);

  return 0;
}
Problem p03373 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, a, n) for (int i = a; i <= (n); ++i)

using namespace std;
using ll = long long;
using ull = unsigned long long;

int main() {
  int A;
  cin >> A;
  int B;
  cin >> B;
  int C;
  cin >> C;
  int X;
  cin >> X;
  int Y;
  cin >> Y;

  int min_price = 1 << 30;
  rrep(a, 0, X) {
    rrep(b, 0, Y) {
      int price = A * (X - a) + B * (Y - b) + C * 2 * max(a, b);
      min_price = min(min_price, price);
    }
  }

  cout << min_price << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, a, n) for (int i = a; i <= (n); ++i)

using namespace std;
using ll = long long;
using ull = unsigned long long;

int main() {
  int A;
  cin >> A;
  int B;
  cin >> B;
  int C;
  cin >> C;
  int X;
  cin >> X;
  int Y;
  cin >> Y;

  int min_price = 1 << 30;
  rrep(i, 0, 100000) {
    int price = 2 * C * i; // ABピザは2枚で2C円の1セットと考える
    int a = X - i;         // A ピザの枚数
    int b = Y - i;         // A ピザの枚数
    if (a > 0)
      price += A * a; // A ピザが足りなければ買い足す
    if (b > 0)
      price += B * b; // B ピザが足りなければ買い足す
    min_price = min(min_price, price);
  }

  cout << min_price << endl;
  return 0;
}
Problem p03373 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
a1b1 a1b2 a1b3
a2b1 a2b2 a2b3
a3b1 a3b2 a3b3


*/

#include <iostream>
using namespace std;

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

  // cout << A << B << C <<X << Y << endl;
  int min = A * X + B * Y;
  // cout << min << endl;
  int price;
  for (int a = 0; a <= X; ++a) {   // a: Aピザ購入枚数
    for (int b = 0; b <= Y; ++b) { // a: Aピザ購入枚数
      int a_husoku = X - a;
      int b_husoku = Y - b;
      int c;
      if (a_husoku > b_husoku)
        c = a_husoku * 2;
      else
        c = b_husoku * 2;
      price = a * A + b * B + c * C;
      // cout << price << endl;
      if (min > price)
        min = price;
    }
  }
  cout << min << endl;
}

Fixed submission:
/*
a1b1 a1b2 a1b3
a2b1 a2b2 a2b3
a3b1 a3b2 a3b3


*/

#include <iostream>
using namespace std;

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

  // cout << A << B << C <<X << Y << endl;
  int min = A * X + B * Y;
  // cout << min << endl;
  for (int c = 0; c <= 2 * X + 2 * Y; ++c) { // c: Aピザ購入枚数
    int a = X - c / 2;
    if (a < 0)
      a = 0;
    int b = Y - c / 2;
    if (b < 0)
      b = 0;
    int price = a * A + b * B + c * C;
    if (min > price)
      min = price;
  }
  cout << min << endl;
}
Problem p03373 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, x, y;
  cin >> a >> b >> c >> x >> y;

  int m = a * x + b * y;

  for (int i = 0; i < x + 1; i++) {
    for (int j = 0; j < y + 1; j++) {
      int k = max(2 * x - 2 * i, 2 * y - 2 * j);
      if (m > a * i + b * j + c * k) {
        m = a * i + b * j + c * k;
      }
    }
  }

  cout << m << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, x, y;
  cin >> a >> b >> c >> x >> y;

  int m = a * x + b * y;

  for (int k = 0; k < 2 * max(x, y) + 1; k++) {
    int i = max(0, (x - k / 2));
    int j = max(0, (y - k / 2));

    if (m > a * i + b * j + c * k) {
      m = a * i + b * j + c * k;
    }
  }

  cout << m << endl;
}