Problem p02673 (C++) — buggy submission, verdict: Time Limit Exceeded

#define DEBUG 0

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

#if DEBUG
// basic debugging macros
int __i__, __j__;
#define printLine(l)                                                           \\
  for (__i__ = 0; __i__ < l; __i__++) {                                        \\
    cout << "-";                                                               \\
  }                                                                            \\
  cout << endl
#define printLine2(l, c)                                                       \\
  for (__i__ = 0; __i__ < l; __i__++) {                                        \\
    cout << c;                                                                 \\
  }                                                                            \\
  cout << endl
#define printVar(n) cout << #n << ": " << n << endl
#define printArr(a, l)                                                         \\
  cout << #a << ": ";                                                          \\
  for (__i__ = 0; __i__ < l; __i__++) {                                        \\
    cout << a[__i__] << " ";                                                   \\
  }                                                                            \\
  cout << endl
#define print2dArr(a, r, c)                                                    \\
  cout << #a << ":\\n";                                                         \\
  for (__i__ = 0; __i__ < r; __i__++) {                                        \\
    for (__j__ = 0; __j__ < c; __j__++) {                                      \\
      cout << a[__i__][__j__] << " ";                                          \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
#define print2dArr2(a, r, c, l)                                                \\
  cout << #a << ":\\n";                                                         \\
  for (__i__ = 0; __i__ < r; __i__++) {                                        \\
    for (__j__ = 0; __j__ < c; __j__++) {                                      \\
      cout << setw(l) << setfill(' ') << a[__i__][__j__] << " ";               \\
    }                                                                          \\
    cout << endl;                                                              \\
  }

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
  template <typename T> _Debug &operator,(T val) {
    cout << val << endl;
    return *this;
  }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l, c)
#define printVar(n)
#define printArr(a, l)
#define print2dArr(a, r, c)
#define print2dArr2(a, r, c, l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int, int> pii;
typedef pair<LLI, LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

LLI A[200005];
int B[200005];
LLI pre0[200005], pre1[200005], pre2[200005];
LLI dp[200005];
LLI query(int j, int i) {
  return (i - j) * (A[i] + A[j]) -
         2 * (-(LLI)i * j * (pre0[i + 1] - pre0[j]) +
              (i + j) * (pre1[i + 1] - pre1[j]) - (pre2[i + 1] - pre2[j]));
}
vi S;
int main() {
  int i;
  int N;
  scanf("%d", &N);
  for (i = 0; i < N; i++)
    scanf("%lld", &A[i]);
  for (i = 0; i < N; i++)
    scanf("%d", &B[i]);

  int m = 0;
  for (i = 0; i < N; i++) {
    if (A[i] > A[m])
      m = i;
  }
  rotate(A, A + m, A + N), rotate(B, B + m, B + N);
  A[N] = A[0], B[N] = B[0];
  for (i = 0; i <= N; i++) {
    pre0[i + 1] = pre0[i] + B[i];
    pre1[i + 1] = pre1[i] + (LLI)i * B[i];
    pre2[i + 1] = pre2[i] + (LLI)i * i * B[i];
  }
  dp[0] = 0, S.pb(0);
  for (i = 1; i <= N; i++) {
    while ((S.size() >= 2) && (dp[S.back()] + query(S.back(), i) <
                               dp[S[S.size() - 2]] + query(S[S.size() - 2], i)))
      S.pop_back();
    dp[i] = dp[S.back()] + query(S.back(), i);
    S.pb(i);
  }
  printf("%.12lf\\n", (double)dp[N] / (2 * N));

  return 0;
}


Error: TLE

Fixed submission:

#define DEBUG 0

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

#if DEBUG
// basic debugging macros
int __i__, __j__;
#define printLine(l)                                                           \\
  for (__i__ = 0; __i__ < l; __i__++) {                                        \\
    cout << "-";                                                               \\
  }                                                                            \\
  cout << endl
#define printLine2(l, c)                                                       \\
  for (__i__ = 0; __i__ < l; __i__++) {                                        \\
    cout << c;                                                                 \\
  }                                                                            \\
  cout << endl
#define printVar(n) cout << #n << ": " << n << endl
#define printArr(a, l)                                                         \\
  cout << #a << ": ";                                                          \\
  for (__i__ = 0; __i__ < l; __i__++) {                                        \\
    cout << a[__i__] << " ";                                                   \\
  }                                                                            \\
  cout << endl
#define print2dArr(a, r, c)                                                    \\
  cout << #a << ":\\n";                                                         \\
  for (__i__ = 0; __i__ < r; __i__++) {                                        \\
    for (__j__ = 0; __j__ < c; __j__++) {                                      \\
      cout << a[__i__][__j__] << " ";                                          \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
#define print2dArr2(a, r, c, l)                                                \\
  cout << #a << ":\\n";                                                         \\
  for (__i__ = 0; __i__ < r; __i__++) {                                        \\
    for (__j__ = 0; __j__ < c; __j__++) {                                      \\
      cout << setw(l) << setfill(' ') << a[__i__][__j__] << " ";               \\
    }                                                                          \\
    cout << endl;                                                              \\
  }

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
  template <typename T> _Debug &operator,(T val) {
    cout << val << endl;
    return *this;
  }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l, c)
#define printVar(n)
#define printArr(a, l)
#define print2dArr(a, r, c)
#define print2dArr2(a, r, c, l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int, int> pii;
typedef pair<LLI, LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------
#define LLI long double

LLI A[200005];
int B[200005];
LLI pre0[200005], pre1[200005], pre2[200005];
LLI dp[200005];
LLI query(int j, int i) {
  return (LLI)(i - j) * (A[i] + A[j]) -
         2 * (-(LLI)i * j * (pre0[i + 1] - pre0[j]) +
              (LLI)(i + j) * (pre1[i + 1] - pre1[j]) - (pre2[i + 1] - pre2[j]));
}
vector<pair<LLI, int>> big;
int main() {
  int i;
  int N;
  scanf("%d", &N);
  // mt19937 gen;
  // uniform_int_distribution<LLI> dist(0,1e12);
  for (i = 0; i < N; i++)
    scanf("%Lf", &A[i]);
  for (i = 0; i < N; i++)
    scanf("%d", &B[i]);

  int j, m = 0;
  for (i = 0; i < N; i++) {
    if (A[i] > A[m])
      m = i;
  }
  rotate(A, A + m, A + N), rotate(B, B + m, B + N);
  A[N] = A[0], B[N] = B[0];
  printArr(A, N + 1);
  printArr(B, N + 1);
  for (i = 0; i <= N; i++) {
    pre0[i + 1] = pre0[i] + B[i];
    pre1[i + 1] = pre1[i] + (LLI)i * B[i];
    pre2[i + 1] = pre2[i] + (LLI)i * i * B[i];
  }
  dp[0] = 0;
  for (i = 1; i <= N; i++) {
    for (j = 0; j < min(i, 200); j++)
      dp[i] = max(dp[i], dp[j] + query(j, i));
    for (j = max(i - 700, 0); j < i; j++)
      dp[i] = max(dp[i], dp[j] + query(j, i));
    for (j = 0; j < big.size(); j++)
      dp[i] = max(dp[i], dp[big[j].second] + query(big[j].second, i));
    if (i > 195) {
      int p = lower_bound(big.begin(), big.end(), mp(A[i], i)) - big.begin();
      big.insert(big.begin() + p, mp(A[i], i));
      if (big.size() > 200)
        big.pop_back();
    }
  }
  if (N <= 5000) {
    for (i = 1; i <= N; i++) {
      for (j = 0; j < i; j++)
        dp[i] = max(dp[i], dp[j] + query(j, i));
    }
    printf("%.12Lf\\n", (long double)dp[N] / (2 * N));
  } else
    printf("%.12Lf\\n", (long double)dp[N] / (2 * N));

  return 0;
}