alexjercan/bugnet huggingface dataset:
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long int i64;
typedef pair<int, int> pint;
string s;
bool dp[501][501];
bool used[501][501];
bool dfs(int l, int r) {
  if (l > r)
    return 0;
  if (used[l][r])
    return dp[l][r];
  used[l][r] = true;
  if (l == r)
    return dp[l][r] = true;
  if (s[l] != 'm' || s[r - 1] != 'w')
    return false;
  REP(i, l + 1, r - 1) {
    if (s[i] == 'e') {
      bool a = dfs(l + 1, i);
      bool b = dfs(i + 1, r - 1);
      if (a && b) {
        dp[l][r] = true;
        break;
      }
    }
  }
  return dp[l][r];
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  memset(dp, 0, sizeof(dp));
  memset(used, 0, sizeof(used));
  cin >> s;
  if (dfs(0, s.length()))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long int i64;
typedef pair<int, int> pint;
string parts(const string &s, int l, int r) {
  string res = "";
  REP(i, l, r) res += s[i];
  return res;
}
bool dfs(string s) {
  if (s == "")
    return true;
  if (s.front() != 'm' && s.back() != 'm')
    return false;
  int n = s.length();
  REP(i, 1, n - 1) {
    if (s[i] == 'e') {
      bool a = dfs(parts(s, 1, i));
      bool b = dfs(parts(s, i + 1, n - 1));
      if (a && b)
        return true;
    }
  }
  return false;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  if (dfs(s))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
  return 0;
}
Problem p01486 (C++) — buggy submission, verdict: Runtime Error

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

string S;
int dp[510][510];

bool rec(int l, int r) {
  if (dp[l][r] != -1)
    return dp[l][r];
  dp[l][r] = 0;
  if (l > r)
    return dp[l][r] = 1;
  if (S[l] != 'm' || S[r] != 'w')
    return dp[l][r] = 0;
  for (int i = l + 1; i < r; i++) {
    if (S[i] == 'e')
      dp[l][r] = (dp[l][r] | (rec(l + 1, i - 1) & rec(i + 1, r - 1)));
  }
  return dp[l][r];
}

int main() {
  cin >> S;
  int N = S.size();
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      dp[i][j] = -1;
  cout << (rec(0, N - 1) ? "Cat" : "Rabbit") << endl;
  /*for(int i=0;i<N;i++){
      for(int j=i;j<N;j++) cerr << dp[i][j] << " ";
      cerr << endl;
  }*/
}


Error: 0
0 11
1 1
3 10
4 3
5 9
4 6
8 9
4 8
10 9
1 3
5 10
1 6
2 1
3 5
4 3
5 4
2 3
5 5
8 10
9 8
10 9
1 8
10 10


Fixed submission:

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

string S;
int dp[510][510];

bool rec(int l, int r) {
  cerr << l << " " << r << endl;
  if (dp[l][r] != -1)
    return dp[l][r];
  dp[l][r] = 0;
  if (l > r)
    return dp[l][r] = 1;
  if (S[l] != 'm' || S[r] != 'w')
    return dp[l][r] = 0;
  for (int i = l + 1; i < r; i++) {
    if (S[i] == 'e')
      dp[l][r] = (dp[l][r] | (rec(l + 1, i - 1) & rec(i + 1, r - 1)));
  }
  return dp[l][r];
}

int main() {
  cin >> S;
  int N = S.size();
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      dp[i][j] = -1;
  cout << (rec(0, N - 1) ? "Cat" : "Rabbit") << endl;
  /*for(int i=0;i<N;i++){
      for(int j=i;j<N;j++) cerr << dp[i][j] << " ";
      cerr << endl;
  }*/
}
Problem p01486 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string s;
  cin >> s;
  stack<char> sc;
  bool neko = true;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'm' || s[i] == 'e' || s[i] == 'w') {
      sc.push(s[i]);
      if (sc.top() == 'w') {
        sc.pop();
        if (!sc.empty() && sc.top() == 'e') {
          sc.pop();
          if (!sc.empty() && sc.top() == 'm') {
            sc.pop();
          } else {
            sc.push('e');
            sc.push('w');
          }
        } else {
          sc.push('w');
        }
      }
    } else {
      neko = false;
      break;
    }
    if (sc.empty() && i != s.length() - 1) {
      neko = false;
      break;
    }
  }
  if (neko && sc.empty())
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
}

Error: 0

Fixed submission:

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

int main() {
  string s;
  cin >> s;
  stack<char> sc;
  bool neko = true;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'm' || s[i] == 'e' || s[i] == 'w') {
      sc.push(s[i]);
      if (sc.top() == 'w') {
        if (!sc.empty())
          sc.pop();
        else
          continue;
        if (sc.top() == 'e') {
          if (!sc.empty())
            sc.pop();
          else
            continue;
          if (sc.top() == 'm') {

          } else {
            sc.push('e');

            sc.push('w');
          }
        } else {
          sc.push('w');
        }
      }
    } else {
      neko = false;
      break;
    }
    if (sc.empty() && i != s.length() - 1) {
      neko = false;
      break;
    }
  }
  if (neko)
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
}
Problem p01487 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define N 100010
#define int long long
using namespace std;
typedef pair<int, int> P;
int n, m;
vector<int> G[N];
int flag;
int col[N];

P cnt;
void dfs(int pos, int c) {
  if (col[pos] != -1 && c != col[pos])
    flag = 1;
  if (col[pos] != -1 && c == col[pos])
    return;
  if (flag)
    return;
  c ? cnt.first++ : cnt.second++;
  col[pos] = c;
  for (int nx : G[pos])
    dfs(nx, !c);
}

map<int, int> A;

int calc(int d) {
  int a = (n - d) / 2;
  int b = n - a;
  return a * b;
}

int dp[N] = {};
int DP() {
  int sum = 0;
  for (P a : A)
    sum += a.first * a.second;

  for (P a : A) {
    dp[0] = 1;
    for (int j = 0; j < N; j++)
      if (dp[j])
        dp[j] = a.second + 1;
    for (int j = 0; j < N; j++) {
      if (j + a.first >= N)
        break;
      if (dp[j] == 0)
        continue;
      dp[j + a.first] = max(dp[j + a.first], dp[j] - 1);
    }
  }

  int res = 0;
  for (int i = 0; i < N; i++)
    if (dp[i] > 0)
      res = max(res, calc(abs(i - (sum - i))));
  return res;
}

signed main() {
  cin >> n >> m;
  for (int i = 0, a, b; i < m; i++) {
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  memset(col, -1, sizeof(col));
  for (int i = 0; i < n; i++) {
    if (col[i] != -1)
      continue;
    cnt = P(0, 0);
    dfs(i, 0);
    A[abs(cnt.first - cnt.second)]++;
  }

  if (flag)
    cout << "-1" << endl, exit(0);
  cout << DP() - m << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 100010
#define int long long
using namespace std;
typedef pair<int, int> P;
int n;
vector<int> G[N];
int flag;
int col[N];

P cnt;
void dfs(int pos, int c) {
  if (col[pos] != -1 && c != col[pos])
    flag = 1;
  if (col[pos] != -1 && c == col[pos])
    return;
  if (flag)
    return;
  c ? cnt.first++ : cnt.second++;
  col[pos] = c;
  for (int nx : G[pos])
    dfs(nx, !c);
}

vector<P> A;
int ans;
P Sum[N];

void dfs(int idx, int f, int s) {
  if (idx == (int)A.size())
    ans = max(ans, f * s);
  if (idx == (int)A.size())
    return;
  if (f > s)
    swap(f, s);

  int F = f + Sum[idx].first;
  int S = s + Sum[idx].second;
  ans = max(ans, F * S);
  if (F < S)
    return;

  int a = A[idx].first;
  int b = A[idx].second;
  dfs(idx + 1, f + a, s + b);
  dfs(idx + 1, f + b, s + a);
}

signed main() {
  int m;
  cin >> n >> m;
  for (int i = 0, a, b; i < m; i++) {
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  memset(col, -1, sizeof(col));
  for (int i = 0; i < n; i++) {
    if (col[i] != -1)
      continue;
    cnt = P(0, 0);
    dfs(i, 0);
    if (cnt.first < cnt.second)
      swap(cnt.first, cnt.second);
    A.push_back(cnt);
  }
  if (flag)
    cout << "-1" << endl, exit(0);

  sort(A.begin(), A.end(), greater<P>());
  for (int i = A.size() - 1; i >= 0; i--)
    Sum[i] = P(Sum[i + 1].first + A[i].first, Sum[i + 1].second + A[i].second);

  dfs(0, 0, 0);
  cout << ans - m << endl;
  ;
  return 0;
}
Problem p01487 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

vector<int> G[100000];
bool flag = false;
int a = 0, b = 0;
int color[100000];

void dfs(int n, int c) {
  color[n] = c;
  if (c - 1) {
    b++;
  } else {
    a++;
  }
  for (auto v : G[n]) {
    if (color[v] == 0) {
      dfs(v, 3 - c);
    } else if (color[v] == c) {
      flag = true;
    }
  }
}

int main() {
  cin.sync_with_stdio(false);
  ll V, E;
  cin >> V >> E;
  for (int i = 0, a, b; i < E; i++) {
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }
  ll x = 0;
  vector<ll> d;
  for (int i = 0; i < V; i++) {
    if (color[i] == 0) {
      dfs(i, 1);
    }
    if (a != b) {
      d.push_back(abs(a - b) * 2);
      x += abs(a - b);
    }
    a = b = 0;
  }
  sort(d.begin(), d.end());
  ll ma = 0;
  vector<int> dp(x * 2 + 1);
  dp[0] = 1;
  for (auto bi : d) {
    for (int i = min(x * 2 - bi, ma); i >= 0; i--) {
      if (dp[i]) {
        dp[i + bi] = 1;
        ma = max(ma, i + bi);
      }
    }
  }
  ll res = 0;
  while (dp[x - res] == 0 && dp[x + res] == 0)
    res++;
  cout << (flag ? -1ll : (V + res) * (V - res) / 4 - E) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

vector<int> G[100000];
bool flag = false;
int a = 0, b = 0;
int color[100000];

void dfs(int n, int c) {
  color[n] = c;
  if (c - 1) {
    b++;
  } else {
    a++;
  }
  for (auto v : G[n]) {
    if (color[v] == 0) {
      dfs(v, 3 - c);
    } else if (color[v] == c) {
      flag = true;
    }
  }
}

int main() {
  ll V, E;
  cin >> V >> E;
  for (int i = 0, a, b; i < E; i++) {
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }
  ll x = 0;
  vector<ll> d;
  for (int i = 0; i < V; i++) {
    if (color[i] == 0) {
      dfs(i, 1);
    }
    if (a != b) {
      d.push_back(abs(a - b) * 2);
      x += abs(a - b);
    }
    a = b = 0;
  }
  vector<int> dp(x * 2 + 1);
  dp[0] = 1;
  for (auto bi : d) {
    for (int i = x * 2 - bi; i >= 0; i--) {
      if (dp[i]) {
        dp[i + bi] = 1;
      }
    }
  }
  ll res = 0;
  while (dp[x - res] == 0 && dp[x + res] == 0)
    res++;
  cout << (flag ? -1ll : (V + res) * (V - res) / 4 - E) << endl;
  return 0;
}
Problem p01487 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX_V 111111
#define int long long
int V, E;
vector<int> G[MAX_V];
int used[MAX_V];
void add_edge(int a, int b) {
  G[a].push_back(b);
  G[b].push_back(a);
}
int a[2];
bool dfs(int v, int c) {
  if (~used[v])
    return used[v] == c;
  bool f = 1;
  a[c]++;
  used[v] = c;
  for (int i = 0; i < (int)G[v].size(); i++)
    f &= dfs(G[v][i], !c);
  return f;
}
typedef pair<int, int> P;
bool dp[2][MAX_V];
int solve() {
  int x = 0;
  vector<P> v;
  memset(used, -1, sizeof(used));
  for (int i = 0; i < V; i++) {
    if (used[i] >= 0)
      continue;
    a[0] = a[1] = 0;
    if (dfs(i, 0)) {
      if (a[1])
        v.push_back(P(a[1], a[0]));
      else
        x++;
    } else
      return -1;
  }
  int k = 0, m = 0;
  memset(dp[0], 0, sizeof(dp[0]));
  dp[0][0] = 1;
  for (int i = 0; i < (int)v.size(); i++) {
    bool f = i % 2;
    memset(dp[!f], 0, sizeof(dp[!f]));
    for (int j = 0; j <= m; j++) {
      if (dp[f][j])
        dp[!f][j + v[i].first] = dp[!f][j + v[i].second] = 1,
                   m = max(m, j + max(v[i].first, v[i].second));
    }
  }
  for (int i = 0; i < V; i++) {
    if (dp[v.size() % 2][i]) {
      for (int j = 0; j <= x && i + j < V; j++) {
        // cout<<i+j<<":"<<(V-(i+j))*(i+j)<<endl;
        if ((V - k) * k < (V - (i + j)) * (i + j))
          k = i + j;
      }
    }
  }
  return (V - k) * k - E;
}
signed main() {
  cin >> V >> E;
  for (int i = 0, a, b; i < E; i++)
    cin >> a >> b, add_edge(a - 1, b - 1);
  cout << solve() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX_V 111111
int V, E;
vector<int> G[MAX_V];
int used[MAX_V];
void add_edge(int a, int b) {
  G[a].push_back(b);
  G[b].push_back(a);
}
int a[2];
bool dfs(int v, int c) {
  if (~used[v])
    return used[v] == c;
  bool f = 1;
  a[c]++;
  used[v] = c;
  for (int i = 0; i < (int)G[v].size(); i++)
    f &= dfs(G[v][i], !c);
  return f;
}
int solve() {
  vector<int> v;
  memset(used, -1, sizeof(used));
  for (int i = 0; i < V; i++) {
    if (used[i] >= 0)
      continue;
    a[0] = a[1] = 0;
    if (dfs(i, 0))
      v.push_back(abs(a[1] - a[0]));
    else
      return -1;
  }
  int k = V;
  sort(v.begin(), v.end(), greater<int>());
  set<int> s[2];
  s[0].insert(0);
  for (int i = 0; i < (int)v.size(); i++) {
    bool f = i % 2;
    s[!f].clear();
    for (set<int>::iterator it = s[f].begin(); it != s[f].end(); ++it) {
      s[!f].insert(*it + v[i]);
      s[!f].insert(*it - v[i]);
    }
  }
  for (set<int>::iterator it = s[v.size() % 2].begin();
       it != s[v.size() % 2].end(); ++it)
    k = min(k, abs(*it));
  return ((V - k) / 2) * ((V - k) / 2 + k) - E;
}
int main() {
  cin >> V >> E;
  for (int i = 0, a, b; i < E; i++)
    cin >> a >> b, add_edge(a - 1, b - 1);
  cout << solve() << endl;
  return 0;
}
Problem p01489 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
long long k, _min, _max, x_y, x, y, fibx, fiby, a;
long double mid;

#define MOD 1000000007

int dp[3];

int main() {
  cin >> k;
  a = sqrt(k) / 16;
  x_y = (long long)((2 * sqrtl(k - a - 0.01)) - 1);
  _max = (long long)((x_y + 2) * (x_y + 2) / 4);
  _min = (long long)((x_y + 1) * (x_y + 1) / 4) + 1;
  mid = 1.0 * (_max + _min) / 2;
  if (k <= mid) {
    y = (k - _min) * 2;
  } else {
    y = (_max - k) * 2 + 1;
  }
  x = x_y - y;

  // fibx.
  if (x < 3) {
    fibx = x + 1;
  }

  else {
    dp[0] = 1;
    dp[1] = 2;
    dp[2] = 3;

    for (long long i = 3; i <= x; i += 3) {
      dp[0] = (dp[1] + dp[2]) % MOD;
      dp[1] = (dp[2] + dp[0]) % MOD;
      dp[2] = (dp[0] + dp[1]) % MOD;
    }

    fibx = dp[x % 3];
  }

  // fiby.
  if (y < 3) {
    fiby = y + 1;
  }

  else {
    dp[0] = 1;
    dp[1] = 2;
    dp[2] = 3;

    for (long long i = 3; i <= y; i += 3) {
      dp[0] = (dp[1] + dp[2]) % MOD;
      dp[1] = (dp[2] + dp[0]) % MOD;
      dp[2] = (dp[0] + dp[1]) % MOD;
    }

    fiby = dp[y % 3];
  }

  cout << ((long long)(fibx * fiby) % MOD) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
long long k, _min, _max, x_y, x, y, a;
long double mid;

long long fib(long long n, long long mod) {
  if (n < 3) {
    return n + 1;
  }

  int dp[3];

  dp[0] = 1;
  dp[1] = 2;
  dp[2] = 3;

  for (long long i = 3; i <= n; i += 3) {
    dp[0] = (dp[1] + dp[2]) % mod;
    dp[1] = (dp[2] + dp[0]) % mod;
    dp[2] = (dp[0] + dp[1]) % mod;
  }

  return dp[n % 3];
}

#define MOD 1000000007

int main() {
  cin >> k;
  a = sqrt(k) / 16;
  x_y = (long long)((2.0 * sqrtl(k - a - 0.3)) - 1.0);
  _max = (long long)(pow((1.0 * x_y + 2) / 2, 2));
  _min = (long long)(pow((1.0 * x_y + 1) / 2, 2)) + 1;
  mid = 1.0 * (_max + _min) / 2;
  if (k <= mid) {
    y = (k - _min) * 2;
  } else {
    y = (_max - k) * 2 + 1;
  }
  x = x_y - y;

  cout << ((long long)(fib(x, MOD) * fib(y, MOD)) % MOD) << endl;
  return 0;
}
Problem p01490 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
using ld = long double;
constexpr ld PI = 3.14159265358979;
int main() {
  int N;
  cin >> N;
  vector<ld> r(N);
  for (int i = 0; i < N; i++) {
    cin >> r[i];
  }
  sort(r.begin(), r.end(), greater<ld>{});
  ld maximum = 0;
  for (int i = 3; i <= N; i++) {
    vector<ld> v = r;
    do {
      vector<ld> prod(i);
      for (int j = 0; j < i; j++) {
        prod[j] = v[j] * v[(j + 1) % i];
      }
      sort(prod.begin(), prod.end());
      ld inf = -1;
      ld sup = 1;
      for (int j = 0; j < 30; j++) {
        const ld mid = (inf + sup) / 2;
        const ld lambda = mid * prod[0];
        ld sum = 0;
        for (int k = 0; k < i; k++) {
          sum += acos(lambda / prod[k]);
        }
        if (sum < 2 * PI) {
          sup = mid;
        } else {
          inf = mid;
        }
      }
      inf *= prod[0];
      ld sum = 0;
      ld s = 0;
      for (int j = 0; j < i; j++) {
        const ld theta = acos(inf / prod[j]);
        s += theta;
        sum += prod[j] * sin(theta) / 2;
      }
      if (abs(s - 2 * PI) > 0.001) {
        continue;
      }
      maximum = max(maximum, sum);
    } while (prev_permutation(v.begin(), v.end()));
  }
  cout << fixed << setprecision(15) << maximum << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
using ld = long double;
constexpr ld PI = 3.14159265358979;
int main() {
  int N;
  cin >> N;
  vector<ld> r(N);
  for (int i = 0; i < N; i++) {
    cin >> r[i];
  }
  sort(r.begin(), r.end(), greater<ld>{});
  ld maximum = 0;
  for (int i = 3; i <= N; i++) {
    vector<ld> v = r;
    do {
      vector<ld> prod(i);
      for (int j = 0; j < i; j++) {
        prod[j] = v[j] * v[(j + 1) % i];
      }
      sort(prod.begin(), prod.end());
      ld inf = -1;
      ld sup = 1;
      for (int j = 0; j < 100; j++) {
        const ld mid = (inf + sup) / 2;
        const ld lambda = mid * prod[0];
        ld sum = 0;
        for (int k = 0; k < i; k++) {
          sum += acos(lambda / prod[k]);
        }
        if (sum < 2 * PI) {
          sup = mid;
        } else {
          inf = mid;
        }
      }
      if (inf == 1 or sup == -1) {
        continue;
      }
      inf *= prod[0];
      ld sum = 0;
      for (int j = 0; j < i; j++) {
        const ld theta = acos(inf / prod[j]);
        sum += prod[j] * sin(theta) / 2;
      }
      maximum = max(maximum, sum);
    } while (prev_permutation(v.begin(), v.end()));
  }
  cout << fixed << setprecision(15) << maximum << endl;

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

#include <bits/stdc++.h>
using namespace std;
double PI = acos(-1);
double l[8];

double f(double v, vector<double> L) {
  double sum = 0;
  for (int i = 0; i < L.size() - 1; i++) {
    double t = v / L[i] / L[i + 1];
    sum += acos(t);
  }
  return sum;
}
double calc(double v, vector<double> L) {
  double sum = 0;
  for (int i = 0; i < L.size() - 1; i++) {
    double t = v / L[i] / L[i + 1];

    sum += sin(acos(t)) * L[i] * L[i + 1] / 2.0;
  }
  return sum;
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> l[i];

  double ans = 0;
  for (int i = 0; i < (1 << N); i++) {
    vector<double> L;
    for (int j = 0; j < N; j++)
      if (i >> j & 1)
        L.push_back(l[j]);
    if (L.size() < 3)
      continue;

    sort(L.begin(), L.end());
    do {
      L.push_back(L.front());
      double s = 1e18;
      for (int j = 0; j < L.size() - 1; j++)
        s = min(s, L[j] * L[j + 1]);
      double le = -2 * s, ri = 2 * s;
      for (int k = 0; k < 128; k++) {
        double mi = (le + ri) / 2.;
        if (f(mi, L) < 2 * PI) {
          ri = mi;
        } else {
          le = mi;
        }
      }
      // cout << f(le,L) << endl;
      ans = max(ans, calc(le, L));
      L.pop_back();
    } while (next_permutation(L.begin(), L.end()));
  }
  printf("%.15lf\\n", ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define double long double
double PI = acos(-1);
double l[8];

double f(double v, vector<double> L) {
  double sum = 0;
  for (int i = 0; i < L.size() - 1; i++) {
    double t = v / L[i] / L[i + 1];
    sum += acos(t);
  }
  return sum;
}
double calc(double v, vector<double> L) {
  double sum = 0;
  for (int i = 0; i < L.size() - 1; i++) {
    double t = v / L[i] / L[i + 1];

    sum += sin(acos(t)) * L[i] * L[i + 1] / 2.0;
  }
  return sum;
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> l[i];

  double ans = 0;
  for (int i = 0; i < (1 << N); i++) {
    vector<double> L;
    for (int j = 0; j < N; j++)
      if (i >> j & 1)
        L.push_back(l[j]);
    if (L.size() < 3)
      continue;

    sort(L.begin(), L.end());
    do {
      L.push_back(L.front());
      double s = 1e18;
      for (int j = 0; j < L.size() - 1; j++)
        s = min(s, L[j] * L[j + 1]);
      double le = -2 * s, ri = 2 * s;
      for (int k = 0; k < 128; k++) {
        double mi = (le + ri) / 2.;
        if (f(mi, L) < 2 * PI) {
          ri = mi;
        } else {
          le = mi;
        }
      }
      // cout << f(le,L) << endl;
      ans = max(ans, calc(le, L));
      L.pop_back();
    } while (next_permutation(L.begin(), L.end()));
  }
  printf("%.15Lf\\n", ans);
}
Problem p01491 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef long long int ll;

const int limit = 2500100;
ll M, N;
int m[limit], n[limit];
ll ms[limit], ns[limit];
ll md, nd;

int main(void) {
  cin >> M >> N >> m[0] >> md >> n[0] >> nd;
  rep(i, M - 1) m[i + 1] = (m[i] * 58 + md) % (N + 1);
  rep(i, N - 1) n[i + 1] = (n[i] * 58 + nd) % (M + 1);
  sort(m, m + M + 1);
  sort(n, n + N + 1);
  rep(i, M) ms[i + 1] = ms[i] + m[i + 1];
  rep(i, N) ns[i + 1] = ns[i] + n[i + 1];

  ll ans = M * N;
  rep(i, M + 1) {
    ll cur = ms[i];
    int index = lower_bound(n, n + N + 1, M - i) - n;
    if (index - 1 >= 0)
      cur += ns[index - 1];
    cur += (M - i) * (N - index + 1);
    ans = min(ans, cur);
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef long long int ll;

const int limit = 3000000;
ll M, N;
ll m[limit], n[limit];
ll ms[limit], ns[limit];
ll md, nd;

int main(void) {
  cin >> M >> N >> m[0] >> md >> n[0] >> nd;
  rep(i, M - 1) m[i + 1] = (m[i] * 58 + md) % (N + 1);
  rep(i, N - 1) n[i + 1] = (n[i] * 58 + nd) % (M + 1);
  sort(m, m + M + 1);
  sort(n, n + N + 1);
  rep(i, M) ms[i + 1] = ms[i] + m[i + 1];
  rep(i, N) ns[i + 1] = ns[i] + n[i + 1];

  ll ans = M * N;
  rep(i, M + 1) {
    ll cur = ms[i];
    int index = lower_bound(n, n + N + 1, M - i) - n;
    if (index - 1 >= 0)
      cur += ns[index - 1];
    cur += (M - i) * (N - index + 1);
    ans = min(ans, cur);
  }
  cout << ans << endl;
  return 0;
}