alexjercan/bugnet huggingface dataset:
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define INF 1e+9
using namespace std;

int main() {
  int m, n, p[10000], c[500], e[500];
  static int dp[501][20001] = {};
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> c[i] >> e[i];
  for (int i = 0; i <= n; i++)
    for (int j = 10000; j <= m + 10000; j++)
      dp[i][j] = INF;
  sort(p, p + m, greater<int>());
  for (int i = 1; i < m; i++)
    p[i] += p[i - 1];
  dp[0][10000] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      dp[i][j + 10000] =
          min(dp[i - 1][j + 10000], dp[i - 1][j + 10000 - c[i - 1]] + e[i - 1]);
    }
  }
  int ma = 0;
  for (int i = 1; i <= m; i++) {
    // cout << p[i - 1] << " " << dp[n][i + 10000] << endl;
    ma = max(ma, p[i - 1] - dp[n][i + 10000]);
  }
  cout << ma << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF 1e+9
using namespace std;

int main() {
  int m, n, p[10000], c[500], e[500];
  static int dp[501][10001] = {};
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> c[i] >> e[i];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      dp[i][j] = INF;
  sort(p, p + m, greater<int>());
  for (int i = 1; i < m; i++)
    p[i] += p[i - 1];
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (j - c[i - 1] < 0)
        dp[i][j] = dp[i - 1][j];
      dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - c[i - 1]] + e[i - 1]);
    }
  }
  int ma = 0;
  for (int i = 1; i <= m; i++) {
    ma = max(ma, p[i - 1] - dp[n][i]);
  }
  cout << ma << endl;
  return 0;
}
Problem p00523 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int main() {
  ifstream ifs("in.txt");
  int n;
  cin >> n;
  VL a(n);
  REP(i, n) cin >> a[i];
  REP(i, n) { a.push_back(a[i]); }
  VL s(2 * n + 1);
  REP(i, 2 * n) s[i + 1] = s[i] + a[i];
  s.push_back(1e17);
  s.push_back(2e17);
  s.push_back(3e17);
  ll l = 1, r = 1e16;
  while (r - l > 1) {
    ll m = (l + r) / 2;
    bool pos = 0;
    REP(i, n) {
      int x = i;
      REP(j, 3) { x = lower_bound(s.begin(), s.end(), s[x] + m) - s.begin(); }
      if (x <= i + n) {
        pos = 1;
        break;
      }
    }
    if (pos)
      l = m;
    else
      r = m;
  }

  cout << l << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int main() {
  int n;
  cin >> n;
  VL a(n);
  REP(i, n) scanf("%lld", &a[i]);
  REP(i, n) { a.push_back(a[i]); }
  ll l = 1, r = 1e16;
  while (r - l > 1) {
    ll m = (l + r) / 2;
    bool pos = 0;
    REP(i, n) {
      int x = i, p = 0;
      ll t = 0;
      while (x < i + n) {
        t += a[x];
        x++;
        if (t >= m) {
          t = 0;
          p++;
        }
        if (p == 3) {
          pos = 1;
          break;
        }
      }
    }
    if (pos)
      l = m;
    else
      r = m;
  }

  cout << l << endl;

  return 0;
}
Problem p00523 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cstdio>

using namespace std;

int a[111111 * 3];
long long sum[111111 * 3];
long long res = 0;
int n;

long long C(int s, int e) {
  long long lim = sum[e] - sum[s];
  // printf("[%d,%d) [%d,%d)\\n",s,e,e,s+n);
  s += n;
  int l = e, r = s;
  while (e < s) {
    int m = (s + e) / 2;
    assert(sum[m] - sum[l] + lim + sum[r] - sum[m] == sum[n]);
    // printf("%d %d %d %lld %lld %lld
    // %lld\\n",e,m,s,sum[m]-sum[l],lim,sum[r]-sum[m],sum[m]-sum[l]+lim+sum[r]-sum[m]);
    if (sum[m] - sum[l] >= sum[r] - sum[m]) {
      s = m;
    } else {
      e = m + 1;
    }
  }
  // s = e;
  assert(lim + sum[r] - sum[s] + sum[s] - sum[l] == sum[n]);
  // printf(":%d %d %lld %lld %lld %lld %d
  // %d\\n",l,r,lim,sum[r]-sum[s],sum[s]-sum[l],lim+sum[r]-sum[s]+sum[s]-sum[l],sum[n],l,r);
  long long ret = min(min(lim, sum[r] - sum[s]), sum[s] - sum[l]);
  res = max(res, ret);
  return ret;
}

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    a[i + n] = a[i];
    a[i + 2 * n] = a[i];
  }
  for (int j = 0; j < 3 * n; j++) {
    sum[j + 1] = sum[j] + a[j];
  }
  int s, e;
  s = 0;
  e = 1;
  for (; e < 2 * n; ++e) {
    while (e < 2 * n && C(s, e) > sum[e] - sum[s]) {
      ++e;
    }
    while (s < e && C(s, e) < sum[e] - sum[s]) {
      ++s;
    }
  }
  printf("%lld\\n", res);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>

using namespace std;

int a[111111 * 2];
long long sum[111111 * 2];
long long res = 0;
int n;

long long C(int s, int e) {
  long long lim = sum[e] - sum[s];
  // printf("[%d,%d) [%d,%d)\\n",s,e,e,s+n);
  s += n;
  int l = e, r = s;
  while (e < s) {
    int m = (s + e) / 2;
    assert(sum[m] - sum[l] + lim + sum[r] - sum[m] == sum[n]);
    // printf("%d %d %d %lld %lld %lld
    // %lld\\n",e,m,s,sum[m]-sum[l],lim,sum[r]-sum[m],sum[m]-sum[l]+lim+sum[r]-sum[m]);
    if (sum[m] - sum[l] >= sum[r] - sum[m]) {
      s = m;
    } else {
      e = m + 1;
    }
  }
  // s = e;
  assert(lim + sum[r] - sum[s] + sum[s] - sum[l] == sum[n]);
  // printf(":%d %d %lld %lld %lld %lld %d
  // %d\\n",l,r,lim,sum[r]-sum[s],sum[s]-sum[l],lim+sum[r]-sum[s]+sum[s]-sum[l],sum[n],l,r);
  long long ret = min(min(lim, sum[r] - sum[s]), sum[s] - sum[l]);
  res = max(res, ret);
  return ret;
}

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    a[i + n] = a[i];
    a[i + 2 * n] = a[i];
  }
  for (int j = 0; j < 3 * n; j++) {
    sum[j + 1] = sum[j] + a[j];
  }
  int s, e;
  s = 0;
  e = 1;
  for (; e < 2 * n; ++e) {
    while (e < 2 * n && C(s, e) > sum[e] - sum[s]) {
      ++e;
    }
    while (s < e && C(s, e) < sum[e] - sum[s]) {
      ++s;
    }
  }
  printf("%lld\\n", res);
  return 0;
}
Problem p00523 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long int ll;
int N;
ll A[200000];
ll AA[200000];
int l = 0;
int r = 1;
int LL, RR, CC;
int main() {
  int i, j, k;
  ll ans = 0;
  ll sum = 0;
  ll sum1;
  ll sum2;
  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
    A[i + N] = A[i];
  }
  AA[0] = A[0];
  for (i = 1; i < 2 * N; i++)
    AA[i] = AA[i - 1] + A[i];
  while (l < N) {
    if (l != 0)
      sum = AA[r - 1] - AA[l - 1];
    else
      sum = AA[r - 1];
    // printf("%d %d %lld\\n", l, r,sum);
    if (sum > AA[N - 1] / 3) {
      l++;
    } else {
      LL = r;
      RR = N + l;
      while (RR - LL > 1) {
        CC = (LL + RR) / 2;
        // printf(":%d %d %lld\\n", LL, RR, AA[CC] - AA[r-1]);
        if (AA[CC] - AA[r - 1] < sum)
          LL = CC;
        else
          RR = CC;
      }
      LL -= 3;
      if (LL <= -1)
        LL = 0;
      while (AA[LL] - AA[r - 1] < sum)
        LL++;
      sum1 = AA[LL] - AA[r - 1];
      sum2 = AA[N - 1] - sum - sum1;
      // printf("::%d %lld %lld\\n", LL, sum1, sum2);
      if (sum <= sum1 && sum <= sum2) {
        ans = max(ans, sum);
        r++;
      } else
        l++;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long int ll;
int N;
ll A[100000];
ll AA[100000];
int main() {
  int i, j, k;
  ll sum1 = 0;
  ll sum2 = 0;
  ll sum3 = 0;
  ll ans = 0;
  scanf("%d", &N);
  for (i = 0; i < N; i++)
    scanf("%lld", &A[i]);
  AA[0] = A[0];
  for (i = 1; i < N; i++)
    AA[i] = AA[i - 1] + A[i];
  for (i = 0; i < N; i++) {
    for (j = i + 1; j < N; j++) {
      for (k = j + 1; k < N; k++) {
        sum1 = 0;
        sum2 = 0;
        sum3 = 0;
        if (i != 0)
          sum3 += AA[i - 1];
        if (i != 0)
          sum1 += AA[j - 1] - AA[i - 1];
        else
          sum1 += AA[j - 1];
        sum2 += AA[k - 1] - AA[j - 1];
        sum3 += AA[N - 1] - AA[k - 1];
        ans = max(ans, min(sum1, min(sum2, sum3)));
      }
    }
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p00523 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MAX_N 100010
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 100000005
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef pair<int, int> PI;

int N;
ll allsum = 0;
ll A[MAX_N * 2], sum[MAX_N * 2];

bool check(ll shrt) {
  int c1, c2, c3;
  for (c1 = 0; c1 < N; c1++) {
    c2 = (int)(lower_bound(sum, sum + 2 * N + 1, sum[c1] + shrt) - sum);
    if (c2 > c1 + N)
      continue;
    c3 = (int)(lower_bound(sum, sum + 2 * N + 1, sum[c2] + shrt) - sum);
    if (c3 > c1 + N)
      continue;
    if (allsum - (sum[c3] - sum[c1]) >= shrt) {
      return true;
    }
  }
  return false;
}

int main() {
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    allsum += A[i];
    A[i + N] = A[i];
  }
  sum[0] = 0;
  rep(i, 2 * N) { sum[i + 1] = sum[i] + A[i]; }
  ll left = 0, right = allsum + 1;
  while (right - left > 1) {
    ll mid = (right + left) / 2;
    if (check(mid)) {
      left = mid;
    } else {
      right = mid;
    }
  }
  cout << left << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MAX_N 100010
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 100000005
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef pair<int, int> PI;

int N;
ll allsum = 0;
ll A[MAX_N], sum[MAX_N];

bool check(ll shrt) {
  int c1, c2, c3;
  for (c1 = 0; c1 < N; c1++) {
    c2 = (int)(lower_bound(sum, sum + 2 * N + 1, sum[c1] + shrt) - sum);
    if (c2 > c1 + N)
      continue;
    c3 = (int)(lower_bound(sum, sum + 2 * N + 1, sum[c2] + shrt) - sum);
    if (c3 > c1 + N)
      continue;
    if (allsum - (sum[c3] - sum[c1]) >= shrt) {
      return true;
    }
  }
  return false;
}

int main() {
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    allsum += A[i];
    A[i + N] = A[i];
  }
  sum[0] = 0;
  rep(i, 2 * N) { sum[i + 1] = sum[i] + A[i]; }
  ll left = 0, right = allsum + 1;
  while (right - left > 1) {
    ll mid = (right + left) / 2;
    if (check(mid)) {
      left = mid;
    } else {
      right = mid;
    }
  }
  cout << left << endl;
}
Problem p00523 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

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

  long long int whole = 0; // バームクーヘンの大きさ
  int *A = new int[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    whole += (long long int)A[i];
  }

  long long int one_third = whole / 3;

  int i = 0;
  long long int piece1 = 0;
  while (piece1 + (long long int)A[i] <= one_third) {
    piece1 += (long long int)A[i];
    i++;
    if (i == N)
      i -= N; // 周期的境界条件
  }

  int piece2_begin = i;
  long long int piece2 = 0;
  while (1) {
    piece2 += (long long int)A[i];
    i++;
    if (i == N)
      i -= N; // 周期的境界条件
    if (piece2 >= piece1)
      break;
  }

  int piece3_begin = i;
  long long int piece3 = whole - piece1 - piece2;
  long long int ans = min(piece1, piece3);

  for (int i = 1; i < N; i++) {
    if (piece1 == 0) {
      piece2 -= (long long int)A[piece2_begin];
      piece2_begin++;
    } else
      piece1 -= (long long int)A[i - 1];

    while (piece1 + (long long int)A[piece2_begin] <= one_third) {
      piece1 += (long long int)A[piece2_begin];
      piece2 -= (long long int)A[piece2_begin];
      piece2_begin++;
      if (piece2_begin == N)
        piece2_begin -= N; // 周期的境界条件
    }

    while (piece2 < piece1) {
      piece2 += (long long int)A[piece3_begin];
      piece3_begin++;
      if (piece3_begin == N)
        piece3_begin -= N; // 周期的境界条件
    }

    piece3 = whole - piece1 - piece2;
    ans = max(ans, min(piece1, piece3));
  }

  cout << ans << endl;

  delete[] A;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

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

  long long int whole = 0; // バームクーヘンの大きさ
  int *A = new int[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    whole += (long long int)A[i];
  }

  const long long int one_third = whole / 3;
  long long int ans = 0;

  for (int i = 0; i < N; i++) {
    int j = i;
    long long int piece1 = 0;
    while (piece1 + (long long int)A[j] <= one_third) {
      piece1 += (long long int)A[j];
      j++;
      if (j == N)
        j -= N; // 周期的境界条件
    }

    long long int piece2 = 0;
    while (1) {
      piece2 += (long long int)A[j];
      j++;
      if (j == N)
        j -= N; // 周期的境界条件
      if (piece2 >= piece1)
        break;
    }

    long long int piece3 = whole - piece1 - piece2;
    long long int tmpans = min(piece1, piece3);
    ans = max(ans, tmpans);
  }

  cout << ans << endl;

  delete[] A;
  return 0;
}
Problem p00523 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll a[300000];
int main() {
  ll n;
  scanf("%lld", &n);
  rep(i, n) {
    scanf("%lld", &a[i + 1]);
    a[i + 1 + n] = a[i + 1];
  }
  for (ll i = 1; i <= 2 * n; i++)
    a[i] += a[i - 1];
  ll s = 0, g = 1000000000 * n;
  while (s != g) {
    ll t = (s + g) / 2;
    if (g - s == 1)
      t = g;
    bool flag = false;
    rep(i, n) {
      ll j = lower_bound(a + i, a + i + n + 1, a[i] + t) - a;
      if (j == n + i + 1)
        continue;
      ll k = lower_bound(a + j, a + i + n + 1, a[j] + t) - a;
      if (a[j] - a[i] >= t && a[k] - a[j] >= t && a[i + n] - a[k] >= t) {
        flag = true;
        break;
      }
    }
    if (flag)
      s = t;
    else
      g = t - 1;
  }
  printf("%lld\\n", s);
}

Error: TLE

Fixed submission:

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

ll a[300000];
int main() {
  ll n;
  scanf("%lld", &n);
  rep(i, n) {
    scanf("%lld", &a[i + 1]);
    a[i + 1 + n] = a[i + 1];
  }
  for (ll i = 1; i <= 2 * n; i++)
    a[i] += a[i - 1];
  ll s = 0, g = 1000000000 * n;
  while (s != g) {
    ll t = (s + g) / 2 + s % 2;
    bool flag = false;
    rep(i, n) {
      ll j = lower_bound(a + i, a + i + n, a[i] + t) - a;
      if (j == n + i)
        continue;
      ll k = lower_bound(a + j, a + i + n, a[j] + t) - a;
      if (a[j] - a[i] >= t && a[k] - a[j] >= t && a[i + n] - a[k] >= t) {
        flag = true;
        break;
      }
    }
    if (flag)
      s = t;
    else
      g = t - 1;
  }
  printf("%lld\\n", s);
}
Problem p00523 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

// int dy[]={0, 0, 1, -1, 0};
// int dx[]={1, -1, 0, 0, 0};

#define int long long

int n;
vector<int> v;
vector<int> pos(200010, 0);

bool ch(int x) {
  int l, r = l = 0, sum = 0;
  while (l < n) {
    while (sum < x && (r + 1) % n != l) {
      sum += v[r];
      r = (r + 1) % n;
    }

    if ((r + 1) % n == l)
      return false;
    pos[l] = r;
    sum -= v[l];
    l++;
  }

  REP(i, n) {
    int l1 = pos[i] - i, l2 = pos[pos[i]] - pos[i],
        l3 = pos[pos[pos[i]]] - pos[pos[i]];

    if (l1 < 0)
      l1 += n;
    if (l2 < 0)
      l2 += n;
    if (l3 < 0)
      l3 += n;
    if (l1 + l2 + l3 <= n)
      return true;
  }

  return false;
}

int32_t main() {
  scanf("%lld", &n);
  int sum = 0;
  REP(i, n) {
    int m;
    scanf("%lld", &m);
    v.push_back(m);
    sum += m;
  }

  int l = 0, r = INF;
  int mid;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (ch(mid))
      l = mid;
    else
      r = mid;
  }

  printf("%lld\\n", l);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

// int dy[]={0, 0, 1, -1, 0};
// int dx[]={1, -1, 0, 0, 0};

#define int long long

int n;
vector<int> v(100010);

bool ch(int x) {
  int j, ret = -1;
  REP(k, n) {
    j = k;
    int res = INF, c = 0;
    REP(i, 3) {
      int sum = 0;
      for (; c < n && sum <= x; c++) {
        sum += v[j];
        j++;
        j %= n;
      }

      res = min(res, sum);
    }

    ret = max(ret, res);
  }

  return ret <= x;
}

int32_t main() {
  scanf("%lld", &n);
  REP(i, n) scanf("%lld", &v[i]);

  int l = 0, r = INF;
  int mid;
  REP(i, 100) {
    mid = (l + r) / 2;
    if (ch(mid))
      r = mid;
    else
      l = mid;
  }

  printf("%lld\\n", r);
}
Problem p00523 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int N;
long long A[200010];
long long T[200010];

bool f(long long X) {
  for (int i = 0; i < N; i++) {
    int j = i;
    long long d = 0;

    auto it = lower_bound(T + j, T + i + N, X + (j > 0 ? T[j - 1] : 0));
    if (it == T + i + N)
      continue;
    j = it - T;
    j++;

    it = lower_bound(T + j, T + i + N, X + (j > 0 ? T[j - 1] : 0));
    if (it == T + i + N)
      continue;
    j = it - T;
    j++;

    if (T[i + N - 1] - T[j - 1] >= X)
      return true;
  }
  return false;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[i] = A[i + N] = a;
  }
  long long t = 0;
  for (int i = 0; i < N + N; i++) {
    t += A[i];
    T[i] = t;
  }

  long long lo = 0, hi = 1000000000000000000L;
  long long m;
  while (hi - lo > 1) {
    m = (lo + hi) / 2;
    if (f(m))
      lo = m;
    else
      hi = m;
  }
  if (f(hi))
    m = hi;
  else
    m = lo;

  cout << m << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int N;
long long A[200010];
long long T[200010];

bool f(long long X) {
  for (int i = 0; i < N; i++) {
    int j = i;
    long long d = 0;
    bool ok = false;
    for (; j < i + N; j++) {
      d += A[j];
      if (d >= X) {
        ok = true;
        break;
      }
    }
    if (!ok)
      continue;
    j++;

    d = 0;
    for (; j < i + N; j++) {
      d += A[j];
      if (d >= X) {
        ok = true;
        break;
      }
    }
    if (!ok)
      continue;
    j++;

    if (T[i + N - 1] - T[j - 1] >= X) {
      return true;
    }
  }
  return false;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[i] = A[i + N] = a;
  }
  long long t = 0;
  for (int i = 0; i < N + N; i++) {
    t += A[i];
    T[i] = t;
  }

  long long lo = 0, hi = 1000000000000000000L;
  long long m;
  while (hi - lo > 1) {
    m = (lo + hi) / 2;
    if (f(m))
      lo = m;
    else
      hi = m;
  }
  if (f(hi))
    m = hi;
  else
    m = lo;

  cout << m << "\\n";
  return 0;
}
Problem p00523 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define INF (1e+15)
using namespace std;

typedef long long ll;

int main() {

  int n, a[100001];
  ll ssum[100001 * 2] = {0};

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ssum[0] = a[0];
  for (int i = 1; i < n * 2; i++)
    ssum[i] = ssum[i - 1] + a[i % n];

  ll al = 0, ar = INF, amid;
  int c2;
  while (al < ar) {
    amid = ((al + 1) + ar) / 2; // 解を二分探索
    bool judge = false;
    for (int i = 0; i < n; i++) {
      int l = i, r = n + i; // 添字を二分探索
      while (l < r) {
        int mid = (l + r) / 2;
        ll sub = ssum[mid] - ssum[i];
        if (sub < amid)
          l = mid + 1;
        else
          r = mid;
      }
      c2 = l++;
      r = n + i;
      while (l < r) {
        int mid = (l + r) / 2;
        ll sub = ssum[mid] - ssum[c2];
        if (sub < amid)
          l = mid + 1;
        else
          r = mid;
      }

      if (ssum[i + n] - ssum[r] >= amid) {
        judge = true;
        break;
      }
    }
    if (judge)
      al = amid;
    else
      ar = amid - 1;
  }
  cout << al << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF (1e+15)
using namespace std;

typedef long long ll;

int main() {

  int n, a[100001];
  ll ssum[100001 * 2] = {0};

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ssum[0] = a[0];
  for (int i = 1; i < n * 2; i++)
    ssum[i] = ssum[i - 1] + a[i % n];

  ll al = 0, ar = INF, amid;
  int c2;
  while (al < ar) {
    amid = (al + ar) / 2; // 解を二分探索
    bool judge = false;
    for (int i = 0; i < n; i++) {
      int l = i, r = n + i; // 添字を二分探索
      while (l < r) {
        int mid = (l + r) / 2;
        ll sub = ssum[mid] - ssum[i];
        if (sub < amid)
          l = mid + 1;
        else
          r = mid;
      }
      c2 = l++;
      r = n + i;
      while (l < r) {
        int mid = (l + r) / 2;
        ll sub = ssum[mid] - ssum[c2];
        if (sub < amid)
          l = mid + 1;
        else
          r = mid;
      }

      if (ssum[i + n] - ssum[r] >= amid) {
        judge = true;
        break;
      }
    }
    if (judge)
      al = amid;
    else
      ar = amid - 1;
  }
  cout << al << endl;
}