Problem p02926 (C++)

Buggy submission (status: Time Limit Exceeded):
// For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456
// a.cpp -o a.exe in cmd
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second

using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  int T;
  T = 1;
  // cin.ignore(); must be there when using getline(cin, s)
  while (T--) {
    ll n, i, j, k, pos;
    vector<pll> v, v2;
    ll a, b, x, y, ans = 0, ct, maxi;
    cin >> n;
    for (i = 1; i <= n; ++i) {
      cin >> a >> b;
      v.pb({a, b});
    }
    for (i = -1; i <= 1; i = i + 2) {
      for (j = -1; j <= 1; j = j + 2) {
        x = 0, y = 0;
        v2.clear();
        for (auto it : v) {
          if (it.f * i >= 0 && it.s * j >= 0) {
            x = x + it.f;
            y = y + it.s;
          } else
            v2.pb({it.f, it.s});
        }
        while (v2.size() > 0) {
          maxi = -1;
          pos = -1;
          for (k = 0; k < v2.size(); ++k) {
            if ((x + v2[k].f) * (x + v2[k].f) + (y + v2[k].s) * (y + v2[k].s) -
                    (x * x + y * y) >
                maxi) {
              maxi = (x + v2[k].f) * (x + v2[k].f) +
                     (y + v2[k].s) * (y + v2[k].s) - (x * x + y * y);
              pos = k;
            }
          }
          if (maxi < 0)
            break;
          // cout<<v2.size()<<" "<<pos<<endl;
          x = x + v2[pos].f;
          y = y + v2[pos].s;
          v2.erase(v2.begin() + pos);
        }
        // cout<<x<<"..."<<y<<endl;
        ans = max(ans, x * x + y * y);
      }
    }

    for (i = -1; i <= 1; i = i + 2) {
      for (j = -1; j <= 1; j = j + 2) {
        x = 0, y = 0;
        v2.clear();
        for (auto it : v) {
          if (it.f * i >= 0 && it.s * j >= 0) {
            x = x + it.f;
            y = y + it.s;
          } else
            v2.pb({it.f, it.s});
        }
        while (v2.size() > 0) {
          maxi = -1;
          pos = -1;
          for (k = 0; k < v2.size(); ++k) {
            if ((x + v2[k].f) * (x + v2[k].f) + (y + v2[k].s) * (y + v2[k].s) -
                    (x * x + y * y) >
                0) {
              maxi = (x + v2[k].f) * (x + v2[k].f) +
                     (y + v2[k].s) * (y + v2[k].s) - (x * x + y * y);
              pos = k;
              break;
            }
          }
          if (maxi < 0)
            break;
          // cout<<v2.size()<<" "<<pos<<endl;
          x = x + v2[pos].f;
          y = y + v2[pos].s;
          v2.erase(v2.begin() + pos);
        }
        // cout<<x<<"..."<<y<<endl;
        ans = max(ans, x * x + y * y);
      }
    }

    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    int X = 100000;
    while (X--) {
      for (i = -1; i <= 1; i = i + 2) {
        for (j = -1; j <= 1; j = j + 2) {
          x = 0, y = 0;
          v2.clear();
          for (auto it : v) {
            if (it.f * i >= 0 && it.s * j >= 0) {
              x = x + it.f;
              y = y + it.s;
            } else
              v2.pb({it.f, it.s});
          }
          shuffle(v2.begin(), v2.end(), rng);
          while (v2.size() > 0) {
            maxi = -1;
            pos = -1;
            for (k = 0; k < v2.size(); ++k) {
              if ((x + v2[k].f) * (x + v2[k].f) +
                      (y + v2[k].s) * (y + v2[k].s) - (x * x + y * y) >
                  0) {
                maxi = (x + v2[k].f) * (x + v2[k].f) +
                       (y + v2[k].s) * (y + v2[k].s) - (x * x + y * y);
                pos = k;
                break;
              }
            }
            if (maxi < 0)
              break;
            // cout<<v2.size()<<" "<<pos<<endl;
            x = x + v2[pos].f;
            y = y + v2[pos].s;
            v2.erase(v2.begin() + pos);
          }
          // cout<<x<<"..."<<y<<endl;
          ans = max(ans, x * x + y * y);
        }
      }
    }

    // cout<<ans<<endl;
    ld l = 0.0, r = 2e8, mid;
    for (i = 0; i <= 200; ++i) {
      mid = (l + r) / 2;
      if (mid * mid > ans)
        r = mid;
      else
        l = mid;
    }
    printf("%.15Lf\n", mid);
  }
  return 0;
}


Fixed submission:
// For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456
// a.cpp -o a.exe in cmd
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second

using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  int T;
  T = 1;
  // cin.ignore(); must be there when using getline(cin, s)
  while (T--) {
    ll n, i, j, k, pos;
    vector<pll> v, v2;
    ll a, b, x, y, ans = 0, ct, maxi;
    cin >> n;
    for (i = 1; i <= n; ++i) {
      cin >> a >> b;
      v.pb({a, b});
    }
    for (i = -1; i <= 1; i = i + 2) {
      for (j = -1; j <= 1; j = j + 2) {
        x = 0, y = 0;
        v2.clear();
        for (auto it : v) {
          if (it.f * i >= 0 && it.s * j >= 0) {
            x = x + it.f;
            y = y + it.s;
          } else
            v2.pb({it.f, it.s});
        }
        while (v2.size() > 0) {
          maxi = -1;
          pos = -1;
          for (k = 0; k < v2.size(); ++k) {
            if ((x + v2[k].f) * (x + v2[k].f) + (y + v2[k].s) * (y + v2[k].s) -
                    (x * x + y * y) >
                maxi) {
              maxi = (x + v2[k].f) * (x + v2[k].f) +
                     (y + v2[k].s) * (y + v2[k].s) - (x * x + y * y);
              pos = k;
            }
          }
          if (maxi < 0)
            break;
          // cout<<v2.size()<<" "<<pos<<endl;
          x = x + v2[pos].f;
          y = y + v2[pos].s;
          v2.erase(v2.begin() + pos);
        }
        // cout<<x<<"..."<<y<<endl;
        ans = max(ans, x * x + y * y);
      }
    }

    for (i = -1; i <= 1; i = i + 2) {
      for (j = -1; j <= 1; j = j + 2) {
        x = 0, y = 0;
        v2.clear();
        for (auto it : v) {
          if (it.f * i >= 0 && it.s * j >= 0) {
            x = x + it.f;
            y = y + it.s;
          } else
            v2.pb({it.f, it.s});
        }
        while (v2.size() > 0) {
          maxi = -1;
          pos = -1;
          for (k = 0; k < v2.size(); ++k) {
            if ((x + v2[k].f) * (x + v2[k].f) + (y + v2[k].s) * (y + v2[k].s) -
                    (x * x + y * y) >
                0) {
              maxi = (x + v2[k].f) * (x + v2[k].f) +
                     (y + v2[k].s) * (y + v2[k].s) - (x * x + y * y);
              pos = k;
              break;
            }
          }
          if (maxi < 0)
            break;
          // cout<<v2.size()<<" "<<pos<<endl;
          x = x + v2[pos].f;
          y = y + v2[pos].s;
          v2.erase(v2.begin() + pos);
        }
        // cout<<x<<"..."<<y<<endl;
        ans = max(ans, x * x + y * y);
      }
    }

    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    int X = 10000;
    while (X--) {
      for (i = -1; i <= 1; i = i + 2) {
        for (j = -1; j <= 1; j = j + 2) {
          x = 0, y = 0;
          v2.clear();
          for (auto it : v) {
            if (it.f * i >= 0 && it.s * j >= 0) {
              x = x + it.f;
              y = y + it.s;
            } else
              v2.pb({it.f, it.s});
          }
          shuffle(v2.begin(), v2.end(), rng);
          while (v2.size() > 0) {
            maxi = -1;
            pos = -1;
            for (k = 0; k < v2.size(); ++k) {
              if ((x + v2[k].f) * (x + v2[k].f) +
                      (y + v2[k].s) * (y + v2[k].s) - (x * x + y * y) >
                  0) {
                maxi = (x + v2[k].f) * (x + v2[k].f) +
                       (y + v2[k].s) * (y + v2[k].s) - (x * x + y * y);
                pos = k;
                break;
              }
            }
            if (maxi < 0)
              break;
            // cout<<v2.size()<<" "<<pos<<endl;
            x = x + v2[pos].f;
            y = y + v2[pos].s;
            v2.erase(v2.begin() + pos);
          }
          // cout<<x<<"..."<<y<<endl;
          ans = max(ans, x * x + y * y);
        }
      }
    }

    // cout<<ans<<endl;
    ld l = 0.0, r = 2e8, mid;
    for (i = 0; i <= 200; ++i) {
      mid = (l + r) / 2;
      if (mid * mid > ans)
        r = mid;
      else
        l = mid;
    }
    printf("%.15Lf\n", mid);
  }
  return 0;
}