Problem p03255 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<LD, LD> PDD;

#define _upgrade                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
template <typename _T> inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',')
    cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}

#ifdef LOCAL
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) (__VA_ARGS__)
#define cerr                                                                   \
  if (0)                                                                       \
  cout
#endif

// ********************** CODE ********************** //

const int N = 2e5 + 7;
const LL INF = 1e18 + 7;

int n;
LL x, T[N];

LL count(int k) {
  LL ans = x * (n + k);
  int tmp = k, kr = 1;
  for (int i = n; i; i--) {
    if (kr == 1)
      ans += 2LL * T[i];
    ans += T[i] * (2 * kr + 1);
    tmp--;
    if (tmp == 0) {
      tmp = k;
      kr++;
    }
  }
  return ans;
}

int main() {
  _upgrade cin >> n >> x;
  for (int i = 1; i <= n; i++)
    cin >> T[i];
  LL ans = INF;
  for (int i = 1; i <= n; i++)
    ans = min(ans, count(i));
  cout << ans << "\n";
  return 0;
}


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

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<LD, LD> PDD;

#define _upgrade                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
template <typename _T> inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',')
    cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}

#ifdef LOCAL
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) (__VA_ARGS__)
#define cerr                                                                   \
  if (0)                                                                       \
  cout
#endif

// ********************** CODE ********************** //

const int N = 2e5 + 7;
const LL INF = 1e18 + 7;

int n;
LL x, T[N];

LL count(int k) {
  LL ans = x * (n + k);
  int tmp = k, kr = 1;
  for (int i = n; i; i--) {
    if (kr == 1)
      ans += 2LL * T[i];
    ans += T[i] * (2 * kr + 1);
    tmp--;
    if (tmp == 0) {
      tmp = k;
      kr++;
    }
  }
  return ans;
}

int main() {
  _upgrade cin >> n >> x;
  for (int i = 1; i <= n; i++)
    cin >> T[i];
  LL ans = INF;
  if (n <= 2000) {
    for (int i = 1; i <= n; i++)
      ans = min(ans, count(i));
  } else {
    int l = 1, r = n;
    while (l + 20 < r) {
      int l1 = (l + l + r) / 3;
      int r1 = (l + r + r) / 3;
      LL c1 = count(l1), c2 = count(r1);
      ans = min({ans, c1, c2});
      if (c1 < c2) {
        r = r1;
      } else {
        l = l1;
      }
    }
    for (int i = l; i <= r; i++)
      ans = min(ans, count(i));
  }
  cout << ans << "\n";
  return 0;
}