prithivMLmods/Coder-Stat huggingface dataset:
Problem p03373 (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;
  long long Min = 1000000000;
  cin >> A >> B >> C >> X >> Y;
  rep(i, X + 1) {
    rep(j, Y + 1) {
      if (A * i + B * j + C * 2 * max(X - i, Y - j) <= Min)
        Min = A * i + B * j + C * 2 * max(X - i, Y - j);
    }
  }
  cout << Min << 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;
  long long Min = 1000000000;
  cin >> A >> B >> C >> X >> Y;
  rep(i, 2 * max(X, Y) + 1) if (Min >= C * 2 * i + A * max(0, X - i) +
                                           B * max(0, Y - i)) Min =
      C * 2 * i + A * max(0, X - i) + B * max(0, Y - i);
  cout << Min << endl;
}
Problem p03373 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
using vi = vector<int>;
using vii = vector<vi>;
using vs = vector<string>;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define vsort(v) sort(v.begin(), v.end())
#define rv(v) reverse(v.begin(), v.end())
#define ll long long
const ll MOD = 1e9 + 7;

int main() {
  int a, b, c, x, y;
  cin >> a >> b >> c >> x >> y;
  ll ans = 1e9 + 7;
  rep(i, x + 1) {
    rep(j, y + 1) {
      int s, t;
      s = 2 * (x - i);
      t = 2 * (y - j);
      s = max(s, t);
      ans = min(ans, (ll)(a * i + b * j + c * s));
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
using vi = vector<int>;
using vii = vector<vi>;
using vs = vector<string>;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define vsort(v) sort(v.begin(), v.end())
#define rv(v) reverse(v.begin(), v.end())
#define ll long long
const ll MOD = 1e9 + 7;

int main() {
  int a, b, c, x, y;
  cin >> a >> b >> c >> x >> y;
  int t = max(x, y);
  ll ans = 1e9 + 2;
  rep(i, 2 * t + 1) {
    int v, w, cost;
    v = x - (i / 2);
    w = y - (i / 2);
    cost = max(0, v) * a + max(0, w) * b + i * c;
    ans = min((ll)cost, ans);
  }
  cout << ans << endl;
}
Problem p03373 (C++)

Buggy submission (status: Time Limit Exceeded):
/* おまじない　*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
typedef long long ll;
typedef long double ld;
using namespace std;
const int MOD = pow(10, 9) + 7;
const ll INF = (1LL << 62);
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
void init() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
}
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(a % b, b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  init();
  ll ans = INF;
  ll A, B, C, X, Y;
  cin >> A >> B >> C >> X >> Y;
  for (ll i = 0; i < pow(10, 8); i += 2) {
    ll num = 0;
    // ABピザをiまい
    num += 2 * i * C;
    if (X >= i)
      num += A * (X - i);
    if (Y >= i)
      num += B * (Y - i);
    ans = min(ans, num);
  }
  cout << ans << endl;
}

Fixed submission:
/* おまじない　*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
typedef long long ll;
typedef long double ld;
using namespace std;
const int MOD = pow(10, 9) + 7;
const ll INF = (1LL << 62);
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
void init() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
}
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(a % b, b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  init();
  ll ans = INF;
  ll A, B, C, X, Y;
  cin >> A >> B >> C >> X >> Y;
  for (ll i = 0; i < pow(10, 7); i++) {
    ll num = 0;
    // ABピザをiまい
    num += 2 * i * C;
    if (X >= i)
      num += A * (X - i);
    if (Y >= i)
      num += B * (Y - i);
    ans = min(ans, num);
  }
  cout << ans << endl;
}
Problem p03373 (C++)

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

int optimize(int pA, int pB, int pAB, int X, int Y) {
  std::vector<std::vector<int>> cost;
  cost.reserve(2 * (X + 1));
  for (int i = 0; i <= 2 * (X); i++)
    cost[i].reserve(2 * (Y + 1));

  for (int i = 0; i <= 2 * X; i++) {
    for (int j = 0; j <= 2 * Y; j++) {
      if (i == 0 && j == 0) {
        cost[i][j] = 0;
      } else if (i == 0 && j == 1) {
        cost[i][j] = std::min(pB, pAB);
      } else if (i == 1 && j == 0) {
        cost[i][j] = std::min(pA, pAB);
      } else {
        int f, s, t;
        f = s = t = INT_MAX;
        if (i - 2 >= 0)
          f = cost[i - 2][j] + pA;
        if (j - 2 >= 0)
          s = cost[i][j - 2] + pB;
        if (i - 1 >= 0 && j - 1 >= 0)
          t = cost[i - 1][j - 1] + pAB;
        cost[i][j] = std::min(f, std::min(s, t));
      }
    }
  }
  return cost[2 * X][2 * Y];
}

int main() {
  int pA, pB, pAB, X, Y;
  std::cin >> pA >> pB >> pAB >> X >> Y;
  int ret = optimize(pA, pB, pAB, X, Y);
  std::cout << ret << std::endl;
}


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

int optimize(int pA, int pB, int pAB, int X, int Y) {
  int fP = std::min(2 * pAB * X + std::max(0, Y - X) * pB, pA * X + pB * Y);
  int sP = std::min(2 * pAB * Y + std::max(0, X - Y) * pA, pA * X + pB * Y);
  return std::min(fP, sP);
}

int main() {
  int pA, pB, pAB, X, Y;
  std::cin >> pA >> pB >> pAB >> X >> Y;
  int ret = optimize(pA, pB, pAB, X, Y);
  std::cout << ret << std::endl;
}
Problem p03373 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

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

  int ans = A * X + B * Y;
  rep(i, 0, X + 1) rep(j, 0, Y + 1) {
    int tmp = i * A + j * B;
    tmp += (X - i > Y - j) ? (X - i) * 2 * C : (Y - j) * 2 * C;
    ans = (tmp < ans) ? tmp : ans;
  }

  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

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

  int ans = inf;
  rep(i, 0, 200001) {
    int x = X - i / 2;
    int y = Y - i / 2;
    int tmp = i * C;
    if (0 < x)
      tmp += x * A;
    if (0 < y)
      tmp += y * B;
    chmin(ans, tmp);
  }

  cout << ans << "\n";
}
Problem p03373 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vt<T>>;
template <class T> using ttt = tuple<T, T>;
using tii = tuple<int, int>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
#define DIV ll(1e9 + 7)
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
// const double PI = M_PI;
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }
// IT 5000兆　欲しい
/* Coding space */
// 最大流
#define MAX_V 100000
int main() {
  ll a, b, c, x, y;
  cin >> a >> b >> c >> x >> y;

  ll ans = LLINF;
  ll tans;
  rep(i, 100000) for (int j = 0; j <= 200000; j += 2) {
    if (i + j / 2 >= x) {
      tans = a * i + c * j + b * (y - j / 2 < 0 ? 0 : y - j / 2);
      ans = min(ans, tans);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vt<T>>;
template <class T> using ttt = tuple<T, T>;
using tii = tuple<int, int>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
#define DIV ll(1e9 + 7)
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
// const double PI = M_PI;
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }
// IT 5000兆　欲しい
/* Coding space */
// 最大流
#define MAX_V 100000
int main() {
  ll a, b, c, x, y;
  cin >> a >> b >> c >> x >> y;

  ll ans = 0;
  ll tar = min(x, y);
  ans += min(tar * 2 * c, tar * (a + b));
  if (x < y)
    swap(x, y), swap(a, b); // x,aが余っている
  ans += min((x - tar) * a, (x - tar) * 2 * c);
  cout << ans << endl;
}
Problem p03373 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int A, B, C, X, Y;
  cin >> A >> B >> C >> X >> Y;
  ll ans = 1LL << 60;
  for (int i = 0; i <= X; i++) {
    for (int j = 0; j <= Y; j++) {
      ll sub = i * A + j * B;
      sub += max((X - i) * 2, (Y - j) * 2) * C;
      ans = min(ans, sub);
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int A, B, C, X, Y;
  cin >> A >> B >> C >> X >> Y;
  ll ans = 1LL << 60;
  for (int i = 0; i <= 2 * max(X, Y); i++) {
    int rx = max(0, X - i / 2);
    int ry = max(0, Y - i / 2);
    ans = min(ans, (ll)i * C + rx * A + ry * B);
  }
  cout << ans << endl;

  return 0;
}
Problem p03373 (C++)

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

#define println(msg) cout << msg << endl

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

  // 計算
  int minPrice = INT_MAX;
  for (int a = 0; a <= X; a++) {
    for (int b = 0; b <= Y; b++) {
      int c = max(X - a, Y - b) * 2;
      int price = a * A + b * B + c * C;
      minPrice = min(minPrice, price);
    }
  }

  // 出力
  println(minPrice);
}

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

#define println(msg) cout << msg << endl

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

  // 計算
  int minPrice = INT_MAX;
  int m = max(X, Y);
  for (int c = 0; c <= (2 * m); c++) {
    int a = max(X - c / 2, 0);
    int b = max(Y - c / 2, 0);
    int price = a * A + b * B + c * C;
    minPrice = min(minPrice, price);
  }

  // 出力
  println(minPrice);
}
Problem p03373 (C++)

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

using namespace std;

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

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

  cout << ans << endl;
}

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

using namespace std;

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

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

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

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

template <typename T> inline void check_max(T a, T &b) {
  if (a > b)
    b = a;
}

namespace o_o {
typedef long long ll;
const int N = 1010;

ll x[N], w[N];

int n;
ll m;

ll f[N], g[N];

void solve() {
  scanf("%d%lld", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", x + i, w + i);
  for (int i = 1; i <= n; i++)
    w[i] += w[i - 1];

  for (int v = 1; v <= n; v++) {
    f[v] = x[v] - w[v - 1];
    g[v] = (-w[v - 1] - (m - x[v]) * 2);
  }
  for (int i = n; i; i--)
    check_max(f[i], f[i - 1]), check_max(g[i], g[i - 1]);

  ll ans = 0;
  for (int i = 1; i <= n; i++)
    check_max(w[i] - x[i], ans);
  for (int i = 1; i <= n; i++)
    check_max(w[n] - w[i - 1] - (m - x[i]), ans);

  for (int u = 1; u < n; u++) {
    check_max((w[u] + w[n] - x[u] * 2 - m) + f[u + 1], ans);
    check_max(w[u] + w[n] - x[u] + g[u + 1], ans);
  }

  printf("%lld\n", ans);
}
} // namespace o_o

int main() {
  o_o::solve();
  return 0;
}


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

template <typename T> inline void check_max(T a, T &b) {
  if (a > b)
    b = a;
}

namespace o_o {
typedef long long ll;
const int N = 101000;

ll x[N], w[N];

int n;
ll m;

ll f[N], g[N];

void solve() {
  scanf("%d%lld", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", x + i, w + i);
  for (int i = 1; i <= n; i++)
    w[i] += w[i - 1];

  for (int v = 1; v <= n; v++) {
    f[v] = x[v] - w[v - 1];
    g[v] = (-w[v - 1] - (m - x[v]) * 2);
  }
  for (int i = n; i; i--)
    check_max(f[i], f[i - 1]), check_max(g[i], g[i - 1]);

  ll ans = 0;
  for (int i = 1; i <= n; i++)
    check_max(w[i] - x[i], ans);
  for (int i = 1; i <= n; i++)
    check_max(w[n] - w[i - 1] - (m - x[i]), ans);

  for (int u = 1; u < n; u++) {
    check_max((w[u] + w[n] - x[u] * 2 - m) + f[u + 1], ans);
    check_max(w[u] + w[n] - x[u] + g[u + 1], ans);
  }

  printf("%lld\n", ans);
}
} // namespace o_o

int main() {
  o_o::solve();
  return 0;
}
Problem p03374 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define max(a, b) (a > b ? a : b)
using namespace std;
const int N = 100050;
int gi() {
  char ch = getchar();
  int x = 0, q = 0;
  while (ch < '0' || ch > '9')
    q = ch == '-' ? 1 : q, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return q ? -x : x;
}
int n, v[N];
ll s[N], x[N], C, ans;
int main() {
  int n = gi();
  cin >> C;
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &x[i]), x[i + n] = x[i], v[i] = v[i + n] = gi();
  for (int i = n + 1; i <= 2 * n; ++i)
    s[i] = s[i - 1] + v[i];
  for (int i = n + 1; i <= 2 * n; ++i)
    s[i] -= x[i];
  for (int i = n + 1; i <= 2 * n; ++i)
    s[i] = max(s[i], s[i - 1]);
  ll sum = 0;
  for (int i = n; i >= 1; --i) {
    sum += v[i];
    ans = max(ans, sum - C + x[i]);
    ans = max(ans, sum - 2 * (C - x[i]) + s[i + n - 1]);
  }
  memset(s, 0, sizeof(s));
  reverse(x + 1, x + 2 * n + 1), reverse(v + 1, v + 2 * n + 1);
  for (int i = n + 1; i <= 2 * n; ++i)
    s[i] = s[i - 1] + v[i];
  for (int i = n + 1; i <= 2 * n; ++i)
    s[i] -= C - x[i];
  for (int i = n + 1; i <= 2 * n; ++i)
    s[i] = max(s[i], s[i - 1]);
  sum = 0;
  for (int i = n; i >= 1; --i) {
    sum += v[i];
    ans = max(ans, sum - x[i]);
    ans = max(ans, sum - 2 * x[i] + s[i + n - 1]);
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define max(a, b) (a > b ? a : b)
using namespace std;
const int N = 200050;
int gi() {
  char ch = getchar();
  int x = 0, q = 0;
  while (ch < '0' || ch > '9')
    q = ch == '-' ? 1 : q, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return q ? -x : x;
}
int n, v[N];
ll s[N], x[N], C, ans;
int main() {
  int n = gi();
  cin >> C;
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &x[i]), x[i + n] = x[i], v[i] = v[i + n] = gi();
  for (int i = n + 1; i <= 2 * n; ++i)
    s[i] = s[i - 1] + v[i];
  for (int i = n + 1; i <= 2 * n; ++i)
    s[i] -= x[i];
  for (int i = n + 1; i <= 2 * n; ++i)
    s[i] = max(s[i], s[i - 1]);
  ll sum = 0;
  for (int i = n; i >= 1; --i) {
    sum += v[i];
    ans = max(ans, sum - C + x[i]);
    ans = max(ans, sum - 2 * (C - x[i]) + s[i + n - 1]);
  }
  memset(s, 0, sizeof(s));
  reverse(x + 1, x + 2 * n + 1), reverse(v + 1, v + 2 * n + 1);
  for (int i = n + 1; i <= 2 * n; ++i)
    s[i] = s[i - 1] + v[i];
  for (int i = n + 1; i <= 2 * n; ++i)
    s[i] -= C - x[i];
  for (int i = n + 1; i <= 2 * n; ++i)
    s[i] = max(s[i], s[i - 1]);
  sum = 0;
  for (int i = n; i >= 1; --i) {
    sum += v[i];
    ans = max(ans, sum - x[i]);
    ans = max(ans, sum - 2 * x[i] + s[i + n - 1]);
  }
  cout << ans;
  return 0;
}
Problem p03374 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define rep(i, m) for (long long i = 0; i < m; i++)
#define FOR(i, n, m) for (long long i = n; i < m; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
int main() {
  long n, c, x[100007], v[100007], a[100007], b[100007], d[100007], e[100007],
      f[100007], g[100007], al, ans;
  cin >> n >> c;
  ans = 0;
  rep(i, n) { cin >> x[i] >> v[i]; }
  rep(i, n) { al += v[i]; }
  a[0] = v[0] - 2 * x[0];
  b[0] = v[0] - x[0];
  rep(i, n) {
    if (i != 0) {
      a[i] = a[i - 1] + v[i] + 2 * x[i - 1];
      b[i] = a[i] - x[i];
      a[i] -= 2 * x[i];
    }
  }
  d[0] = al - 2 * c + 2 * x[0];
  e[0] = al - c + x[0];
  rep(i, n) {
    if (i != 0) {
      d[i] = d[i - 1] - v[i - 1] + 2 * c - 2 * x[i - 1];
      e[i] = d[i];
      d[i] -= 2 * c;
      d[i] += 2 * x[i];
      e[i] -= c;
      e[i] += x[i];
    }
    f[0] = a[0];
    FOR(i, 1, n - 1) {
      if (f[i - 1] < a[i])
        f[i] = a[i];
      else
        f[i] = f[i - 1];
    }
    g[0] = b[0];
    FOR(i, 1, n - 1) {
      if (g[i - 1] < b[i])
        g[i] = b[i];
      else
        g[i] = g[i - 1];
    }
  }
  rep(i, n) {
    if (f[i - 1] + e[i] < g[i - 1] + d[i] && ans < g[i - 1] + d[i])
      ans = g[i - 1] + d[i];
    else if (ans < f[i - 1] + e[i])
      ans = f[i - 1] + e[i];
  }
  rep(i, n) {
    if (ans < b[i])
      ans = b[i];
    if (ans < e[i])
      ans = e[i];
  }
  printf("%ld", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(i, m) for (long long i = 0; i < m; i++)
#define FOR(i, n, m) for (long long i = n; i < m; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
int main() {
  long n, c, x[100007], v[100007], a[100007], b[100007], d[100007], e[100007],
      f[100007], g[100007], al, ans;
  cin >> n >> c;
  ans = 0;
  rep(i, n) { cin >> x[i] >> v[i]; }
  rep(i, n) { al += v[i]; }
  a[0] = v[0] - 2 * x[0];
  b[0] = v[0] - x[0];
  rep(i, n) {
    if (i != 0) {
      a[i] = a[i - 1] + v[i] + 2 * x[i - 1];
      b[i] = a[i] - x[i];
      a[i] -= 2 * x[i];
    }
  }
  d[0] = al - 2 * c + 2 * x[0];
  e[0] = al - c + x[0];
  rep(i, n) {
    if (i != 0) {
      d[i] = d[i - 1] - v[i - 1] + 2 * c - 2 * x[i - 1];
      e[i] = d[i];
      d[i] -= 2 * c;
      d[i] += 2 * x[i];
      e[i] -= c;
      e[i] += x[i];
    }
  }
  f[0] = a[0];
  FOR(i, 1, n - 1) {
    if (f[i - 1] < a[i])
      f[i] = a[i];
    else
      f[i] = f[i - 1];
  }
  g[0] = b[0];
  FOR(i, 1, n - 1) {
    if (g[i - 1] < b[i])
      g[i] = b[i];
    else
      g[i] = g[i - 1];
  }
  rep(i, n) {
    if (f[i - 1] + e[i] < g[i - 1] + d[i] && ans < g[i - 1] + d[i])
      ans = g[i - 1] + d[i];
    else if (ans < f[i - 1] + e[i])
      ans = f[i - 1] + e[i];
  }
  rep(i, n) {
    if (ans < b[i])
      ans = b[i];
    if (ans < e[i])
      ans = e[i];
  }
  printf("%ld", ans);
}
Problem p03374 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 10;
const int INF = int(1e9);
#define ll long long
ll x[maxn], v[maxn];
ll dpr[maxn], dpl[maxn];
void init() {
  memset(x, 0, sizeof(x));
  memset(v, 0, sizeof(v));
  memset(dpr, 0, sizeof(dpr));
  memset(dpl, 0, sizeof(dpl));
}
int main() {
  int n;
  ll c, rsum = 0, lsum = 0, sum = 0, ans = 0;
  init();
  scanf("%d %lld", &n, &c);
  for (int i = 1; i <= n; i++) {
    scanf("%lld %lld", &x[i], &v[i]);
    sum += v[i];
  }
  for (int i = 2; i <= n; i++) {
    rsum += v[i - 1];
    dpr[i] = max(dpr[i - 1], rsum - x[i - 1] * 2);
  }
  for (int i = n - 1; i >= 1; i--) {
    lsum += v[i + 1];
    dpl[i] = max(dpl[i + 1], lsum - (c - x[i + 1]) * 2);
  }
  lsum = 0;
  rsum = 0;
  for (int i = 1; i <= n; i++) {
    rsum += v[i];
    ans = max(ans, rsum - x[i] + dpl[i]);
  }
  for (int i = n; i >= 1; i--) {
    lsum += v[i];
    ans = max(ans, lsum - (c - x[i]) + dpr[i]);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
const int INF = int(1e9);
#define ll long long
ll x[maxn], v[maxn];
ll dpr[maxn], dpl[maxn];
void init() {
  memset(x, 0, sizeof(x));
  memset(v, 0, sizeof(v));
  memset(dpr, 0, sizeof(dpr));
  memset(dpl, 0, sizeof(dpl));
}
int main() {
  int n;
  ll c, rsum = 0, lsum = 0, sum = 0, ans = 0;
  init();
  scanf("%d %lld", &n, &c);
  for (int i = 1; i <= n; i++) {
    scanf("%lld %lld", &x[i], &v[i]);
    sum += v[i];
  }
  for (int i = 2; i <= n; i++) {
    rsum += v[i - 1];
    dpr[i] = max(dpr[i - 1], rsum - x[i - 1] * 2);
  }
  for (int i = n - 1; i >= 1; i--) {
    lsum += v[i + 1];
    dpl[i] = max(dpl[i + 1], lsum - (c - x[i + 1]) * 2);
  }
  lsum = 0;
  rsum = 0;
  for (int i = 1; i <= n; i++) {
    rsum += v[i];
    ans = max(ans, rsum - x[i] + dpl[i]);
  }
  for (int i = n; i >= 1; i--) {
    lsum += v[i];
    ans = max(ans, lsum - (c - x[i]) + dpr[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03374 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
ID: taoxiaa1
PROG: concom
LANG: C++
*/
// URAL ID:248353QC
#include <bits/stdc++.h>
using namespace std;
// FILE *fin, *out;
// fin = fopen("money.fin", "r");
// out = fopen("money.out", "w");
// ofstream fout("money.out");
// ifstream fin("money.in");
long long dp1[100005], dp2[100005], t[100005], s[100005];
int main() {
  int n;
  long long c;
  scanf("%d%lld", &n, &c);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &s[i], &t[i]);
  s[0] = 0, t[0] = 0;
  for (int i = 1; i <= n; i++)
    dp1[i] = dp1[i - 1] + t[i];
  for (int i = n; i >= 1; i--) {
    if (i == n)
      dp2[i] = dp2[0] + t[i];
    else
      dp2[i] = dp2[i + 1] + t[i];
  }
  long long sum = 0;
  for (int i = 1; i <= n; i++)
    for (int j = i; j <= n; j++) {
      sum = max(sum, dp1[j] - s[j]);
      // cout << sum << endl;
      sum = max(sum, dp2[i] - c + s[i]);
      // cout << sum << endl;
      if (i != j) {
        sum = max(sum, dp1[i] - 2 * s[i] + dp2[j] - c + s[j]);
        // cout << sum << endl;
        sum = max(sum, dp2[j] - 2 * c + 2 * s[j] + dp1[i] - s[i]);
        // cout << sum << endl;
      } else {
        sum = max(sum, dp1[i] - 2 * s[i] + dp2[j] - t[j] - c + s[j]);
        // cout << sum << endl;
        sum = max(sum, dp2[j] - 2 * c + 2 * s[j] + dp1[i] - t[i] - s[i]);
        // cout << sum << endl;
      }
    }
  printf("%lld\n", sum);
  return 0;
}


Fixed submission:
/*
ID: taoxiaa1
PROG: concom
LANG: C++
*/
// URAL ID:248353QC
#include <bits/stdc++.h>
using namespace std;
// FILE *fin, *out;
// fin = fopen("money.fin", "r");
// out = fopen("money.out", "w");
// ofstream fout("money.out");
// ifstream fin("money.in");
long long dp1[100005], dp2[100005], t[100005], s[100005];
int main() {
  int n;
  long long c;
  scanf("%d%lld", &n, &c);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &s[i], &t[i]);
  s[0] = 0, t[0] = 0;
  for (int i = 1; i <= n; i++)
    dp1[i] = dp1[i - 1] + t[i];
  for (int i = n; i >= 1; i--) {
    if (i == n)
      dp2[i] = dp2[0] + t[i];
    else
      dp2[i] = dp2[i + 1] + t[i];
  }
  long long sum = 0, temp = 0;
  for (int i = 1; i <= n; i++) {
    sum = max(sum, dp1[i] - s[i]);
    sum = max(dp2[i] - c + s[i] + temp, sum);
    temp = max(temp, dp1[i] - 2 * s[i]);
  }
  temp = 0;
  for (int i = n; i >= 1; i--) {
    sum = max(sum, dp2[i] - c + s[i]);
    sum = max(sum, dp1[i] - s[i] + temp);
    temp = max(dp2[i] - 2 * c + 2 * s[i], temp);
  }
  printf("%lld\n", sum);
  return 0;
}
Problem p03374 (C++)

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

using namespace std;

typedef long long ll;
const int maxn = 10010;

int n;
ll C, x[maxn];
int v[maxn];
ll pre[maxn], suf[maxn];
ll p_sum[maxn], s_sum[maxn];
ll val_p[maxn], val_s[maxn];

int main() {
  cin >> n >> C;
  ll ans1 = 0, ans2 = 0;
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> v[i];
  {
    for (int i = 1; i <= n; i++) {
      p_sum[i] = p_sum[i - 1] + v[i];
    }
    for (int i = n; i >= 1; i--) {
      s_sum[i] = s_sum[i + 1] + v[i];
    }
    for (int i = 1; i <= n; i++) {
      val_p[i] = p_sum[i] - 2 * x[i];
      val_s[i] = s_sum[i] - (C - x[i]);
    }
    for (int i = 1; i <= n; i++)
      pre[i] = max(pre[i - 1], val_p[i]);
    for (int i = n; i >= 1; i--)
      suf[i] = max(suf[i + 1], val_s[i]);
    for (int i = 0; i <= n; i++) {
      ans1 = max(ans1, pre[i] + suf[i + 1]);
    }
  }
  {
    for (int i = 1; i <= n; i++) {
      p_sum[i] = p_sum[i - 1] + v[i];
    }
    for (int i = n; i >= 1; i--) {
      s_sum[i] = s_sum[i + 1] + v[i];
    }
    for (int i = 1; i <= n; i++) {
      val_p[i] = p_sum[i] - x[i];
      val_s[i] = s_sum[i] - 2 * (C - x[i]);
    }
    for (int i = 1; i <= n; i++)
      pre[i] = max(pre[i - 1], val_p[i]);
    for (int i = n; i >= 1; i--)
      suf[i] = max(suf[i + 1], val_s[i]);
    for (int i = 0; i <= n; i++) {
      ans2 = max(ans2, pre[i] + suf[i + 1]);
    }
  }
  cout << max(ans1, ans2) << endl;
  return 0;
}


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

using namespace std;

typedef long long ll;
const int maxn = 100010;

int n;
ll C, x[maxn];
int v[maxn];
ll pre[maxn], suf[maxn];
ll p_sum[maxn], s_sum[maxn];
ll val_p[maxn], val_s[maxn];

int main() {
  cin >> n >> C;
  ll ans1 = 0, ans2 = 0;
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> v[i];
  {
    for (int i = 1; i <= n; i++) {
      p_sum[i] = p_sum[i - 1] + v[i];
    }
    for (int i = n; i >= 1; i--) {
      s_sum[i] = s_sum[i + 1] + v[i];
    }
    for (int i = 1; i <= n; i++) {
      val_p[i] = p_sum[i] - 2 * x[i];
      val_s[i] = s_sum[i] - (C - x[i]);
    }
    for (int i = 1; i <= n; i++)
      pre[i] = max(pre[i - 1], val_p[i]);
    for (int i = n; i >= 1; i--)
      suf[i] = max(suf[i + 1], val_s[i]);
    for (int i = 0; i <= n; i++) {
      ans1 = max(ans1, pre[i] + suf[i + 1]);
    }
  }
  {
    for (int i = 1; i <= n; i++) {
      p_sum[i] = p_sum[i - 1] + v[i];
    }
    for (int i = n; i >= 1; i--) {
      s_sum[i] = s_sum[i + 1] + v[i];
    }
    for (int i = 1; i <= n; i++) {
      val_p[i] = p_sum[i] - x[i];
      val_s[i] = s_sum[i] - 2 * (C - x[i]);
    }
    for (int i = 1; i <= n; i++)
      pre[i] = max(pre[i - 1], val_p[i]);
    for (int i = n; i >= 1; i--)
      suf[i] = max(suf[i + 1], val_s[i]);
    for (int i = 0; i <= n; i++) {
      ans2 = max(ans2, pre[i] + suf[i + 1]);
    }
  }
  cout << max(ans1, ans2) << endl;
  return 0;
}
Problem p03374 (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 p03374 (C++)

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

typedef long long ll;
const int maxn = 1e5 + 10;
ll n, c, x[maxn], v[maxn], a[maxn], b[maxn], ans;

int main() {
  cin >> n >> c;
  for (int i = 1; i <= n; ++i)
    cin >> x[i] >> v[i];
  for (int i = 1; i <= n; ++i)
    a[i] = a[i - 1] + v[i];
  for (int i = n; i; --i)
    b[i] = b[i + 1] + v[i];
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, a[i] - x[i]);
    ans = max(ans, b[i] - (c - x[i]));
  }
  ll tmp1 = -1e18, tmp2 = -1e18;
  for (int i = n - 1; i; --i)
    for (int j = i + 1; j <= n; ++j) {
      tmp1 = max(tmp1, b[i + 1] - (c - x[i + 1]));
      tmp2 = max(tmp2, b[i + 1] - 2 * (c - x[i + 1]));
      ans = max(ans, a[i] + tmp1 - 2 * x[i]);
      ans = max(ans, a[i] + tmp2 - x[i]);
    }
  cout << ans << endl;
  return 0;
}

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

typedef long long ll;
const int maxn = 1e5 + 10;
ll n, c, x[maxn], v[maxn], a[maxn], b[maxn], ans;

int main() {
  cin >> n >> c;
  for (int i = 1; i <= n; ++i)
    cin >> x[i] >> v[i];
  for (int i = 1; i <= n; ++i)
    a[i] = a[i - 1] + v[i];
  for (int i = n; i; --i)
    b[i] = b[i + 1] + v[i];
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, a[i] - x[i]);
    ans = max(ans, b[i] - (c - x[i]));
  }
  ll tmp1 = -1e18, tmp2 = -1e18;
  for (int i = n - 1; i; --i) {
    tmp1 = max(tmp1, b[i + 1] - (c - x[i + 1]));
    tmp2 = max(tmp2, b[i + 1] - 2 * (c - x[i + 1]));
    ans = max(ans, a[i] + tmp1 - 2 * x[i]);
    ans = max(ans, a[i] + tmp2 - x[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03374 (C++)

Buggy submission (status: Time Limit Exceeded):
// by sdfzchy
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL inf = (1ll << 50);
inline LL in() {
  char ch = getchar();
  LL f = 1, tmp = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    tmp = (tmp << 1) + (tmp << 3) + (ch - '0');
    ch = getchar();
  }
  return tmp * f;
}

const int N = 100010;

int n;
LL C;
LL x[N], v[N], f[N], g[N];
LL ans, ff[N], gg[N];

LL sum[N];
LL sum2[N];
LL A, B;
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  //	freopen("out.out","w",stdout);
#endif
  n = in();
  C = in();
  for (int i = 1; i <= n; i++)
    x[i] = in(), v[i] = in();

  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + v[i];
  for (int i = n; i >= 1; i--)
    sum2[i] = sum2[i + 1] + v[i];

  for (int i = 1; i <= n; i++)
    f[i] = max(f[i - 1], sum[i] - x[i]);
  for (int i = n; i >= 1; i--)
    g[i] = max(g[i + 1], sum2[i] - (C - x[i]));
  for (int i = 1; i <= n; i++)
    ff[i] = max(ff[i - 1], sum[i] - 2ll * x[i]);
  for (int i = n; i >= 1; i--)
    gg[i] = max(gg[i + 1], sum2[i] - 2ll * (C - x[i]));

  for (int i = 1; i <= n; i++)
    ans = max(ans, f[i] + gg[i + 1]);
  for (int i = n; i >= 1; i--)
    ans = max(ans, g[i] + ff[i - 1]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
// by sdfzchy
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL inf = (1ll << 50);
inline LL in() {
  char ch = getchar();
  LL f = 1, tmp = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    tmp = (tmp << 1) + (tmp << 3) + (ch - '0');
    ch = getchar();
  }
  return tmp * f;
}

const int N = 100010;

int n;
LL C;
LL x[N], v[N], f[N], g[N];
LL ans, ff[N], gg[N];

LL sum[N];
LL sum2[N];
LL A, B;
int main() {
  n = in();
  C = in();
  for (int i = 1; i <= n; i++)
    x[i] = in(), v[i] = in();

  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + v[i];
  for (int i = n; i >= 1; i--)
    sum2[i] = sum2[i + 1] + v[i];

  for (int i = 1; i <= n; i++)
    f[i] = max(f[i - 1], sum[i] - x[i]);
  for (int i = n; i >= 1; i--)
    g[i] = max(g[i + 1], sum2[i] - (C - x[i]));
  for (int i = 1; i <= n; i++)
    ff[i] = max(ff[i - 1], sum[i] - 2ll * x[i]);
  for (int i = n; i >= 1; i--)
    gg[i] = max(gg[i + 1], sum2[i] - 2ll * (C - x[i]));

  for (int i = 1; i <= n; i++)
    ans = max(ans, f[i] + gg[i + 1]);
  for (int i = n; i >= 1; i--)
    ans = max(ans, g[i] + ff[i - 1]);
  cout << ans << endl;
  return 0;
}
Problem p03374 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>

using namespace std;
typedef unsigned long ul;

int main() {
  long long n, c, x[50001], v[50001];
  long long r[50001], l[50001], ri[50001], li[50001];
  long long m = 0;
  cin >> n >> c;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> v[i];
  }

  r[0] = v[0] - x[0];
  ri[0] = 0;
  m = max(m, r[0]);
  for (int i = 1; i < n; i++) {
    r[i] = r[i - 1] + v[i] - x[i] + x[i - 1];
    ri[i] = r[i] > r[ri[i - 1]] ? i : ri[i - 1];
    m = max(m, r[i]);
  }
  l[n - 1] = v[n - 1] - c + x[n - 1];
  li[n - 1] = n - 1;
  m = max(m, l[n - 1]);
  for (int i = n - 2; i >= 0; i--) {
    l[i] = l[i + 1] + v[i] + x[i] - x[i + 1];
    li[i] = l[i] > l[li[i + 1]] ? i : li[i + 1];
    m = max(m, l[i]);
  }

  for (int i = 0; i < n - 1; i++) {
    m = max(m, r[i] - x[i] + l[li[i + 1]]);
  }
  for (int i = n - 1; i > 0; i--) {
    m = max(m, l[i] - c + x[i] + r[ri[i - 1]]);
  }
  cout << m << endl;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>

using namespace std;
typedef unsigned long ul;

int main() {
  long long n, c, x[100010], v[100010];
  long long r[100010], l[100010], ri[100010], li[100010];
  long long m = 0;
  cin >> n >> c;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> v[i];
  }

  r[0] = v[0] - x[0];
  ri[0] = 0;
  m = max(m, r[0]);
  for (int i = 1; i < n; i++) {
    r[i] = r[i - 1] + v[i] - x[i] + x[i - 1];
    ri[i] = r[i] > r[ri[i - 1]] ? i : ri[i - 1];
    m = max(m, r[i]);
  }
  l[n - 1] = v[n - 1] - c + x[n - 1];
  li[n - 1] = n - 1;
  m = max(m, l[n - 1]);
  for (int i = n - 2; i >= 0; i--) {
    l[i] = l[i + 1] + v[i] + x[i] - x[i + 1];
    li[i] = l[i] > l[li[i + 1]] ? i : li[i + 1];
    m = max(m, l[i]);
  }

  for (int i = 0; i < n - 1; i++) {
    m = max(m, r[i] - x[i] + l[li[i + 1]]);
  }
  for (int i = n - 1; i > 0; i--) {
    m = max(m, l[i] - c + x[i] + r[ri[i - 1]]);
  }
  cout << m << endl;
}
Problem p03374 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 10000 + 20;
LL l[maxn], r[maxn], v[maxn], lsum[maxn], rsum[maxn], c, n, lmax1[maxn],
    lmax2[maxn], rmax1[maxn], rmax2[maxn];
int main() {
  scanf("%lld%lld", &n, &c);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld%lld", &l[i], &v[i]);
    r[i] = c - l[i];
    lsum[i] = lsum[i - 1] + v[i];
  }
  for (int i = n; i >= 1; --i)
    rsum[i] = rsum[i + 1] + v[i];
  for (int i = 1; i <= n; ++i) {
    lmax1[i] = max(lmax1[i - 1], lsum[i] - l[i]);
    lmax2[i] = max(lmax2[i - 1], lsum[i] - 2 * l[i]);
  }
  for (int i = n; i >= 1; --i) {

    rmax1[i] = max(rmax1[i + 1], rsum[i] - r[i]);
    rmax2[i] = max(rmax2[i + 1], rsum[i] - 2 * r[i]);
  }
  LL ans = 0;
  for (int i = 1; i <= n; ++i) {
    LL tmp = 0;
    tmp = max(lmax1[i], rmax1[i]);
    ans = max(ans, tmp);
    tmp = max(lmax1[i] + rmax2[i + 1], rmax1[i] + lmax2[i - 1]);
    ans = max(ans, tmp);
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 100000 + 233;
LL l[maxn], r[maxn], v[maxn], lsum[maxn], rsum[maxn], c, n, lmax1[maxn],
    lmax2[maxn], rmax1[maxn], rmax2[maxn];
int main() {
  scanf("%lld%lld", &n, &c);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld%lld", &l[i], &v[i]);
    r[i] = c - l[i];
    lsum[i] = lsum[i - 1] + v[i];
  }
  for (int i = n; i >= 1; --i)
    rsum[i] = rsum[i + 1] + v[i];
  for (int i = 1; i <= n; ++i) {
    lmax1[i] = max(lmax1[i - 1], lsum[i] - l[i]);
    lmax2[i] = max(lmax2[i - 1], lsum[i] - 2 * l[i]);
  }
  for (int i = n; i >= 1; --i) {

    rmax1[i] = max(rmax1[i + 1], rsum[i] - r[i]);
    rmax2[i] = max(rmax2[i + 1], rsum[i] - 2 * r[i]);
  }
  LL ans = 0;
  for (int i = 1; i <= n; ++i) {
    LL tmp = 0;
    tmp = max(lmax1[i], rmax1[i]);
    ans = max(ans, tmp);
    tmp = max(lmax1[i] + rmax2[i + 1], rmax1[i] + lmax2[i - 1]);
    ans = max(ans, tmp);
  }
  printf("%lld", ans);
  return 0;
}
Problem p03375 (C++)

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

long long N, mod, dp[3009][3009], nr[3009][3009], FinalAns;

void init() {
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= i; j++) {
      if (i == j || i == 0)
        dp[i][j] = 1;
      else
        dp[i][j] = (dp[i - 1][j - 1] + 1LL * dp[i - 1][j] * (j + 1)) % mod;
    }
  }
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      if (i == 0 || j == 0)
        nr[i][j] = 1;
      else
        nr[i][j] = (nr[i - 1][j] + nr[i][j - 1]) % mod;
    }
  }
}

long long modpow(long long a, long long b, long long m) {
  long long p = 1, q = a;
  for (int i = 0; i < 31; i++) {
    if ((b / (1LL << i)) % 2 == 1) {
      p *= q;
      p %= m;
    }
    q *= q;
    q %= m;
  }
  return p;
}

long long ncr(long long n, long long r) { return nr[n - r][r]; }

int main() {
  cin >> N >> mod;
  init();

  for (int i = 0; i <= N; i++) {
    long long A1 = modpow(2, modpow(2, i, mod - 1), mod);
    long long A2 = 0;
    for (int j = 0; j <= N - i; j++) {
      long long K1 = dp[N - i][j];
      long long K2 = modpow(2, 1LL * j * i, mod);
      A2 += K1 * K2;
      A2 %= mod;
    }
    long long ans = (A1 * A2 % mod) * ncr(N, i) % mod;
    if ((N - i) % 2 == 0)
      FinalAns += ans;
    else
      FinalAns -= ans;
  }
  FinalAns += 1000000000LL * mod;
  FinalAns %= mod;
  cout << FinalAns << endl;
  return 0;
}

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

long long N, mod, dp[3009][3009], nr[3009][3009], FinalAns;

void init() {
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= i; j++) {
      if (i == j || i == 0)
        dp[i][j] = 1;
      else
        dp[i][j] = (dp[i - 1][j - 1] + 1LL * dp[i - 1][j] * (j + 1)) % mod;
    }
  }
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      if (i == 0 || j == 0)
        nr[i][j] = 1;
      else
        nr[i][j] = (nr[i - 1][j] + nr[i][j - 1]) % mod;
    }
  }
}

long long modpow(long long a, long long b, long long m) {
  long long p = 1, q = a;
  long long K = 31;
  if (b <= 2500000)
    K = 22;
  for (int i = 0; i < K; i++) {
    if ((b / (1LL << i)) % 2 == 1) {
      p *= q;
      p %= m;
    }
    q *= q;
    q %= m;
  }
  return p;
}

long long ncr(long long n, long long r) { return nr[n - r][r]; }

int main() {
  cin >> N >> mod;
  init();

  for (int i = 0; i <= N; i++) {
    long long A1 = modpow(2, modpow(2, i, mod - 1), mod);
    long long A2 = 0;
    for (int j = 0; j <= N - i; j++) {
      long long K1 = dp[N - i][j];
      long long K2 = modpow(2, 1LL * j * i, mod);
      A2 += K1 * K2;
      A2 %= mod;
    }
    long long ans = (A1 * A2 % mod) * ncr(N, i) % mod;
    if ((N - i) % 2 == 0)
      FinalAns += ans;
    else
      FinalAns -= ans;
  }
  FinalAns += 1000000000LL * mod;
  FinalAns %= mod;
  cout << FinalAns << endl;
  return 0;
}
Problem p03375 (C++)

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

typedef long long LL;
const int MN = 3005;

int Mod;

inline int pw(int e, int m = Mod) {
  int b = 2, a = 1;
  for (; e; e >>= 1, b = (LL)b * b % m)
    if (e & 1)
      a = (LL)a * b % m;
  return a;
}

int N, C[MN][MN], S[MN][MN], Ans;

int main() {
  scanf("%d%d", &N, &Mod);
  for (int i = 0; i <= N; ++i)
    C[i][0] = 1;
  for (int i = 1; i <= N; ++i)
    for (int j = 1; j <= i; ++j)
      C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % Mod;
  S[0][0] = 1;
  for (int i = 1; i <= N + 1; ++i)
    for (int j = 1; j <= i; ++j)
      S[i][j] = (S[i - 1][j - 1] + (LL)S[i - 1][j] * j) % Mod;
  for (int i = 0; i <= N; ++i)
    for (int j = 0; j <= N - i; ++j)
      Ans = (Ans + ((i + j) & 1 ? -1ll : 1ll) * C[N][i + j] *
                       S[i + j + 1][i + 1] % Mod * pw((N - i - j) * i) % Mod *
                       pw(pw(N - i - j, Mod - 1))) %
            Mod;
  printf("%d\n", (Ans + Mod) % Mod);
  return 0;
}

Fixed submission:
#include <cstdio>

typedef long long LL;
const int MN = 3005;

int Mod;

inline int pw(int e, int m = Mod) {
  int b = 2, a = 1;
  for (; e; e >>= 1, b = (LL)b * b % m)
    if (e & 1)
      a = (LL)a * b % m;
  return a;
}

int N, C[MN][MN], S[MN][MN], Ans;

int main() {
  scanf("%d%d", &N, &Mod);
  for (int i = 0; i <= N; ++i)
    C[i][0] = 1;
  for (int i = 1; i <= N; ++i)
    for (int j = 1; j <= i; ++j)
      C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % Mod;
  S[0][0] = 1;
  for (int i = 1; i <= N + 1; ++i)
    for (int j = 1; j <= i; ++j)
      S[i][j] = (S[i - 1][j - 1] + (LL)S[i - 1][j] * j) % Mod;
  for (int i = 0; i <= N; ++i) {
    int Sum = 0, C0 = pw(N - i), Coef = 1;
    for (int j = 0; j <= i; ++j)
      Sum = (Sum + (LL)S[i + 1][j + 1] * Coef) % Mod,
      Coef = (LL)Coef * C0 % Mod;
    Sum = (LL)Sum * C[N][i] % Mod * pw(pw(N - i, Mod - 1)) % Mod;
    Ans = (Ans + (i & 1 ? -Sum : Sum)) % Mod;
  }
  printf("%d\n", (Ans + Mod) % Mod);
  return 0;
}
Problem p03375 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int N, M, fact[3009], inv[3009], factinv[3009], dp[3009][3009], pw[9000009],
    fermat[3009];
int main() {
  cin >> N >> M;
  fact[0] = 1;
  for (int i = 1; i <= N; i++)
    fact[i] = 1LL * fact[i - 1] * i % M;
  inv[1] = 1;
  for (int i = 2; i <= N; i++)
    inv[i] = 1LL * inv[M % i] * (M - M / i) % M;
  factinv[0] = 1;
  for (int i = 1; i <= N; i++)
    factinv[i] = 1LL * inv[i] * factinv[i - 1] % M;
  pw[0] = 1;
  for (int i = 1; i <= N * N; i++)
    pw[i] = 2 * pw[i - 1] % M;
  fermat[0] = 2;
  for (int i = 1; i <= N; i++)
    fermat[i] = 1LL * fermat[i - 1] * fermat[i - 1] % M;
  dp[0][0] = 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= i; j++) {
      dp[i][j] = (dp[i - 1][j - 1] + 1LL * j * dp[i - 1][j]) % M;
    }
  }
  int ret = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; i + j <= N; j++) {
      int val = 1LL * fact[N] * factinv[i] % M * factinv[j] % M *
                factinv[N - i - j] % M * fermat[N - i - j] % M;
      for (int k = 0; k <= j; k++) {
        int x = 1LL * val * dp[j][k] % M * pw[(N - i - j) * k] % M;
        if ((i + j) & 1)
          ret = (M + ret - x) % M;
        else
          ret = (ret + x) % M;
      }
    }
  }
  cout << ret << "\n";
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int N, M, fact[3009], inv[3009], factinv[3009], dp[3009][3009], pw[9000009],
    fermat[3009];
int main() {
  cin >> N >> M;
  fact[0] = 1;
  for (int i = 1; i <= N; i++)
    fact[i] = 1LL * fact[i - 1] * i % M;
  inv[1] = 1;
  for (int i = 2; i <= N; i++)
    inv[i] = 1LL * inv[M % i] * (M - M / i) % M;
  factinv[0] = 1;
  for (int i = 1; i <= N; i++)
    factinv[i] = 1LL * inv[i] * factinv[i - 1] % M;
  pw[0] = 1;
  for (int i = 1; i <= N * N; i++)
    pw[i] = 2 * pw[i - 1] % M;
  fermat[0] = 2;
  for (int i = 1; i <= N; i++)
    fermat[i] = 1LL * fermat[i - 1] * fermat[i - 1] % M;
  dp[0][0] = 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= i; j++) {
      dp[i][j] = (dp[i - 1][j - 1] + 1LL * j * dp[i - 1][j]) % M;
    }
  }
  int ret = 0;
  for (int i = 0; i <= N; i++) {
    int val =
        1LL * fact[N] * factinv[i] % M * factinv[N - i] % M * fermat[N - i] % M;
    if (i & 1)
      val = (val == 0 ? 0 : M - val);
    for (int j = 0; j <= i; j++) {
      ret = (ret + 1LL * val % M * dp[i][j] % M * pw[(N - i) * j]) % M;
      ret =
          (ret + 1LL * (j + 1) * val % M * dp[i][j + 1] % M * pw[(N - i) * j]) %
          M;
    }
  }
  cout << ret << "\n";
  return 0;
}