Language: C++

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

typedef long double ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 5e18
#define mod 1000000007

P seg[1<<21]; // < 10^6
ll sz;

inline ll calc(const P& f,ll x){ return f.first * x + f.second; }

void init(ll n){
  sz=1;
  while(sz<n)sz*=2;
  for(ll i=0;i<sz*2;i++)seg[i]=P(0,INF);
}

void apply(const P& func,ll k=0,ll l=0,ll r=sz){
  P crt=seg[k];
  if(calc(crt,l)<=calc(func,l)&&calc(crt,r-1)<=calc(func,r-1)) return;
  if(calc(crt,l)>=calc(func,l)&&calc(crt,r-1)>=calc(func,r-1)){ seg[k]=func; return; }

  P lo,hi;
  ll mid=(l+r)/2;
  if(calc(crt,l)<calc(func,l)){ lo=crt; hi=func; }
  else{ lo=func; hi=crt; }

  if(calc(lo,mid)>calc(hi,mid)){
    seg[k]=hi;
    apply(lo,k*2+1,l,mid);
  }else{
    seg[k]=lo;
    apply(hi,k*2+2,mid,r);
  }
}

ll query(ll x,ll k=0,ll l=0,ll r=sz){
  if(l+1==r)return calc(seg[k],x);

  ll res=calc(seg[k],x);
  ll mid=(l+r)/2;
  if(x<mid) res=min(res,query(x,k*2+1,l,mid));
  else res=min(res,query(x,k*2+2,mid,r));
  return res;
}

ll N,X;
ll x[200010];
ll sum[200010],sumk[200010];

int main(){
  cin>>N>>X;
  rep(i,N)cin>>x[i];
  rep(i,N){
    sum[i+1]=sum[i]+x[i];
    sumk[i+1]=sumk[i]+x[i]*i;
  }

  init(500000);
  apply(P(0,0));
  ll res=N*X;
  repl(i,1,N+1){
    ll crtadd=x[i-1]+x[i-1]*4+X;
    ll crtv=query(2*i+1)+(2*i+1)*sum[i-1]-2*sumk[i-1];
    if(i==N){
      res+=crtadd+crtv;
      break;
    }
    apply(P(-sum[i],2*sumk[i]+crtadd+crtv));
  }

  cout<<res<<endl;

  return 0;
}


Compiler output:
a.cc: In function 'void init(ll)':
a.cc:33:28: error: invalid types 'P [2097152] {aka std::pair<long double, long double> [2097152]}[ll {aka long double}]' for array subscript
   33 |   for(ll i=0;i<sz*2;i++)seg[i]=P(0,INF);
      |                            ^
a.cc: In function 'void apply(const P&, ll, ll, ll)':
a.cc:37:12: error: invalid types 'P [2097152] {aka std::pair<long double, long double> [2097152]}[ll {aka long double}]' for array subscript
   37 |   P crt=seg[k];
      |            ^
a.cc:39:68: error: invalid types 'P [2097152] {aka std::pair<long double, long double> [2097152]}[ll {aka long double}]' for array subscript
   39 |   if(calc(crt,l)>=calc(func,l)&&calc(crt,r-1)>=calc(func,r-1)){ seg[k]=func; return; }
      |                                                                    ^
a.cc:47:8: error: invalid types 'P [2097152] {aka std::pair<long double, long double> [2097152]}[ll {aka long double}]' for array subscript
   47 |     seg[k]=hi;
      |        ^
a.cc:50:8: error: invalid types 'P [2097152] {aka std::pair<long double, long double> [2097152]}[ll {aka long double}]' for array subscript
   50 |     seg[k]=lo;
      |        ^
a.cc: In function 'll query(ll, ll, ll, ll)':
a.cc:56:28: error: invalid types 'P [2097152] {aka std::pair<long double, long double> [2097152]}[ll {aka long double}]' for array subscript
   56 |   if(l+1==r)return calc(seg[k],x);
      |                            ^
a.cc:58:18: error: invalid types 'P [2097152] {aka std::pair<long double, long double> [2097152]}[ll {aka long double}]' for array subscript
   58 |   ll res=calc(seg[k],x);
      |                  ^
a.cc: In function 'int main()':
a.cc:71:17: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   71 |   rep(i,N)cin>>x[i];
      |                 ^
a.cc:73:8: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   73 |     sum[i+1]=sum[i]+x[i];
      |        ^
a.cc:73:17: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   73 |     sum[i+1]=sum[i]+x[i];
      |                 ^
a.cc:73:22: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   73 |     sum[i+1]=sum[i]+x[i];
      |                      ^
a.cc:74:9: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   74 |     sumk[i+1]=sumk[i]+x[i]*i;
      |         ^
a.cc:74:19: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   74 |     sumk[i+1]=sumk[i]+x[i]*i;
      |                   ^
a.cc:74:24: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   74 |     sumk[i+1]=sumk[i]+x[i]*i;
      |                        ^
a.cc:81:16: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   81 |     ll crtadd=x[i-1]+x[i-1]*4+X;
      |                ^
a.cc:81:23: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   81 |     ll crtadd=x[i-1]+x[i-1]*4+X;
      |                       ^
a.cc:82:37: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   82 |     ll crtv=query(2*i+1)+(2*i+1)*sum[i-1]-2*sumk[i-1];
      |                                     ^
a.cc:82:49: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   82 |     ll crtv=query(2*i+1)+(2*i+1)*sum[i-1]-2*sumk[i-1];
      |                                                 ^
a.cc:87:17: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   87 |     apply(P(-sum[i],2*sumk[i]+crtadd+crtv));
      |                 ^
a.cc:87:27: error: invalid types 'll [200010] {aka long double [200010]}[ll {aka long double}]' for array subscript
   87 |     apply(P(-sum[i],2*sumk[i]+crtadd+crtv));
      |                           ^