Problem p02926 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

template <class T> struct Vector2D {
  T X, Y;

  Vector2D(T x = 0, T y = 0) : X(x), Y(y) {}

  constexpr T MagnitudeSqr() const { return X * X + Y * Y; }

  constexpr T DistanceSqr(const Vector2D<T> &r) const {
    double a = r.X - X, b = r.Y - Y;
    return a * a + b * b;
  }

  constexpr double Distance(const Vector2D<T> &r) const {
    return sqrt(DistanceSqr(r));
  }

  Vector2D<T> &operator+=(const Vector2D<T> &r) {

    X += r.X;
    Y += r.Y;

    return *this;
  }

  friend Vector2D<T> operator+(Vector2D<T> l, const Vector2D<T> &r) {
    l += r;
    return l;
  }

  Vector2D<T> &operator-=(const Vector2D<T> &r) {

    X -= r.X;
    Y -= r.Y;

    return *this;
  }

  friend Vector2D<T> operator-(Vector2D<T> l, const Vector2D<T> &r) {
    l -= r;
    return l;
  }

  template <class N> Vector2D<T> &operator*=(const N &r) {

    X *= r;
    Y *= r;

    return *this;
  }

  template <class N> friend Vector2D<T> operator*(Vector2D<T> l, const N &r) {
    l *= r;
    return l;
  }

  template <class N> Vector2D<T> &operator/=(const N &r) {

    X /= r;
    Y /= r;

    return *this;
  }

  template <class N> friend Vector2D<T> operator/(Vector2D<T> l, const N &r) {
    l /= r;
    return l;
  }

  T DotProduct(const Vector2D<T> &r) const { return X * r.X + Y * r.Y; }

  Vector2D<T> Midpoint(const Vector2D<T> &r) const {
    return Vector2D<T>((X + r.X) / 2, (Y + r.Y) / 2);
  }

  // これが 0 のとき2つのベクトルは平行
  // （誤差を考え abs(perp) < eps という判定にした方が良い）
  T PerpProduct(const Vector2D<T> &r) const { return X * r.Y - Y * r.X; }

  static vector<Vector2D<T>> GetIntersectionOf2Lines(const Vector2D<T> &p1,
                                                     const Vector2D<T> &v1,
                                                     const Vector2D<T> &p2,
                                                     const Vector2D<T> &v2,
                                                     T eps = 1e-10) {

    T d = v1.PerpProduct(v2);

    if (Abs(d) < eps) {
      // 2つの線は平行

      // 重なっている場合は交点は存在することになるが、今回は省略
      return {};
    }

    auto w = p1 - p2;
    T si = v2.PerpProduct(w) / d;

    return {p1 + v1 * si};
  }

  friend constexpr ostream &operator<<(ostream &os,
                                       const Vector2D<T> &x) noexcept {
    return os << x.X << ',' << x.Y;
  }
};

using ld = long double;

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  cout << std::fixed << std::setprecision(15);

  int N;
  cin >> N;
  vector<Vector2D<ld>> vs(N);
  F0R(i, N) { cin >> vs[i].X >> vs[i].Y; }
  sort(rrng(vs), [](const Vector2D<ld> &l, const Vector2D<ld> &r) {
    return l.MagnitudeSqr() < r.MagnitudeSqr();
  });

  if (N == 1) {
    cout << sqrt(vs[0].MagnitudeSqr());
    return 0;
  }

  ld result = 0;

  int pc = 0;
  for (i64 n = 1; n < 3e8; n *= N) {
    pc++;
  }

  FOR(i, 1, min(N, pc) + 1) {
    Vector2D<ld> v1;

    bitset<100> ub(false);
    vector<int> us(i);
    iota(rng(us), 0);
    bool keep = true;
    F0R(j, i) {
      ub.set(j);
      v1 += vs[j];
    }
    while (keep) {

      ld d = v1.MagnitudeSqr();
      // Print(us);
      // debug(v1);
      // debug(d);
      Vector2D<ld> v2(v1);

      F0R(j, N) {

        if (ub.test(j))
          continue;
        ld nd = (v2 + vs[j]).MagnitudeSqr();
        if (d < nd) {
          v2 += vs[j];
          d = nd;
        }
      }
      chmax(result, d);

      RF0(j, i) {
        // debug(j);
        if (++us[j] <= j + N - i) {
          // debug(us[j] - 1);
          v1 -= vs[us[j] - 1];
          ub.reset(us[j] - 1);
          v1 += vs[us[j]];
          ub.set(us[j]);

          FOR(k, j + 1, i) {
            // debug('!');
            v1 -= vs[us[k] - 1];
            ub.reset(us[k] - 1);
            us[k] = us[k - 1] + 1;
            v1 += vs[us[k]];
            ub.set(us[k]);
          }
          break;
        } else if (j == 0) {
          keep = false;
        }
      }
    }
  }

  cout << sqrt(result);
}
#endif


Fixed submission:
#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

template <class T> struct Vector2D {
  T X, Y;

  Vector2D(T x = 0, T y = 0) : X(x), Y(y) {}

  constexpr T MagnitudeSqr() const { return X * X + Y * Y; }

  constexpr T DistanceSqr(const Vector2D<T> &r) const {
    double a = r.X - X, b = r.Y - Y;
    return a * a + b * b;
  }

  constexpr double Distance(const Vector2D<T> &r) const {
    return sqrt(DistanceSqr(r));
  }

  Vector2D<T> &operator+=(const Vector2D<T> &r) {

    X += r.X;
    Y += r.Y;

    return *this;
  }

  friend Vector2D<T> operator+(Vector2D<T> l, const Vector2D<T> &r) {
    l += r;
    return l;
  }

  Vector2D<T> &operator-=(const Vector2D<T> &r) {

    X -= r.X;
    Y -= r.Y;

    return *this;
  }

  friend Vector2D<T> operator-(Vector2D<T> l, const Vector2D<T> &r) {
    l -= r;
    return l;
  }

  template <class N> Vector2D<T> &operator*=(const N &r) {

    X *= r;
    Y *= r;

    return *this;
  }

  template <class N> friend Vector2D<T> operator*(Vector2D<T> l, const N &r) {
    l *= r;
    return l;
  }

  template <class N> Vector2D<T> &operator/=(const N &r) {

    X /= r;
    Y /= r;

    return *this;
  }

  template <class N> friend Vector2D<T> operator/(Vector2D<T> l, const N &r) {
    l /= r;
    return l;
  }

  T DotProduct(const Vector2D<T> &r) const { return X * r.X + Y * r.Y; }

  Vector2D<T> Midpoint(const Vector2D<T> &r) const {
    return Vector2D<T>((X + r.X) / 2, (Y + r.Y) / 2);
  }

  // これが 0 のとき2つのベクトルは平行
  // （誤差を考え abs(perp) < eps という判定にした方が良い）
  T PerpProduct(const Vector2D<T> &r) const { return X * r.Y - Y * r.X; }

  static vector<Vector2D<T>> GetIntersectionOf2Lines(const Vector2D<T> &p1,
                                                     const Vector2D<T> &v1,
                                                     const Vector2D<T> &p2,
                                                     const Vector2D<T> &v2,
                                                     T eps = 1e-10) {

    T d = v1.PerpProduct(v2);

    if (Abs(d) < eps) {
      // 2つの線は平行

      // 重なっている場合は交点は存在することになるが、今回は省略
      return {};
    }

    auto w = p1 - p2;
    T si = v2.PerpProduct(w) / d;

    return {p1 + v1 * si};
  }

  friend constexpr ostream &operator<<(ostream &os,
                                       const Vector2D<T> &x) noexcept {
    return os << x.X << ',' << x.Y;
  }
};

using ld = long double;

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  cout << std::fixed << std::setprecision(15);

  int N;
  cin >> N;
  vector<Vector2D<ld>> vs(N);
  F0R(i, N) { cin >> vs[i].X >> vs[i].Y; }
  sort(rrng(vs), [](const Vector2D<ld> &l, const Vector2D<ld> &r) {
    return l.MagnitudeSqr() < r.MagnitudeSqr();
  });

  if (N == 1) {
    cout << sqrt(vs[0].MagnitudeSqr());
    return 0;
  }

  ld result = 0;

  int pc = -1;
  for (i64 n = N; n < 2e10; n *= N) {
    pc++;
  }

  FOR(i, 1, min(N, pc) + 1) {
    Vector2D<ld> v1;

    bitset<100> ub(false);
    vector<int> us(i);
    iota(rng(us), 0);
    bool keep = true;
    F0R(j, i) {
      ub.set(j);
      v1 += vs[j];
    }
    while (keep) {

      ld d = v1.MagnitudeSqr();
      // Print(us);
      // debug(v1);
      // debug(d);
      Vector2D<ld> v2(v1);

      F0R(j, N) {

        if (ub.test(j))
          continue;
        ld nd = (v2 + vs[j]).MagnitudeSqr();
        if (d < nd) {
          v2 += vs[j];
          d = nd;
        }
      }
      chmax(result, d);

      RF0(j, i) {
        // debug(j);
        if (++us[j] <= j + N - i) {
          // debug(us[j] - 1);
          v1 -= vs[us[j] - 1];
          ub.reset(us[j] - 1);
          v1 += vs[us[j]];
          ub.set(us[j]);

          FOR(k, j + 1, i) {
            // debug('!');
            v1 -= vs[us[k] - 1];
            ub.reset(us[k] - 1);
            us[k] = us[k - 1] + 1;
            v1 += vs[us[k]];
            ub.set(us[k]);
          }
          break;
        } else if (j == 0) {
          keep = false;
        }
      }
    }
  }

  cout << sqrt(result);
}
#endif