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

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

const double EPS = 1e-10;
const ll INF = 100000000;
const ll MOD = 1000000007;

ll n, q;
ll v, w;

int main() {
  cin >> n >> q;
  rep(i, q) {
    cin >> v >> w;
    v--;
    w--;
    vector<int> t1, t2;
    while (v >= 0) {
      t1.push_back(v);
      if (v == 0)
        break;
      v = (v - 1) / n;
    }
    while (w >= 0) {
      t2.push_back(w);
      if (w == 0)
        break;
      w = (w - 1) / n;
    }
    reverse(t1.begin(), t1.end());
    reverse(t2.begin(), t2.end());
    int si = min(t1.size(), t2.size());
    int ans = 0;
    for (int j = 0; j < si; j++) {
      if (t1[j] != t2[j]) {
        break;
      }
      ans = t1[j];
    }
    cout << ans + 1 << endl;
  }
}


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

const double EPS = 1e-10;
const ll INF = 100000000;
const ll MOD = 1000000007;

ll n, q;
ll v, w;

int main() {
  cin >> n >> q;
  rep(i, q) {
    cin >> v >> w;
    if (n == 1) {
      cout << min(v, w) << endl;
      continue;
    }
    v--;
    w--;
    vector<int> t1, t2;
    while (v >= 0) {
      t1.push_back(v);
      if (v == 0)
        break;
      v = (v - 1) / n;
    }
    while (w >= 0) {
      t2.push_back(w);
      if (w == 0)
        break;
      w = (w - 1) / n;
    }
    reverse(t1.begin(), t1.end());
    reverse(t2.begin(), t2.end());
    int si = min(t1.size(), t2.size());
    int ans = 0;
    for (int j = 0; j < si; j++) {
      if (t1[j] != t2[j]) {
        break;
      }
      ans = t1[j];
    }
    cout << ans + 1 << endl;
  }
}
Problem p03506 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int main() {
  int N, Q;
  cin >> N >> Q;
  vector<Int> K, S, powN(100, 1);
  S.push_back(0);
  K.push_back(1), S.push_back(1);
  for (int i = 1; i < 100; i++) {
    powN[i] = powN[i - 1] * N;
  }
  for (int i = 1; i < 100 && K.back() <= 1e9; i++) {
    K.push_back(pow(N, i));
    S.push_back(S.back() + K.back());
  }
  auto f = [&](Int x) {
    int l = lower_bound(begin(S), end(S), x) - begin(S) - 1;
    int i = x - S[l] - 1;
    return make_pair(l, i);
  };
  auto g = [&](auto pv, auto pw) {
    int ok = 0, ng = min(pv.first, pw.first) + 1;
    while (ng - ok > 1) {
      int mid = (ok + ng) / 2;
      int s = pv.second / powN[pv.first - mid];
      int t = pw.second / powN[pw.first - mid];
      (s == t ? ok : ng) = mid;
    }
    return S[ok] + pv.second / powN[pv.first - ok] + 1;
  };
  for (int i = 0; i < Q; i++) {
    int v, w;
    cin >> v >> w;
    cout << g(f(v), f(w)) << '\n';
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int main() {
  int N, Q;
  cin >> N >> Q;
  vector<Int> K, S, powN(100, 1);
  S.push_back(0);
  K.push_back(1), S.push_back(1);
  for (int i = 1; i < 100; i++) {
    powN[i] = powN[i - 1] * N;
  }
  for (int i = 1; i < 100 && K.back() <= 1e9; i++) {
    K.push_back(pow(N, i));
    S.push_back(S.back() + K.back());
  }
  auto f = [&](Int x) {
    int l = lower_bound(begin(S), end(S), x) - begin(S) - 1;
    int i = x - S[l] - 1;
    return make_pair(l, i);
  };
  auto g = [&](auto pv, auto pw) {
    int ok = 0, ng = min(pv.first, pw.first) + 1;
    while (ng - ok > 1) {
      int mid = (ok + ng) / 2;
      int s = pv.second / powN[pv.first - mid];
      int t = pw.second / powN[pw.first - mid];
      (s == t ? ok : ng) = mid;
    }
    return S[ok] + pv.second / powN[pv.first - ok] + 1;
  };
  for (int i = 0; i < Q; i++) {
    int v, w;
    cin >> v >> w;
    if (N == 1)
      cout << min(v, w) << '\n';
    else
      cout << g(f(v), f(w)) << '\n';
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
using namespace std;
const int INF = 1e9;
typedef long long ll;

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  REP(i, q) {
    int sum;
    int v, w;
    scanf("%d %d", &v, &w);
    // cin >> v >> w;
    v--;
    w--;
    if (n == 1) {
      sum = min(v, w);
    }
    while (1) {
      if (v == w) {
        sum = v;
        break;
      } else if (v < w) {
        if (w % n == 0)
          w--;
        w = w / n;
      } else {
        if (v % n == 0)
          v--;
        v = v / n;
      }
    }
    printf("%d\n", sum + 1);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
using namespace std;
const int INF = 1e9;
typedef long long ll;

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  REP(i, q) {
    int sum;
    int v, w;
    scanf("%d %d", &v, &w);
    // cin >> v >> w;
    v--;
    w--;
    if (n == 1) {
      sum = min(v, w);
    } else {
      while (1) {
        if (v == w) {
          sum = v;
          break;
        } else if (v < w) {
          if (w % n == 0)
            w--;
          w = w / n;
        } else {
          if (v % n == 0)
            v--;
          v = v / n;
        }
      }
    }
    printf("%d\n", sum + 1);
  }
  return 0;
}
Problem p03506 (C++)

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

using namespace std;

typedef long long int ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, int> pli;
typedef std::pair<ll, ll> pll;

#define FOR(i, n, m) for (ll i = (ll)(m); i < (ll)(n); ++i)
#define REP(i, n) FOR(i, n, 0)
#define IREP(i, n) for (ll i = (ll)(n); i >= 0; --i)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;
ll N, Q;

ll findParent(ll a, ll b) {
  ll max = std::max(a, b);
  ll min = std::min(a, b);
  while (max != min) {
    max = (max + (N - 2)) / N;
    if (min > max) {
      swap(min, max);
    }
  }
  return min;
}

int main() {
  cin >> N >> Q;
  vector<ll> ans(Q);
  REP(i, Q) {
    ll v, w;
    cin >> v >> w;
    ans[i] = findParent(v, w);
  }
  REP(i, Q) { cout << ans[i] << endl; }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef long long int ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, int> pli;
typedef std::pair<ll, ll> pll;

#define FOR(i, n, m) for (ll i = (ll)(m); i < (ll)(n); ++i)
#define REP(i, n) FOR(i, n, 0)
#define IREP(i, n) for (ll i = (ll)(n); i >= 0; --i)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;
ll N, Q;

ll findParent(ll a, ll b) {
  ll max = std::max(a, b);
  ll min = std::min(a, b);
  if (N != 1) {
    while (max != min) {
      max = (max + (N - 2)) / N;
      if (min > max) {
        swap(min, max);
      }
    }
  }
  return min;
}

int main() {
  cin >> N >> Q;
  vector<ll> ans(Q);
  REP(i, Q) {
    ll v, w;
    cin >> v >> w;
    ans[i] = findParent(v, w);
  }
  REP(i, Q) { cout << ans[i] << endl; }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, q;
int find(int u, int v) {
  while (u != v) {
    while (u > v)
      u = u % n <= 1 ? u / n : u / n + 1;
    while (v > u)
      v = v % n <= 1 ? v / n : v / n + 1;
  }
  return u;
}
int main() {
  scanf("%d%d", &n, &q);
  for (register int i = 1; i <= q; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    printf("%d\n", find(u, v));
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, q;
int find(int u, int v) {
  while (u != v) {
    while (u > v)
      u = u % n <= 1 ? u / n : u / n + 1;
    while (v > u)
      v = v % n <= 1 ? v / n : v / n + 1;
  }
  return u;
}
int main() {
  scanf("%d%d", &n, &q);
  for (register int i = 1; i <= q; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (n == 1)
      printf("%d\n", min(u, v));
    else
      printf("%d\n", find(u, v));
  }
  return 0;
}
Problem p03506 (C++)

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

using namespace std;

int N, Q;
vector<int> vec;

int main(void) {
  cin >> N >> Q;

  int v, w;
  for (int i = 0; i < Q; i++) {
    cin >> v >> w;
    vec.clear();
    while (v > 0) {
      vec.push_back(v);
      v = (v + (N - 2)) / N;
    }
    reverse(vec.begin(), vec.end());
    int ans;
    while (w > 0) {
      if (w == *lower_bound(vec.begin(), vec.end(), w)) {
        ans = w;
        break;
      }
      w = (w + (N - 2)) / N;
    }
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

int N, Q;
vector<int> vec;

int main(void) {
  cin >> N >> Q;

  int v, w;
  for (int i = 0; i < Q; i++) {
    cin >> v >> w;

    if (N == 1) {
      cout << min(v, w) << endl;
      continue;
    }

    vec.clear();
    while (v > 0) {
      vec.push_back(v);
      v = (v + (N - 2)) / N;
    }
    reverse(vec.begin(), vec.end());
    int ans;
    while (w > 0) {
      if (w == *lower_bound(vec.begin(), vec.end(), w)) {
        ans = w;
        break;
      }
      w = (w + (N - 2)) / N;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long i64;
using std::cin;
using std::cout;
using std::endl;

int main() {
  int n;
  cin >> n;
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    std::map<int, bool> used;
    used[a] = true;
    while (a) {
      a = (a - 1) / n;

      used[a] = true;
    }

    if (used[b]) {
      cout << b + 1 << endl;
      continue;
    }

    while (b) {
      b = (b - 1) / n;

      if (used[b]) {
        cout << b + 1 << endl;
        break;
      }
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long i64;
using std::cin;
using std::cout;
using std::endl;

int main() {
  int n;
  cin >> n;
  int q;
  cin >> q;
  if (n == 1) {
    while (q--) {
      int a, b;
      cin >> a >> b;

      cout << std::min(a, b) << endl;
    }
    return 0;
  }

  while (q--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    std::map<int, bool> used;
    used[a] = true;
    while (a) {
      a = (a - 1) / n;

      used[a] = true;
    }

    if (used[b]) {
      cout << b + 1 << endl;
      continue;
    }

    while (b) {
      b = (b - 1) / n;

      if (used[b]) {
        cout << b + 1 << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p03506 (C++)

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

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;

// rep
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(x) cerr << #x << ": " << x << '\n'

int parent(int i, int n) { return (i - 1) / n; }

int n, q;
VI v;

signed main() {
  cin >> n >> q;
  REP(i, q) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    while (a != b) {
      if (a < b) {
        b = parent(b, n);

      } else {
        int temp = b;
        b = a;
        a = temp;
      }
      //  DEBUG(a);
      // DEBUG(b);
    }
    cout << a + 1 << endl;
  }

  return 0;
}

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

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;

// rep
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(x) cerr << #x << ": " << x << '\n'

int parent(int i, int n) { return (i - 1) / n; }

int n, q;
VI v;

signed main() {
  cin >> n >> q;
  if (n == 1) {
    REP(i, q) {
      int a, b;
      cin >> a >> b;
      cout << min(a, b) << endl;
    }
    return 0;
  }
  REP(i, q) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    while (a != b) {
      if (a < b) {
        b = parent(b, n);

      } else {
        int temp = b;
        b = a;
        a = temp;
      }
      //  DEBUG(a);
      // DEBUG(b);
    }
    cout << a + 1 << endl;
  }

  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
int n;
inline int parent(int &x) { return (x - 1) / n; }
int query(int x, int y) {
  if (x == 0 || y == 0)
    return 1;
  vector<int> v;
  while (true) {
    v.push_back(x);
    if (x == 0)
      break;
    x = parent(x);
  }
  int pointer = 0;
  while (true) {
    while (pointer + 1 < (int)v.size() && v[pointer + 1] >= y) {
      ++pointer;
    }
    if (v[pointer] == y)
      return y + 1;
    y = parent(y);
  }
  return 0;
}

int main(int argc, char const *argv[]) {
  int q;
  scanf("%d %d", &n, &q);
  while (q--) {
    int p, q;
    scanf("%d %d", &p, &q);
    printf("%d\n", (int)query(p - 1, q - 1));
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
int n;
inline int parent(int &x) { return (x - 1) / n; }
int query(int x, int y) {
  if (n == 1)
    return min(x, y) + 1;
  if (x == 0 || y == 0)
    return 1;
  vector<int> v;
  while (true) {
    v.push_back(x);
    if (x == 0)
      break;
    x = parent(x);
  }
  int pointer = 0;
  while (true) {
    while (pointer + 1 < (int)v.size() && v[pointer + 1] >= y) {
      ++pointer;
    }
    if (v[pointer] == y)
      return y + 1;
    y = parent(y);
  }
  return 0;
}

int main(int argc, char const *argv[]) {
  int q;
  scanf("%d %d", &n, &q);
  while (q--) {
    int p, q;
    scanf("%d %d", &p, &q);
    printf("%d\n", (int)query(p - 1, q - 1));
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep2(x, from, to) for (long long x = (from); (x) < (to); (x)++)
#define rep(x, to) rep2(x, 0, to)
#define INF 100000000
#define debug(x) cout << #x << ": " << x << endl

#define all(x) x.begin(), x.end()
typedef pair<long long, long long> P;
typedef pair<long long, P> PP;

long long n, q;
long long v, u;
long long del;
int main() {
  cin >> n >> q;
  del = n - 2;
  rep(i, q) {
    cin >> v >> u;
    v += del;
    u += del;
    while (1) {
      if (u == v) {
        cout << u - del << endl;
        break;
      }
      if (u > v) {
        u /= n;
        u += del;
      } else {
        v /= n;
        v += del;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep2(x, from, to) for (long long x = (from); (x) < (to); (x)++)
#define rep(x, to) rep2(x, 0, to)
#define INF 100000000
#define debug(x) cout << #x << ": " << x << endl

#define all(x) x.begin(), x.end()
typedef pair<long long, long long> P;
typedef pair<long long, P> PP;

long long n, q;
long long v, u;
long long del;
int main() {
  cin >> n >> q;
  del = n - 2;
  rep(i, q) {
    cin >> v >> u;
    if (n == 1) {
      if (v > u)
        cout << u << endl;
      else
        cout << v << endl;
      continue;
    }
    v += del;
    u += del;
    while (1) {
      if (u == v) {
        cout << u - del << endl;
        break;
      }
      if (u > v) {
        u /= n;
        u += del;
      } else {
        v /= n;
        v += del;
      }
    }
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, q;
int par(int x) { return (x - 1 + n - 1) / n; }

int main(void) {
  cin >> n >> q;
  while (q--) {
    int v, w;
    cin >> v >> w;
    while (v != w) {
      while (v < w)
        w = par(w);
      if (v == w)
        break;
      v = par(v);
    }
    cout << v << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, q;
int par(int x) { return (x - 1 + n - 1) / n; }

int main(void) {
  cin >> n >> q;
  while (q--) {
    int v, w;
    cin >> v >> w;
    if (n == 1) {
      cout << v << endl;
      continue;
    }
    while (v != w) {
      while (v < w)
        w = par(w);
      if (v == w)
        break;
      v = par(v);
    }
    cout << v << endl;
  }
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>

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

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

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

int main() {
  ll n, q;
  cin >> n >> q;
  REP(_, q) {
    ll v, w;
    cin >> v >> w;
    while (v != w) {
      if (v > w)
        swap(v, w);
      while (v < w) {
        ll t = w % n;
        if (t == 0) {
          w /= n;
        } else if (t == 1) {
          w = (w - 1) / n;
        } else {
          w = (w + (n - t)) / n;
        }
      }
    }
    cout << v << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>

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

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

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

int main() {
  ll n, q;
  cin >> n >> q;
  REP(_, q) {
    ll v, w;
    cin >> v >> w;
    if (n == 1) {
      cout << min(v, w) << endl;
      continue;
    }
    while (v != w) {
      if (v > w)
        swap(v, w);
      while (v < w) {
        ll t = w % n;
        if (t == 0) {
          w /= n;
        } else if (t == 1) {
          w = (w - 1) / n;
        } else {
          w = (w + (n - t)) / n;
        }
      }
    }
    cout << v << endl;
  }
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

int main() {
  int n, q;
  cin >> n >> q;

  rep(i, q) {
    int a, b;
    cin >> a >> b;
    a -= 2;
    b -= 2;

    while (a != b) {
      if (a > b) {
        a = a / n - 1;
      } else {
        b = b / n - 1;
      }
    }
    cout << a + 2 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

int main() {
  int n, q;
  cin >> n >> q;

  rep(i, q) {
    int a, b;
    cin >> a >> b;
    a -= 2;
    b -= 2;

    if (n == 1) {
      cout << min(a, b) + 2 << endl;
      continue;
    }

    while (a != b) {
      if (a > b) {
        a = a / n - 1;
      } else {
        b = b / n - 1;
      }
    }
    cout << a + 2 << endl;
  }
}
Problem p03506 (C++)

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

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int N, Q;
  cin >> N >> Q;
  while (Q--) {
    int u, v;
    cin >> u >> v;
    vector<int> x, y;
    while (u > 0) {
      x.push_back(u);
      u = (u + N - 2) / N;
    }
    while (v > 0) {
      y.push_back(v);
      v = (v + N - 2) / N;
    }
    reverse(y.begin(), y.end());
    for (auto val : x) {
      if (binary_search(y.begin(), y.end(), val)) {
        printf("%d\n", val);
        break;
      }
    }
  }
  return 0;
}


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

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int N, Q;
  cin >> N >> Q;
  while (Q--) {
    int u, v;
    cin >> u >> v;
    if (N == 1) {
      cout << min(u, v) << endl;
      continue;
    }
    vector<int> x, y;
    while (u > 0) {
      x.push_back(u);
      u = (u + N - 2) / N;
    }
    while (v > 0) {
      y.push_back(v);
      v = (v + N - 2) / N;
    }
    reverse(y.begin(), y.end());
    for (auto val : x) {
      if (binary_search(y.begin(), y.end(), val)) {
        printf("%d\n", val);
        break;
      }
    }
  }
  return 0;
}
Problem p03506 (C++)

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

signed main() {
#ifdef LOCAL
  assert(freopen("b.in", "r", stdin));
#endif
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < q; ++i) {
    int u, v;
    cin >> u >> v;
    while (u != v) {
      if (u > v) {
        swap(u, v);
      }
      v = (v + n - 2) / n;
    }
    cout << u << '\n';
  }
}


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

signed main() {
#ifdef LOCAL
  assert(freopen("b.in", "r", stdin));
#endif
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < q; ++i) {
    int u, v;
    cin >> u >> v;
    if (n == 1) {
      u = v = min(u, v);
    }
    while (u != v) {
      if (u > v) {
        swap(u, v);
      }
      v = (v + n - 2) / n;
    }
    cout << u << '\n';
  }
}
Problem p03506 (C++)

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

const int maxn = 2e5 + 10;
typedef long long ll;

/*
1
p*n-n+2 .... p*n+1
(p-1)*n+2, .... p*n+1
all minus 2
(p-1)*n, ...., (p-1)*n+n-1
 */
ll n, q, v, u;
int main() {
  scanf("%lld %lld", &n, &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%lld %lld", &u, &v);
    while (u != v) {
      if (u < v)
        swap(u, v);
      u = (u - 2) / n + 1;
    }
    printf("%lld\n", u);
  }
}


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

const int maxn = 2e5 + 10;
typedef long long ll;

/*
1
p*n-n+2 .... p*n+1
(p-1)*n+2, .... p*n+1
all minus 2
(p-1)*n, ...., (p-1)*n+n-1
 */
ll n, q, v, u;
int main() {
  scanf("%lld %lld", &n, &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%lld %lld", &u, &v);
    if (n == 1) {
      printf("%lld\n", min(u, v));
      continue;
    }
    while (u != v) {
      if (u < v)
        swap(u, v);
      u = (u - 2) / n + 1;
    }
    printf("%lld\n", u);
  }
}
Problem p03506 (C++)

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

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  rep(i, q) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (n == 1) {
      abort();
    }
    set<int> a;
    while (u) {
      a.insert(u);
      u = (u + n - 2) / n;
    }
    while (v) {
      if (a.count(v)) {
        cout << v << endl;
        break;
      }
      v = (v + n - 2) / n;
    }
  }
}

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

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  rep(i, q) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (n == 1) {
      cout << u << endl;
      continue;
    }
    set<int> a;
    while (u) {
      a.insert(u);
      u = (u + n - 2) / n;
    }
    while (v) {
      if (a.count(v)) {
        cout << v << endl;
        break;
      }
      v = (v + n - 2) / n;
    }
  }
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

typedef long long ll;

int n, q;

int main() {
  cin >> n >> q;
  for (int i = 0; i < q; ++i) {
    int v, w;
    cin >> v >> w;
    int dv = 0, dw = 0;
    int vv = v, ww = w;
    while (vv != 1) {
      ++dv;
      vv = (vv + n - 2) / n;
    }
    while (ww != 1) {
      ++dw;
      ww = (ww + n - 2) / n;
    }
    while (dw > dv) {
      w = (w + n - 2) / n;
      --dw;
    }
    while (v != w) {
      v = (v + n - 2) / n;
      w = (w + n - 2) / n;
    }
    cout << v << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

typedef long long ll;

int n, q;

int main() {
  cin >> n >> q;
  for (int i = 0; i < q; ++i) {
    int v, w;
    cin >> v >> w;
    if (n == 1) {
      cout << min(v, w) << endl;
      continue;
    }
    int dv = 0, dw = 0;
    int vv = v, ww = w;
    while (vv != 1) {
      ++dv;
      vv = (vv + n - 2) / n;
    }
    while (ww != 1) {
      ++dw;
      ww = (ww + n - 2) / n;
    }
    while (dw > dv) {
      w = (w + n - 2) / n;
      --dw;
    }
    while (v != w) {
      v = (v + n - 2) / n;
      w = (w + n - 2) / n;
    }
    cout << v << endl;
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <set>
#include <vector>
#define sqr(x) (x) * (x)
using namespace std;
int n, m, i, j, q, x, y;
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> q;
  while (q--) {
    cin >> x >> y;
    set<int> s;
    while (x != 0) {
      s.insert(x);
      if (x % n > 1)
        x += n - (x % n);
      x /= n;
    }
    x = y;
    while (x != 0) {
      if (s.count(x))
        break;
      if (x % n > 1)
        x += n - (x % n);
      x /= n;
    }
    cout << x << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <set>
#include <vector>
#define sqr(x) (x) * (x)
using namespace std;
int n, m, i, j, q, x, y;
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> q;
  while (q--) {
    cin >> x >> y;
    if (n == 1) {
      cout << min(x, y) << endl;
      continue;
    }
    set<int> s;
    while (x != 0) {
      s.insert(x);
      if (x % n > 1)
        x += n - (x % n);
      x /= n;
    }
    x = y;
    while (x != 0) {
      if (s.count(x))
        break;
      if (x % n > 1)
        x += n - (x % n);
      x /= n;
    }
    cout << x << endl;
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <vector>

std::vector<intmax_t> ancestors(intmax_t v, intmax_t n,
                                const std::vector<intmax_t> &min_sibls) {
  std::vector<intmax_t> res;
  while (v > 1) {
    res.push_back(v);
    auto w = std::upper_bound(min_sibls.begin(), min_sibls.end(), v) - 1;
    v = w[-1] + (v - *w) / n;
  }
  res.push_back(1);
  return res;
}

int lca(int v, int w, intmax_t n, const std::vector<intmax_t> &min_sibls) {
  std::vector<intmax_t> pv = ancestors(v, n, min_sibls);
  std::vector<intmax_t> pw = ancestors(w, n, min_sibls);
  size_t i = 0, j = 0;
  while (pv[i] != pw[j]) {
    if (pv[i] < pw[j]) {
      ++j;
    } else {
      ++i;
    }
  }
  return pv[i];
}

int main() {
  intmax_t N, Q;
  scanf("%jd %jd", &N, &Q);

  if (N == 1) {
    for (int i = 0; i < Q; ++i) {
      int v;
      scanf("%d %*d", &v);
      printf("%d\n", v);
    }
    return 0;
  }

  std::vector<intmax_t> min_sibls{1};
  for (intmax_t i = 2, m = 1; i <= 4e18; i += m) {
    min_sibls.push_back(i);
    m *= N;
  }

  for (int i = 0; i < Q; ++i) {
    int v, w;
    scanf("%d %d", &v, &w);
    printf("%d\n", lca(v, w, N, min_sibls));
  }
}


Fixed submission:
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <vector>

std::vector<intmax_t> ancestors(intmax_t v, intmax_t n,
                                const std::vector<intmax_t> &min_sibls) {
  std::vector<intmax_t> res;
  while (v > 1) {
    res.push_back(v);
    auto w = std::upper_bound(min_sibls.begin(), min_sibls.end(), v) - 1;
    v = w[-1] + (v - *w) / n;
  }
  res.push_back(1);
  return res;
}

int lca(int v, int w, intmax_t n, const std::vector<intmax_t> &min_sibls) {
  std::vector<intmax_t> pv = ancestors(v, n, min_sibls);
  std::vector<intmax_t> pw = ancestors(w, n, min_sibls);
  size_t i = 0, j = 0;
  while (pv[i] != pw[j]) {
    if (pv[i] < pw[j]) {
      ++j;
    } else {
      ++i;
    }
  }
  return pv[i];
}

int main() {
  intmax_t N, Q;
  scanf("%jd %jd", &N, &Q);

  if (N == 1) {
    for (int i = 0; i < Q; ++i) {
      int v;
      scanf("%d %*d", &v);
      printf("%d\n", v);
    }
    return 0;
  }

  std::vector<intmax_t> min_sibls{1};
  for (intmax_t i = 2, m = 1; i <= 2e12; i += m) {
    min_sibls.push_back(i);
    m *= N;
  }

  for (int i = 0; i < Q; ++i) {
    int v, w;
    scanf("%d %d", &v, &w);
    printf("%d\n", lca(v, w, N, min_sibls));
  }
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  while (q--) {
    map<int, int> mp;
    int x, y;
    cin >> x >> y;
    while (x) {
      mp[x]++;
      x = (x + n - 2) / n;
    }
    while (y) {
      if (mp[y]) {
        cout << y << endl;
        break;
      }
      y = (y + n - 2) / n;
    }
  }
}

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  while (q--) {
    map<int, int> mp;
    int x, y;
    cin >> x >> y;
    if (n == 1) {
      cout << x << endl;
      continue;
    }
    while (x) {
      mp[x]++;
      x = (x + n - 2) / n;
    }
    while (y) {
      if (mp[y]) {
        cout << y << endl;
        break;
      }
      y = (y + n - 2) / n;
    }
  }
}
Problem p03506 (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 ll = long long;

int main() {
  ll n, q;
  cin >> n >> q;
  vector<ll> ans;
  while (q--) {
    ll v, w;
    cin >> v >> w;
    v--;
    w--;
    while (v != w) {
      if (v > w)
        swap(w, v);
      w = (w - 1) / n;
    }
    ans.push_back(w);
  }
  for (ll x : ans)
    cout << x + 1 << 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 ll = long long;

int main() {
  ll n, q;
  cin >> n >> q;
  vector<ll> ans;
  while (q--) {
    ll v, w;
    cin >> v >> w;
    v--;
    w--;
    if (n == 1) {
      ans.push_back(min(v, w));
      continue;
    }
    while (v != w) {
      if (v > w)
        swap(w, v);
      w = (w - 1) / n;
    }
    ans.push_back(w);
  }
  for (ll x : ans)
    cout << x + 1 << endl;
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int get_depth(long long a, long long n) {
  int d = 0;
  long long mul = 1, sum = 0;
  while (true) {
    sum += mul;
    if (a < sum)
      break;
    mul *= n;
    d++;
  }
  return d;
}

int main() {
  long long n;
  int q;
  scanf("%lld%d", &n, &q);
  while (q--) {
    long long u, v;
    scanf("%lld%lld", &u, &v);
    u--, v--;
    int ud = get_depth(u, n);
    int vd = get_depth(v, n);
    if (ud > vd) {
      swap(u, v);
      swap(ud, vd);
    }
    while (ud != vd) {
      v = (v - 1) / n;
      vd--;
    }
    while (u != v) {
      u = (u - 1) / n;
      v = (v - 1) / n;
    }
    printf("%lld\n", u + 1);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int get_depth(long long a, long long n) {
  int d = 0;
  long long mul = 1, sum = 0;
  while (true) {
    sum += mul;
    if (a < sum)
      break;
    mul *= n;
    d++;
  }
  return d;
}

int main() {
  long long n;
  int q;
  scanf("%lld%d", &n, &q);
  while (q--) {
    long long u, v;
    scanf("%lld%lld", &u, &v);
    u--, v--;
    if (n == 1) {
      printf("%lld\n", min(u + 1, v + 1));
      continue;
    }
    int ud = get_depth(u, n);
    int vd = get_depth(v, n);
    if (ud > vd) {
      swap(u, v);
      swap(ud, vd);
    }
    while (ud != vd) {
      v = (v - 1) / n;
      vd--;
    }
    while (u != v) {
      u = (u - 1) / n;
      v = (v - 1) / n;
    }
    printf("%lld\n", u + 1);
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
ll INF = 1e15;
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void YorN(bool a) { cout << (a ? "YES" : "NO") << "\n"; }

void solve(ll n, ll a, ll b) {
  while (a != b) {
    if (a < b)
      b = (b + n - 2) / n;
    else
      a = (a + n - 2) / n;
  }
  cout << a << "\n";
}

int main() {
  ll n, q;
  cin >> n >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    solve(n, a, b);
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
ll INF = 1e15;
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void YorN(bool a) { cout << (a ? "YES" : "NO") << "\n"; }

void solve(ll n, ll a, ll b) {
  if (n == 1) {
    cout << min(a, b) << "\n";
    return;
  }
  while (a != b) {
    if (a < b)
      b = (b + n - 2) / n;
    else
      a = (a + n - 2) / n;
  }
  cout << a << "\n";
}

int main() {
  ll n, q;
  cin >> n >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    solve(n, a, b);
  }
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int slove(int x, int y, int n);
int main(void) {
  int N, Q;
  cin >> N >> Q;
  while (Q) {
    Q--;
    int x, y;
    cin >> x >> y;
    cout << slove(x, y, N) << endl;
  }
  return 0;
}
int slove(int x, int y, int n) {
  while (x != y) {
    if (x > y)
      x = (x + n - 2) / n;
    else if (y > x)
      y = (y + n - 2) / n;
  }
  return x;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int slove(int x, int y, int n);
int main(void) {
  int N, Q;
  cin >> N >> Q;
  while (Q) {
    Q--;
    int x, y;
    cin >> x >> y;
    if (N == 1)
      cout << min(x, y) << endl;
    else
      cout << slove(x, y, N) << endl;
  }
  return 0;
}
int slove(int x, int y, int n) {
  while (x != y) {
    if (x > y)
      x = (x + n - 2) / n;
    else if (y > x)
      y = (y + n - 2) / n;
  }
  return x;
}
Problem p03507 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define Max zhi5968
typedef long long ll;
using namespace std;

ll a[100000 + 50], b[100000 + 50];

int main() {
  ll n, k, i;
  cin >> n >> k;
  for (i = 0; i < n; i++)
    scanf("%lld %lld", &a[i], &b[i]);
  ll r = 2999999999999999999, l = 0, now;
  while (l <= r) {
    now = (r + l) / 2;
    ll ans = 0;
    for (i = 0; i < n; i++)
      if (now >= a[i])
        ans += (now - a[i] + b[i]) / b[i];
    if (ans >= k)
      r = now - 1;
    else
      l = now;
  }
  printf("%lld\n", l);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define Max zhi5968
typedef long long ll;
using namespace std;

ll a[100000 + 50], b[100000 + 50];

int main() {
  ll n, k, i;
  cin >> n >> k;
  for (i = 0; i < n; i++)
    scanf("%lld %lld", &a[i], &b[i]);
  ll r = 2999999999999999999, l = 0, now;
  while (l <= r) {
    now = (r + l) / 2;
    ll ans = 0;
    for (i = 0; i < n; i++)
      if (now >= a[i])
        ans += (now - a[i] + b[i]) / b[i];
    if (ans >= k)
      r = now - 1;
    else
      l = now + 1;
  }
  printf("%lld\n", l);
  return 0;
}
Problem p03510 (C++)

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

typedef pair<int, int> P;

signed main() {
  int n, ma = 0, dp[100010] = {};
  vector<P> vec;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, s;
    cin >> x >> s;
    vec.push_back(P(x, s));
  }
  sort(vec.begin(), vec.end());
  for (int i = 0; i < n; i++) {
    if (!i)
      dp[i] = vec[i].second;
    else
      dp[i] = max(vec[i].second,
                  dp[i - 1] + vec[i].second - vec[i].first + vec[i - 1].first);
    ma = max(ma, dp[i]);
  }
  cout << ma << endl;
  return 0;
}

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

typedef pair<int, int> P;

signed main() {
  int n, ma = 0, dp[200010] = {};
  vector<P> vec;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, s;
    cin >> x >> s;
    vec.push_back(P(x, s));
  }
  sort(vec.begin(), vec.end());
  for (int i = 0; i < n; i++) {
    if (!i)
      dp[i] = vec[i].second;
    else
      dp[i] = max(vec[i].second,
                  dp[i - 1] + vec[i].second - vec[i].first + vec[i - 1].first);
    ma = max(ma, dp[i]);
  }
  cout << ma << endl;
  return 0;
}
Problem p03510 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int N, s[100009];
long long x[100009], ss[100009];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> x[i] >> s[i];
  x[N] = 1LL << 60;
  for (int i = 0; i < N; i++)
    ss[i + 1] = s[i] + ss[i];
  long long ret = 0, rm = 1LL << 60;
  for (int i = 0; i < N; i++) {
    rm = min(rm, ss[i] - x[i]);
    ret = max(ret, ss[i + 1] - rm - x[i]);
  }
  cout << ret << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int N, s[200009];
long long x[200009], ss[200009];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> x[i] >> s[i];
  x[N] = 1LL << 60;
  for (int i = 0; i < N; i++)
    ss[i + 1] = s[i] + ss[i];
  long long ret = 0, rm = 1LL << 60;
  for (int i = 0; i < N; i++) {
    rm = min(rm, ss[i] - x[i]);
    ret = max(ret, ss[i + 1] - rm - x[i]);
  }
  cout << ret << endl;
  return 0;
}
Problem p03510 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <vector>
#define sqr(x) (x) * (x)
using namespace std;
long long n, m, i, j, dp[100005], x[100005], val[100005], all, ans = -1e17;
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x[i] >> val[i];
  }
  for (i = 1; i <= n; i++) {
    dp[i] = max(dp[i - 1] - (x[i] - x[i - 1]), 0ll) + val[i];
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <vector>
#define sqr(x) (x) * (x)
using namespace std;
long long n, m, i, j, dp[200005], x[200005], val[200005], all, ans = -1e17;
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x[i] >> val[i];
  }
  for (i = 1; i <= n; i++) {
    dp[i] = max(dp[i - 1] - (x[i] - x[i - 1]), 0ll) + val[i];
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}
Problem p03513 (C++)

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

using namespace std;
long long p, n, m, z;
long long t[100000], a[100000], b[1000], c[100000], d[100000], f[2][100000];
int main() {
  p = 1000000007;
  cin >> n >> m;
  t[0] = 1;
  for (int i = 1; i <= 105; i++)
    t[i] = (t[i - 1] * 2) % p;
  for (int i = 1; i <= m; i++) {
    cin >> a[i] >> b[i];
  }
  for (int j = 1; j <= m; j++)
    for (int i = 1; i <= t[n] - 1; i++) {
      if ((1 & i >> a[j] - 1) && (1 & i >> b[j] - 1))
        c[i]++;
      if ((1 & i >> a[j] - 1) || (1 & i >> b[j] - 1))
        d[i]++;
    }
  for (int i = 1; i <= 2; i++)
    for (int j = 0; j <= t[n] - 1; j++) {
      if (!(j & i))
        continue;
      f[i][j] = t[c[j]];
      for (int k = j; k >= 1; k = (k - 1) & j) {
        f[i][j] = (f[i][j] + p - f[i][j - k] * t[c[k]]) % p;
      }
    }
  for (int i = 0; i <= t[n] - 1; i++)
    for (int j = 0; j <= t[n] - 1; j++) {
      if ((i & j) != 0 || c[i] + c[j] != c[i | j])
        continue;
      z = (z + (f[1][i] * f[2][j]) % p * t[m - d[i | j]]) % p;
    }
  cout << (t[m] - z + p) % p << endl;
}

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

using namespace std;
long long p, n, m, z;
long long t[1000], a[1000], b[1000], c[100000], d[100000], f[3][100000];
int main() {
  p = 1000000007;
  cin >> n >> m;
  t[0] = 1;
  for (int i = 1; i <= 105; i++)
    t[i] = (t[i - 1] * 2) % p;
  for (int i = 1; i <= m; i++) {
    cin >> a[i] >> b[i];
  }
  for (int j = 1; j <= m; j++)
    for (int i = 1; i <= t[n] - 1; i++) {
      if ((1 & i >> a[j] - 1) && (1 & i >> b[j] - 1))
        c[i]++;
      if ((1 & i >> a[j] - 1) || (1 & i >> b[j] - 1))
        d[i]++;
    }
  for (int i = 1; i <= 2; i++)
    for (int j = 0; j <= t[n] - 1; j++) {
      if (!(j & i))
        continue;
      f[i][j] = t[c[j]];
      for (int k = j; k >= 1; k = (k - 1) & j) {
        f[i][j] = (f[i][j] + p - f[i][j - k] * t[c[k]]) % p;
      }
    }
  for (int i = 0; i <= t[n] - 1; i++)
    for (int j = 0; j <= t[n] - 1; j++) {
      if ((i & j) != 0 || c[i] + c[j] != c[i | j])
        continue;
      z = (z + (f[1][i] * f[2][j]) % p * t[m - d[i | j]]) % p;
    }
  cout << (t[m] - z + p) % p << endl;
}
Problem p03514 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
  int n, p[100001];
  scanf("%d", &n);
  for (int i = 0; i < 2 * n; i++)
    scanf("%d", &p[i]);
  sort(p, p + n * 2);
  double ans = 0;
  for (int i = 0; i < 2 * n; i++)
    ans += (p[i] * i) / (double)(n * 2 - 1);
  printf("%.10lf\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
  long long n, p[1000001];
  scanf("%d", &n);
  for (int i = 0; i < 2 * n; i++)
    scanf("%d", &p[i]);
  sort(p, p + n * 2);
  double ans = 0;
  for (int i = 0; i < 2 * n; i++)
    ans += (p[i] * i) / (double)(n * 2 - 1);
  printf("%.10lf\n", ans);
  return 0;
}
Problem p03514 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
int n, p[100009];
long long sum = 0;
int main() {
  cin >> n;
  n *= 2;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  sort(p, p + n);
  for (int i = 0; i < n; i++) {
    sum += 1LL * i * p[i];
  }
  printf("%.15Lf\n", 1.0L * sum / (n - 1));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
int n, p[200009];
long long sum = 0;
int main() {
  cin >> n;
  n *= 2;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  sort(p, p + n);
  for (int i = 0; i < n; i++) {
    sum += 1LL * i * p[i];
  }
  printf("%.15Lf\n", 1.0L * sum / (n - 1));
  return 0;
}
Problem p03514 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 100005
using namespace std;
long /*double*/ long ans = 0;
long long n, a[N];
template <typename T> inline void read(T &x) {
  int f = 0;
  int orz = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      orz = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + (c & 15);
    c = getchar();
  }
  f *= orz;
  x = f;
}
template <typename T> void write(T x) {
  /*if (x==INF)
  {
      write(-1);
      return;
  }*/
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
int main() {
  read(n);
  n = n << 1;
  for (register int i = 1; i <= n; i++)
    read(a[i]);
  sort(a + 1, a + n + 1);
  for (register int i = 1; i <= n; i++)
    ans = ans + (unsigned long long)a[i] * (i - 1);
  /*ans/=(n-1);*/
  printf("%.13Lf", (long double)ans / (n - 1));
  putchar('\n');
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 400005
using namespace std;
long /*double*/ long ans = 0;
long long n, a[N];
template <typename T> inline void read(T &x) {
  int f = 0;
  int orz = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      orz = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + (c & 15);
    c = getchar();
  }
  f *= orz;
  x = f;
}
template <typename T> void write(T x) {
  /*if (x==INF)
  {
      write(-1);
      return;
  }*/
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
int main() {
  read(n);
  n = n << 1;
  for (register int i = 1; i <= n; i++)
    read(a[i]);
  sort(a + 1, a + n + 1);
  for (register int i = 1; i <= n; i++)
    ans = ans + (unsigned long long)a[i] * (i - 1);
  /*ans/=(n-1);*/
  printf("%.13Lf", (long double)ans / (n - 1));
  putchar('\n');
  return 0;
}
Problem p03514 (C++)

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

const int MAXN = 1e5 + 5;

int N;
int a[MAXN];

int main() {
  scanf("%d", &N);
  N *= 2;
  for (int i = 0; i < N; i++)
    scanf("%d", a + i);

  sort(a, a + N);

  double sol = 0;
  for (int i = 0; i < N; i++)
    sol += (double)i * a[i];

  printf("%.10lf\n", sol / (N - 1));

  return 0;
}

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

const int MAXN = 2e5 + 5;

int N;
int a[MAXN];

int main() {
  scanf("%d", &N);
  N *= 2;
  for (int i = 0; i < N; i++)
    scanf("%d", a + i);

  sort(a, a + N);

  double sol = 0;
  for (int i = 0; i < N; i++)
    sol += (double)i * a[i];

  printf("%.10lf\n", sol / (N - 1));

  return 0;
}