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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define BOX 100000000000000000LL
const int WID = 100;
long long n, k, l;
unsigned long long p, q;
vector<long long> vec;
long long sum[WID * 10];
int main() {
  cin >> n >> k >> p;
  q = p;
  if (p == 0) {
    cout << "0" << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    long long r = p;
    long long L = r / BOX;
    if (r < 0LL)
      L--;
    sum[L + WID]++;
    p ^= p << 13;
    p ^= p >> 7;
    p ^= p << 17;
  }
  for (int i = 1; i <= WID * 2; i++)
    sum[i] += sum[i - 1];
  for (int i = 0; i < WID * 2; i++) {
    if (sum[i] >= k) {
      l = i - WID;
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    long long K = (long long)q / BOX;
    if ((long long)q < 0)
      K--;
    if (l == K)
      vec.push_back((long long)q);
    q ^= q << 13;
    q ^= q >> 7;
    q ^= q << 17;
  }
  sort(vec.begin(), vec.end());
  cout << vec[k - sum[l + WID - 1] - 1] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define BOX 10000000000000000LL
#define WID 2000
long long n, k, l;
unsigned long long p, q;
map<long long, long long> sum;
vector<long long> vec;
long long sum2[WID * 10];
int main() {
  cin >> n >> k >> p;
  q = p;
  k--;
  for (int i = 0; i < n; i++) {
    sum2[WID + (long long)p / BOX]++;
    p ^= p << 13;
    p ^= p >> 7;
    p ^= p << 17;
  }
  for (int i = 0; i <= WID * 2; i++) {
    sum[i - WID] = sum2[i];
  }
  for (int i = -WID; i <= WID; i++)
    sum[i] += sum[i - 1];
  for (int i = WID; i >= -WID; i--) {
    if (sum[i] <= k) {
      l = i;
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    long long K = (long long)q / BOX;
    if ((long long)q < 0)
      K--;
    if (l == K)
      vec.push_back((long long)q);
    q ^= q << 13;
    q ^= q >> 7;
    q ^= q << 17;
  }
  sort(vec.begin(), vec.end());
  cout << vec[k - sum[l]] << endl;
  return 0;
}
Problem p01639 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define whole(xs) xs.begin(), xs.end()

#define MAX_R 101000

namespace {

typedef double real;
typedef long long ll;
typedef unsigned long long ull;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  if (vs.empty())
    return os << "[]";
  os << "[" << vs[0];
  for (int i = 1; i < vs.size(); i++)
    os << " " << vs[i];
  return os << "]";
}
template <class T> istream &operator>>(istream &is, vector<T> &vs) {
  for (auto it = vs.begin(); it != vs.end(); it++)
    is >> *it;
  return is;
}

ull x;
inline ll xorshift() {
  ll r = x;
  x ^= x << 13;
  x ^= x >> 7;
  x ^= x << 17;
  return r;
}

int N, K, X0;
void input() {
  cin >> N >> K >> X0;
  K--;
}

int _count[MAX_R * 2 + 1000];
void solve() {

  if (X0 == 0) {
    cout << 0 << endl;
    return;
  }

  auto count = _count + MAX_R + 500;
  x = X0;
  for (int i = 0; i < N; i++) {
    count[int(floor(xorshift() / 1e14))]++;
  }
  ll s = 0;
  real z = -1;
  for (int i = -MAX_R - 1; i <= MAX_R + 1; i++) {
    s += count[i];
    if (s > K) {
      z = i;
      s -= count[i];
      break;
    }
  }

  x = X0;
  vector<ll> ts;
  for (int i = 0; i < N; i++) {
    ll t = xorshift();
    int r = floor(real(t) / 1e14);
    if (r == z) {
      ts.push_back(t);
    }
  }
  sort(whole(ts));
  cout << ts[K - s] << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define whole(xs) xs.begin(), xs.end()

#define MAX_R 101000

namespace {

typedef double real;
typedef long long ll;
typedef unsigned long long ull;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  if (vs.empty())
    return os << "[]";
  os << "[" << vs[0];
  for (int i = 1; i < vs.size(); i++)
    os << " " << vs[i];
  return os << "]";
}
template <class T> istream &operator>>(istream &is, vector<T> &vs) {
  for (auto it = vs.begin(); it != vs.end(); it++)
    is >> *it;
  return is;
}

ull x;
inline ll xorshift() {
  ll r = x;
  x ^= x << 13;
  x ^= x >> 7;
  x ^= x << 17;
  return r;
}

int N, K, X0;
void input() {
  cin >> N >> K >> X0;
  K--;
}

int _count[MAX_R * 2 + 1000];
void solve() {
  auto count = _count + MAX_R + 500;
  x = X0;
  for (int i = 0; i < N; i++) {
    ll t = xorshift();
    int r = floor(real(t) / 1e14);
    count[r]++;
  }
  ll s = 0;
  real z = -1;
  for (int i = -MAX_R - 1; i <= MAX_R + 1; i++) {
    s += count[i];
    if (s > K) {
      z = i;
      s -= count[i];
      break;
    }
  }

  x = X0;
  vector<ll> ts;
  for (int i = 0; i < N; i++) {
    ll t = xorshift();
    int r = floor(real(t) / 1e14);
    if (r == z) {
      ts.push_back(t);
    }
  }
  sort(whole(ts));
  cout << ts[K - s] << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}
Problem p01639 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
const long long int SP = 8192;
int main() {
  long long int N, K;
  long long int X0;
  cin >> N >> K >> X0;
  if (!X0)
    cout << 0 << endl;
  else {
    unsigned long long int x = X0;
    if (N < 1e6) {

      vector<long long int> nums(N);
      for (int i = 0; i < N; ++i) {
        long long int ax = static_cast<long long int>(x);
        nums[i] = ax;
        x ^= x << 13;
        x ^= x >> 7;
        x ^= x << 17;
      }
      sort(nums.begin(), nums.end());
      cout << nums[K - 1] << endl;
    } else {
      int num = static_cast<int>(K * SP / N);
      int minnum = max(0, num - 32);
      int maxnum = min(static_cast<int>(SP), num + 32);
      vector<long long int> nums;
      int lower = 0;
      for (int i = 0; i < N; ++i) {
        long long int ax = static_cast<long long int>(x);
        long long int d = -(LLONG_MAX) + (ULLONG_MAX / SP * minnum);
        long long int u = -(LLONG_MAX) + (ULLONG_MAX / SP * maxnum);
        if (maxnum == SP)
          u = LLONG_MAX;
        if (minnum == 0)
          d = -LLONG_MAX;
        if (ax < d) {
          lower++;
        } else if (ax <= u) {
          nums.push_back(ax);
        }

        x ^= x << 13;
        x ^= x >> 7;
        x ^= x << 17;
      }

      sort(nums.begin(), nums.end());
      if (0 > K - 1 - lower || K - 1 - lower >= nums.size())
        cout << 0 << endl;
      else {

        cout << nums[K - 1 - lower] << endl;
      }
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
const long long int SP = 8192;
int main() {
  long long int N, K;
  long long int X0;
  cin >> N >> K >> X0;
  unsigned long long int x = X0;
  if (N < 1e6) {

    vector<long long int> nums(N);
    for (int i = 0; i < N; ++i) {
      long long int ax = static_cast<long long int>(x);
      nums[i] = ax;
      x ^= x << 13;
      x ^= x >> 7;
      x ^= x << 17;
    }
    sort(nums.begin(), nums.end());
    cout << nums[K - 1] << endl;
  } else {
    int num = static_cast<int>(K * SP / N);
    int minnum = max(0, num - 32);
    int maxnum = min(static_cast<int>(SP), num + 32);
    vector<long long int> nums;
    int lower = 0;
    for (int i = 0; i < N; ++i) {
      long long int ax = static_cast<long long int>(x);
      long long int d = -(LLONG_MAX) + (ULLONG_MAX / SP * minnum);
      long long int u = -(LLONG_MAX) + (ULLONG_MAX / SP * maxnum);
      if (maxnum == SP)
        u = LLONG_MAX;
      if (minnum == 0)
        d = -LLONG_MAX;
      if (ax < d) {
        lower++;
      } else if (ax <= u) {
        nums.push_back(ax);
      }

      x ^= x << 13;
      x ^= x >> 7;
      x ^= x << 17;
    }

    sort(nums.begin(), nums.end());
    if (0 > K - 1 - lower || K - 1 - lower >= nums.size())
      cout << 0 << endl;
    else {

      cout << nums[K - 1 - lower] << endl;
    }
  }
  return 0;
}
Problem p01639 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int cnt[200000];
vector<long long> v;
int main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);
  if (c == 0) {
    printf("0\\n");
    return 0;
  }
  b--;
  unsigned long long x = c;
  for (int i = 0; i < a; i++) {
    cnt[((long long)x) / (1LL << 47) + 100000]++;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }
  int at = 0;
  for (int i = 0; i < 200000; i++) {
    if (b < cnt[i]) {
      at = i;
      break;
    } else
      b -= cnt[i];
  }
  // printf("%d\\n",at);
  x = c;
  for (int i = 0; i < a; i++) {
    if (((long long)x) / (1LL << 47) + 100000 == at) {
      v.push_back((long long)x);
    }
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }
  std::sort(v.begin(), v.end());
  printf("%lld\\n", v[b]);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int cnt[200000];
vector<long long> v;
int main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);
  b--;
  unsigned long long x = c;
  for (int i = 0; i < a; i++) {
    cnt[((long long)x) / (1LL << 47) + 100000]++;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }
  int at = 0;
  for (int i = 0; i < 200000; i++) {
    if (b < cnt[i]) {
      at = i;
      break;
    } else
      b -= cnt[i];
  }
  // printf("%d\\n",at);
  x = c;
  for (int i = 0; i < a; i++) {
    if (((long long)x) / (1LL << 47) + 100000 == at) {
      v.push_back((long long)x);
    }
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }
  std::sort(v.begin(), v.end());
  printf("%lld\\n", v[b]);
}
Problem p01642 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
using namespace std;
typedef unsigned long long wolf;
int mod = 1000000009;
int c[110][110];
wolf s[2600][42];
wolf u = 1;
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  for (int i = 0; i < a; i++)
    for (int j = 0; j < b; j++) {
      scanf("%d", &c[i][j]);
      s[i * b + j][40] = c[i][j];
    }
  for (int i = 0; i < a; i++)
    for (int j = 0; j < b; j++) {
      for (int k = 0; k < a; k++)
        for (int l = 0; l < b; l++) {
          if (i == k || j == l || (i + j) == (k + l) || (i - j) == (k - l)) {
            int L = i * b + j;
            int R = k * b + l;
            s[L][R / 64] ^= (u << (R % 64));
          }
        }
    }
  int rank = 0;
  for (int i = 0; i < a * b; i++) {
    int at = -1;
    for (int j = rank; j < a * b; j++) {
      if (s[j][i / 64] & (u << (i % 64))) {
        at = j;
        break;
      }
    }
    if (!~at)
      continue;
    for (int j = 0; j < 42; j++)
      swap(s[at][j], s[rank][j]);
    for (int j = rank + 1; j < a * b; j++) {
      if (s[j][i / 64] & (u << (i % 64))) {
        for (int k = 0; k < 42; k++)
          s[j][k] ^= s[rank][k];
      }
    }
    rank++;
  }

  bool ok = true;
  for (int i = 0; i < a * b; i++) {
    bool OK = false;
    for (int j = 0; j < 40; j++)
      if (s[i][j])
        OK = true;
    if (!OK && s[i][40])
      ok = false;
  }
  if (!ok) {
    printf("0\\n");
    return 0;
  }
  int ret = 1;
  for (int i = 0; i < a * b - rank; i++) {
    ret = ret * 2 % mod;
  }
  printf("%d\\n", ret);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
typedef unsigned long long wolf;
int mod = 1000000009;
int c[110][110];
wolf s[2600][42];
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  for (int i = 0; i < a; i++)
    for (int j = 0; j < b; j++) {
      scanf("%d", &c[i][j]);
      s[i * b + j][40] = c[i][j];
    }
  for (int i = 0; i < a; i++)
    for (int j = 0; j < b; j++) {
      for (int k = 0; k < a; k++)
        for (int l = 0; l < b; l++) {
          if (i == k || j == l || (i + j) == (k + l) || (i - j) == (k - l)) {
            int L = i * b + j;
            int R = k * b + l;
            s[L][R / 64] ^= (1LL << (R % 64));
          }
        }
    }
  int rank = 0;
  for (int i = 0; i < a * b; i++) {
    int at = -1;
    for (int j = rank; j < a * b; j++) {
      if (s[j][i / 64] & (1LL << (i % 64))) {
        at = j;
        break;
      }
    }
    if (!~at)
      continue;
    for (int j = 0; j < 42; j++)
      swap(s[at][j], s[rank][j]);
    for (int j = rank + 1; j < a * b; j++) {
      if (s[j][i / 64] & (1LL << (i % 64))) {
        for (int k = 0; k < 42; k++)
          s[j][k] ^= s[rank][k];
      }
    }
    rank++;
  }

  bool ok = true;
  for (int i = 0; i < a * b; i++) {
    bool OK = false;
    for (int j = 0; j < 40; j++)
      if (s[i][j])
        OK = true;
    if (!OK && s[i][40])
      ok = false;
  }
  if (!ok) {
    printf("0\\n");
    return 0;
  }
  int ret = 1;
  for (int i = 0; i < a * b - rank; i++) {
    ret = ret * 2 % mod;
  }
  printf("%d\\n", ret);
}
Problem p01643 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define INF 1000000
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
int n, k;
int a[8001];
bool used[8001];
int R[4001];
int L[4001];

#define MAX_N 8010
int dp[MAX_N];
int solve(int x) {
  fill(dp, dp + n + 1, INF);
  for (int i = L[x] + 1; i < R[x]; i++) {
    int b = a[i];
    if (L[x] > b)
      b += n;
    if (R[x] < b) {
      *lower_bound(dp, dp + n, b) = b;
    }
  }
  return (lower_bound(dp, dp + n, INF) - dp);
}
/*
int solve(int x){
    fill(dp,dp+n,INF);
    for(int i=R[x]+1;i<L[x]+n;i++){
        int b=a[i%n];
        if(b<R[x]&&b>L[x]){
           *lower_bound(dp,dp+n,b)=b;
        }
    }
    return (lower_bound(dp,dp+n,INF)-dp);
}*/

int main() {
  cin >> n >> k;
  REP(i, 8001) { used[i] = false; }
  REP(i, n) {
    cin >> a[i];
    a[i]--;
  }

  int c = 0;
  REP(i, n) {
    if (used[i] == true)
      continue;
    used[i] = true;
    used[a[i]] = true;
    L[c] = i;
    R[c] = a[i];
    c++;
  }

  int ans = 0;
  REP(i, n / 2) {
    //????????§???????¢????????????????
    ans = max(ans, solve(i));
  }
  ans = min(n / 2, ans + k + 1);
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
int n, k;
int a[8001];
bool used[8001];
int R[4001];
int L[4001];

#define MAX_N 8001
int dp[MAX_N];
int solve(int x) {
  fill(dp, dp + n, INF);
  for (int i = L[x] + 1; i <= R[x]; i++) {
    if (i == x)
      continue;
    int b = a[i];
    if (L[x] > b)
      b += n;
    if (R[x] < b) {
      *lower_bound(dp, dp + n, b) = b;
    }
  }
  /* REP(i,5){
       cout<<dp[i]<<endl;
   }*/
  return (lower_bound(dp, dp + n, INF) - dp);
}

int main() {
  cin >> n >> k;
  REP(i, 8001) { used[i] = false; }
  REP(i, n) {
    cin >> a[i];
    a[i]--;
  }

  REP(i, n) {
    if (used[i] == true)
      continue;
    used[i] = true;
    used[a[i]] = true;
    L[i] = i;
    R[i] = a[i];
  }

  int ans = 0;
  REP(i, n) {
    //????????§???R[i],L[i]?????????
    // int LL=L[i];
    // int RR=R[i];
    //????????§???????¢????????????????
    ans = max(ans, solve(i));
    // cout<<ans;
  }
  ans = min(n / 2, ans + k + 1);
  cout << ans << endl;
}
Problem p01643 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int longestIncreasingSubsequence(const vector<int> &a) {
  int n = a.size();
  vector<int> dp(n, INT_MAX);
  for (int i = 0; i < n; ++i) {
    vector<int>::iterator it = upper_bound(dp.begin(), dp.end(), a[i]);
    *it = a[i];
  }
  return lower_bound(dp.begin(), dp.end(), INT_MAX) - dp.begin();
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }

  int ret = 0;
  for (int i = 0; i < n; ++i) {
    if (i < a[i])
      continue;

    vector<int> b(n);
    for (int j = 0; j < n; ++j)
      b[(j + i) % n] = (a[j] + i) % n;

    vector<int> v;
    for (int j = 0; j < b[0]; ++j) {
      if (b[j] > b[0])
        v.push_back(b[j]);
    }
    ret = max(ret, longestIncreasingSubsequence(v) + 1);
  }

  ret = min(n / 2, ret + k);
  cout << ret << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

class RangeMaximumQuery {
  int n;
  vector<int> data;
  int query(int a, int b, int k, int l, int r) {
    if (r < a || b < l)
      return 0;
    if (a <= l && r <= b)
      return data[k];
    int x = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int y = query(a, b, k * 2 + 2, (l + r + 1) / 2, r);
    return max(x, y);
  }

public:
  RangeMaximumQuery(int n0) { // コンストラクタ（要素数を指定）
    n = 1;
    while (n < n0)
      n *= 2;
    data.assign(2 * n - 1, 0);
  }
  void update(int k, int x) { // k番目の要素をxに変更する
    k += n - 1;
    data[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = max(data[k * 2 + 1], data[k * 2 + 2]);
    }
  }
  int query(int a, int b) { // 区間[a,b]の最大値を返す
    return query(a, b, 0, 0, n - 1);
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }

  int ret = 0;
  for (int i = 0; i < n; ++i) {
    if (i < a[i])
      continue;

    vector<int> b(n);
    for (int j = 0; j < n; ++j)
      b[(j + i) % n] = (a[j] + i) % n;

    RangeMaximumQuery rmq(n);
    rmq.update(b[0], 1);
    for (int j = 1; j < b[0]; ++j) {
      if (b[j] > b[0])
        rmq.update(b[j], rmq.query(b[0], b[j] - 1) + 1);
    }
    ret = max(ret, rmq.query(0, n - 1));
  }

  ret = min(n / 2, ret + k);
  cout << ret << endl;

  return 0;
}
Problem p01643 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
const int inf = 999999999;
int seg[16399];
void update(int i, int x) {
  i += 8192;
  seg[i] = x;
  while (i > 1) {
    i >>= 1;
    seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
  }
}
int query(int a, int b, int k, int l, int r) {
  if (a <= l && r <= b)
    return seg[k];
  if (r <= a || b <= l)
    return -inf;
  int lc = query(a, b, k << 1, l, (l + r) >> 1);
  int rc = query(a, b, k << 1 | 1, (l + r) >> 1, r);
  return max(lc, rc);
}
int n, m, a[8009], inv[8009];
bool ok[8009];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i], a[i]--, inv[a[i]] = i;
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (i > a[i])
      continue;
    fill(seg + 1, seg + 16384, -inf);
    update(i, 1);
    for (int j = inv[i] + 1; j < n; j++) {
      if (inv[j] <= i || inv[j] > a[i])
        continue;
      update(inv[j], query(i, inv[j], 1, 0, 8192) + 1);
    }
    ret = max(ret, seg[1]);
  }
  cout << min(ret + m, n / 2) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n, m, a[8009], inv[8009], dp[8009];
bool ok[8009];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i], a[i]--, inv[a[i]] = i;
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (i > a[i])
      continue;
    fill(dp + i, dp + a[i], -999999999);
    dp[i] = 1;
    for (int j = inv[i] + 1; j < n; j++) {
      if (inv[j] <= i || inv[j] > a[i])
        continue;
      for (int k = i; k < inv[j]; k++) {
        dp[inv[j]] = max(dp[inv[j]], dp[k] + 1);
      }
    }
    ret = max(ret, *max_element(dp + i, dp + a[i]));
  }
  cout << min(ret + m, n / 2) << endl;
  return 0;
}
Problem p01644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
const double lim = 0.0001;
int n;
double p[309], sq[309], a[309];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i], sq[i] = sqrt(p[i]), a[i] = 0.03 / n;
  double ret = 1.0e+9;
  int cnt = 0;
  while (true) {
    double curval = 0.0;
    for (int i = 0; i < n; i++)
      curval += p[i] / a[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        double sa = a[i] + a[j], sp = sq[i] + sq[j];
        a[i] = sq[i] / sp * sa;
        a[j] = sq[j] / sp * sa;
        if (a[i] < lim)
          a[j] -= lim - a[i], a[i] = lim;
        if (a[j] < lim)
          a[i] -= lim - a[j], a[j] = lim;
      }
    }
    ret = 0.0;
    for (int i = 0; i < n; i++)
      ret += p[i] / a[i];
    if (abs(curval - ret) < 1.0e-8)
      break;
    cnt++;
  }
  cout << fixed << setprecision(15) << 300 * ret << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
const double lim = 0.0001;
int n;
double p[309], a[309];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i], a[i] = 0.03 / n;
  double ret = 1.0e+9;
  int cnt = 0;
  while (true) {
    double curval = 0.0;
    for (int i = 0; i < n; i++)
      curval += p[i] / a[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        double sa = a[i] + a[j], l = lim, r = sa - lim;
        for (int k = 0; k < 45; k++) {
          double m = (l + r) * 0.5;
          if (p[i] * pow(m, -2.0) > p[j] * pow(sa - m, -2.0))
            l = m;
          else
            r = m;
        }
        a[i] = l, a[j] = sa - l;
      }
    }
    ret = 0.0;
    for (int i = 0; i < n; i++)
      ret += p[i] / a[i];
    if (abs(curval - ret) < 1.0e-8)
      break;
    cnt++;
  }
  cout << fixed << setprecision(15) << 300 * ret << endl;
  return 0;
}