Problem p03255 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef int sign;
typedef long long ll;
#define For(i, a, b) for (register sign i = (sign)(a); i <= (sign)(b); ++i)
#define Fordown(i, a, b) for (register sign i = (sign)(a); i >= (sign)(b); --i)
const int N = 2e5 + 5;
template <typename T> bool cmax(T &a, T b) { return (a < b) ? a = b, 1 : 0; }
template <typename T> bool cmin(T &a, T b) { return (a > b) ? a = b, 1 : 0; }
template <typename T> T read() {
  T ans = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    f = -1, ch = getchar();
  while (isdigit(ch))
    ans = (ans << 3) + (ans << 1) + (ch - '0'), ch = getchar();
  return ans * f;
}
template <typename T> void write(T x, char y) {
  if (x == 0) {
    putchar('0'), putchar(y);
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  static char wr[20];
  int top = 0;
  for (; x; x /= 10)
    wr[++top] = x % 10 + '0';
  while (top)
    putchar(wr[top--]);
  putchar(y);
}
void file() {
#ifndef ONLINE_JUDGE
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
}
int n, x;
ll sum[N], cost[N];
unsigned long long ans;
void input() {
  n = read<int>(), x = read<int>();
  For(i, 1, n) sum[i] = sum[i - 1] + read<int>();
}
void work() {
  cost[1] = 5;
  For(i, 2, n) cost[i] = 1ll * (i + 1) * (i + 1) - 1ll * i * i;
  // For(i,1,n)cout<<cost[i]<<' ';
  // cout<<endl;
  unsigned long long res;
  int l, r, now;
  ans = 1e18;
  For(i, 1, n) {
    //	num=n/i;
    res = 0;
    for (r = n, now = 1; r > 0; r = l - 1, now++) {
      l = r - i + 1;
      cmax(l, 1);
      res = res + (sum[r] - sum[l - 1]) * cost[now];
      if (res > ans)
        res = ans;
    }
    res = res + 1ll * x * i;
    //	cout<<i<<' '<<res<<endl<<endl;

    cmin(ans, res);
    //	i=n/num;
  }
  write(ans + 1ll * n * x, '\n');
}
int main() {
  file();
  input();
  work();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef int sign;
typedef long long ll;
#define For(i, a, b) for (register sign i = (sign)(a); i <= (sign)(b); ++i)
#define Fordown(i, a, b) for (register sign i = (sign)(a); i >= (sign)(b); --i)
const int N = 2e5 + 5;
template <typename T> bool cmax(T &a, T b) { return (a < b) ? a = b, 1 : 0; }
template <typename T> bool cmin(T &a, T b) { return (a > b) ? a = b, 1 : 0; }
template <typename T> T read() {
  T ans = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    f = -1, ch = getchar();
  while (isdigit(ch))
    ans = (ans << 3) + (ans << 1) + (ch - '0'), ch = getchar();
  return ans * f;
}
template <typename T> void write(T x, char y) {
  if (x == 0) {
    putchar('0'), putchar(y);
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  static char wr[20];
  int top = 0;
  for (; x; x /= 10)
    wr[++top] = x % 10 + '0';
  while (top)
    putchar(wr[top--]);
  putchar(y);
}
void file() {
#ifndef ONLINE_JUDGE
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
}
int n, x;
ll sum[N], cost[N];
unsigned long long ans;
void input() {
  n = read<int>(), x = read<int>();
  For(i, 1, n) sum[i] = sum[i - 1] + read<int>();
}
void work() {
  cost[1] = 5;
  For(i, 2, n) cost[i] = 1ll * (i + 1) * (i + 1) - 1ll * i * i;
  // For(i,1,n)cout<<cost[i]<<' ';
  // cout<<endl;
  unsigned long long res;
  int l, r, now;
  ans = 1e18;
  For(i, 1, n) {
    //	num=n/i;
    res = 0;
    for (r = n, now = 1; r > 0; r = l - 1, now++) {
      l = r - i + 1;
      cmax(l, 1);
      res = res + (sum[r] - sum[l - 1]) * cost[now];
      if (res > ans)
        res = ans;
    }
    res = res + 1ll * x * i;
    //	cout<<i<<' '<<res<<endl<<endl;

    cmin(ans, res);
    //	i=n/num;
  }
  write(ans + 1ll * n * x, '\n');
}
int main() {
  //	file();
  input();
  work();
  return 0;
}