Problem:
2014 年春，ある学生は無事大学に合格し，一人暮らしを始める事となった．ここで問題となるのが夕食をどうするかである．彼はこれから
N
日間の夕食の計画を立てる事にした．
彼は
N
日間で得られる幸福度の合計を出来る限り大きくしたい．もちろん，美味しいものや好きなものを食べるほど幸福度は高い．
彼の夕食の選択肢は2 つ，近くの食堂に向かうか，自炊をするかのどちらかである．
食堂で得られる幸福度は，その日のメニューによって変わる．メニューは日替わりだが毎日一種類のみで，
N
日間のメニューは全て既に公開されている．なので，彼は
i
日目 (1 ≤
i
≤
N
) に食堂に行けば
C
i
の幸福度を得られるという情報を全て知っている．
自炊で得られる幸福度は，その自炊の開始時点での
自炊パワー
に定数
P
を掛けたものである．
自炊パワー
は初期値が
Q
であり，毎日，食堂に行けば-1，自炊をすれば+1，その日の食事の終了時に変動する．
彼のために幸福度の総和の最大値を求めよう．
Input
入力は以下の形式で
N + 1
行で与えられる．
N
P
Q
C
1
C
2
:
C
N
1 行目は 3 つの整数
N, P, Q
が与えられ，それぞれ日数，自炊の幸福度を算出するための定数，自炊パワーの初期値である．
2 行目から
N + 1
行目にはそれぞれ整数が 1 つずつ与えられ，
i + 1
行目では
i
日目に食堂に行った時に得られる幸福度が与えられる．
Constraints
1 ≤
N
≤ 500,000
0 ≤
P
≤ 500,000
|Q|
≤ 500,000
|C
i
|
≤ 500,000
Output
幸福度の取りうる最大値を一行に出力せよ．
Sample Input 1
1 1 1
2
Output for the Sample Input 1
2
考えるべき予定は1 日だけである．食堂に行けば2，自炊すると1 の幸福度なので，答えは2 となる．
Sample Input 2
3 2 1
3
3
3
Output for the Sample Input 2
12
このケースでは毎日自炊をするのが最適で，幸福度は
2 × 1 + 2 × 2 + 2 × 3 = 12
となる．
Sample Input 3
3 1 -1
2
-3
2
Output for the Sample Input 3
2
2 日目のみで自炊をすることが最善となる．
Sample Input 4
3 1 -10
-10
-10
-10
Output for the Sample Input 4
-27
答えが負になることもある．いくらまずくても夕食は食べなければならない．

Submitted solutions:

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/05/02  Problem: AOJ 2642 / Link: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2642  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----

明らかにDPとかができない

選択用の比較関数が事前にわからないとむりなので、考える
k回自炊をしていて、i日目に自炊をするとする。

増分は、-C[i] + (Q-2*k-i)*P
このままではO(N^2)っぽいけど、意味論的にはk回自炊をするときの評価となるので、
累積を考えることになる。
あるkのときiを選択できるようになるので、初期値を考えるとO(N)

----解説ここまで---- */

LL N;

LL ans = 0LL;

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	LL P, Q;
	cin >> N >> P >> Q;
	VL c(N);
	VL comp(N);
	FOR(i, 0, N) {
		cin >> c[i];
		comp[i] = c[i] + i*P;
	}
	SORT(comp);
	LL sum = accumulate(ALL(c), 0LL);
	LL val = 0;
	ans = sum;
	FOR(i, 1, N + 1) {
		val += comp[i - 1];
		LL ret = sum + i*P*Q + P*i*(i - 1) - val;
		ans = max(ans, ret);
	}
	cout << ans << "\n";

	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 10936
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, p, q;
    cin >> n >> p >> q;
    vector<ll> x(n);
    ll sum = 0;

    rep(i, n) { 
        cin >> x[i];
        sum += x[i];
        x[i] += p * i;
    }
    sort(all(x));
    ll ans = sum;
    rep(i, n) {
        sum -= x[i];
        sum += p * 2 * i;
        sum += p * q;
        ans = max(ans, sum);
    }
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 7044
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll N, P, Q;
  cin >> N >> P >> Q;

  vector<ll> C(N);
  ll S = 0;
  for(ll i = 0; i < N; i++) {
    cin >> C[i];
    S += C[i];
    C[i] += P * (i + 1);
  }
  ranges::sort(C);

  ll ans = -1e18, s = 0;
  for(ll i = 0; i <= N; i++) {
    ll t = S + P * i * (Q + i) - s;
    if(i != N) { s += C[i]; }
    ans = max(ans, t);
  }

  cout << ans << "\n";
}
accuracy: 1
time_ms: 40
memory_kb: 7336
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
    int N;
    ll P, Q;
    cin >> N >> P >> Q;
    vector<ll> C(N), D(N);
    ll COUNT = 0;
    rep(i,0,N) {
        cin >> C[i];
        D[i] = -C[i]-(ll)i*P;
        COUNT += C[i];
    }
    sort(D.begin(), D.end(), greater<ll>());
    vector<ll> E(N+1);
    E[0] = COUNT;
    rep(i,0,N) E[i+1]=E[i]+D[i];
    ll ANS = -INF;
    rep(i,0,N+1) chmax(ANS, E[i]+(ll)i*Q*P+(ll)i*(ll)(i-1)*P);
    cout << ANS << endl;
}
accuracy: 1
time_ms: 110
memory_kb: 14912
final_rank: 13

code_snippet: #include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define rep(i, a, n) for(int i = a; i < n; i++)
#define rrep(i, a, n) for(int i = a; i >= n; i--)
#define inr(l, x, r) (l <= x && x < r)
#define ll long long
#define ld long double

// using mint = modint1000000007;
// using mint = modint998244353;
constexpr int IINF = 1001001001;
constexpr ll INF = 9e18;

template <typename T> inline bool chmax(T &a, const T b) { if (a < b) { a = b; return true; } return false; }
template <typename T> inline bool chmin(T &a, const T b) { if (a > b) { a = b; return true; } return false; }

/*
全部自炊すると Σ P(Q+i)
i 日目を食堂にする
d := 自炊した回数 - 食堂に行った回数
- P(Q+d) 減る（自炊しない分）
- c_i を得る
- 2P(n-1-i) 減る (後ろの日について自炊パワーが1減る)
まとめると c_i - P(Q+d+2(n-1-i)) 変化する
これが正のところだけ食堂に行けばよい？

あってそうー
*/

int main(){
    ll n, p, q; cin >> n >> p >> q;
    vector<ll> c(n);
    rep(i, 0, n) cin >> c[i];
    ll sum_c = 0;
    rep(i, 0, n) sum_c += c[i];
    ll ans = sum_c, tmp = sum_c;

    vector<ll> d(n);
    rep(i, 0, n) d[i] = -c[i]+p*(q-i);
    priority_queue<ll> que;
    ll cnt = 0;
    rep(i, 0, n) que.push(d[i]);
    while(!que.empty()){
        ll r = que.top(); que.pop();
        tmp += r;
        cnt++;
        chmax(ans, tmp);
        if(cnt < n) tmp += cnt*2*p;
    }

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 17136
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
#define rep(i, N) for(ll i = 0; i < (ll)N; i++)
#define ALL(V)  (V).begin(), (V).end()
ll dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
ll dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
    ll N, P, Q; cin >> N >> P >> Q;
    vector<ll> C(N);
    ll SUM = 0;
    rep (i, N) cin >> C[i], SUM += C[i];
    ll ans = SUM;
    vector<ll> V(N);
    rep (i, N) {
        V[i] = C[i] + P * i;
    }
    sort(ALL(V));
    rep (i, N - 1) {
        V[i + 1] += V[i];
    }
    
    for (ll i = 1; i <= N; i++) {
        ll tmp = SUM;
        tmp += P * Q * i + P * (i*i - i) - V[i-1];
        // cout << tmp << endl;
        ans = max(ans, tmp);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 10932
final_rank: 8

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

void solve() {
    int n;
    long long p, q;
    cin >> n >> p >> q;
    long long c[500010] = {};
    long long s[500010] = {};
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> c[i];
        sum += c[i];
    }
    vector<long long> vec;
    for (int i = 0; i < n; i++) {
        vec.push_back(c[i] + (i + 2) * p);
    }
    sort(vec.begin(), vec.end());
    for (int i = 0; i < n; i++) {
        s[i + 1] = s[i] + vec[i];
    }
    long long ans = -3e18;
    for (int m = 0; m <= n; m++) {
        long long r = sum + q * m * p + p * m * (m + 1) - s[m];
        ans = max(ans, r);
    }
    cout << ans << endl;
    exit(0);
}

int main() {
    while (1) {
        solve();
    }
}
accuracy: 1
time_ms: 120
memory_kb: 17124
final_rank: 14

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
#define INF 1000000001LL
#define LNF 1000000000000000001LL
#define MOD 1000000007LL
#define MAX 502
#define long long long
#define all(x) x.begin(),x.end()
using namespace std;

int main()
{
	ios_base::sync_with_stdio(0); 
    cin.tie(0);

    long n,p,q;
    cin >> n >> p >> q;

    vector<long> arr(n);
    long sum = 0;
    long res = 0;
    for(int i = 0; i<n; i++)
    {
        cin >> arr[i];
        sum+=arr[i];
        arr[i]+=i*p;
    }
    sort(all(arr));
    res = sum;

    for(int i = 0; i<n; i++)
    {
        sum-=arr[i];
        sum+=(q+2*i)*p;
        res = max(res,sum);
    }

    cout << res << endl;
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 6976
final_rank: 3

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

int main() {
    i64 N = in(), P = in(), Q = in();
    vector<i64> C = in(N);

    i64 sumC = [&] {
        i64 s = 0;
        for(int i : rep(N)) s += C[i];
        return s;
    }();

    vector<i64> A(N);
    for(i64 i : rep(N)) A[i] = C[i] + i * P;
    sort(A);
    vector<i64> S(N + 1, 0);
    for(int i : rep(N)) S[i + 1] = S[i] + A[i];

    i64 ans = -8e18;
    for(i64 m = 0; m <= N; m++) {
        i64 fm = P * Q * m + P * m * (m - 1);
        chmax(ans, sumC + fm - S[m]);
    }
    print(ans);
}
accuracy: 1
time_ms: 50
memory_kb: 14832
final_rank: 9

code_snippet: // (⁠◕⁠ᴗ⁠◕⁠✿⁠)

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define srep(i, s, n) for (ll i = s; i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using vvv = vv<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = 3.141592653589793;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;
inline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }

#define debug(var)  do{std::cout << #var << " : \n";view(var);}while(0)
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }

int main(){
    int N; ll P, Q; cin >> N >> P >> Q;
    vl C(N); rep(i, N) cin >> C[i];
    vl A; rep(i, N) A.push_back(-C[i] - i * P);
    sort(all(A));
    reverse(all(A));
    ll sm = accumulate(all(C), 0ll), ans = sm, tmp = 0;
    rep(m, N){
        tmp += A[m];
        ans = max(ans, sm + tmp + P * Q * (m + 1) + P * m * (m + 1));
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 110
memory_kb: 12964
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    long long p, q, c, sum=0;
    vector<long long> vi;
    cin >> n >> p >> q;
    for(int i=0;i<n;i++){
        cin >> c;
        sum += c;
        vi.push_back(c+p*i);
    }
    sort(vi.begin(), vi.end());
    long long now=0, ans=sum;
    for(int i=0;i<n;i++){
        now += vi[i];
        ans = max(ans, sum + p*q*(i+1) + p*i*(i+1) - now);
    }
    cout << ans << endl;
    return(0);
}
accuracy: 1
time_ms: 140
memory_kb: 7064
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

long long N, C[1 << 19];
long long P;
long long Q;
vector<pair<long long, int>> vec;

int main() {
	// Step 1. Input
	cin >> N >> P >> Q;
	for (int i = 1; i <= N; i++) cin >> C[i];

	// Step 2. Sorting
	for (int i = 1; i <= N; i++) vec.push_back(make_pair(C[i] + 1LL * i * P, i));
	sort(vec.begin(), vec.end());

	// Step 3. Brute Force
	long long Current = 0;
	long long Answer = 0;
	for (int i = 1; i <= N; i++) Current += C[i];
	Answer = Current;
	for (int i = 1; i <= N; i++) {
		int idx = vec[i - 1].second;
		Current -= C[idx];
		Current += P * (Q - (idx - 1));
		long long subans = Current + 1LL * P * i * (i - 1);
		Answer = max(Answer, subans);
		// cout << i << " " << subans << endl;
	}

	// Step 4. Output
	cout << Answer << endl;
	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 17344
final_rank: 15

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

long long N, C[1 << 19];
long long P;
long long Q;
vector<pair<long long, int>> vec;

int main() {
	// Step 1. Input
	cin >> N >> P >> Q;
	for (int i = 1; i <= N; i++) cin >> C[i];

	// Step 2. Sorting
	for (int i = 1; i <= N; i++) vec.push_back(make_pair(C[i] - 2LL * P * i, i));
	sort(vec.begin(), vec.end());

	// Step 3. Brute Force
	long long Current = 0;
	long long Answer = 0;
	for (int i = 1; i <= N; i++) Current += C[i];
	Answer = Current;
	for (int i = 1; i <= N; i++) {
		int idx = vec[i - 1].second;
		Current -= C[idx];
		Current += P * (Q - (idx - 1));
		long long subans = Current + 1LL * P * i * (i - 1);
		Answer = max(Answer, subans);
		// cout << i << " " << subans << endl;
	}

	// Step 4. Output
	cout << Answer << endl;
	return 0;
}
accuracy: 0.6
time_ms: 120
memory_kb: 16876
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

long long N, C[1 << 19];
long long P;
long long Q;
vector<pair<long long, int>> vec;

int main() {
	// Step 1. Input
	cin >> N >> P >> Q;
	for (int i = 1; i <= N; i++) cin >> C[i];

	// Step 2. Sorting
	for (int i = 1; i <= N; i++) vec.push_back(make_pair(C[i] - 2LL * P * (N - i), i));
	sort(vec.begin(), vec.end());

	// Step 3. Brute Force
	long long Current = 0;
	long long Answer = 0;
	for (int i = 1; i <= N; i++) Current += C[i];
	Answer = Current;
	for (int i = 1; i <= N; i++) {
		int idx = vec[i - 1].second;
		Current -= C[idx];
		Current += P * (Q - (idx - 1));
		long long subans = Current + 1LL * P * i * (i - 1);
		Answer = max(Answer, subans);
	}

	// Step 4. Output
	cout << Answer << endl;
	return 0;
}
accuracy: 0.6
time_ms: 120
memory_kb: 16880
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

long long N, C[1 << 19];
long long P;
long long Q;
vector<pair<long long, int>> vec;

long long Eval(int num) {
	vector<bool> dinner(N + 1, false);
	for (int i = 0; i < num; i++) dinner[vec[i].second] = true;

	long long Return = 0, Power = Q;
	for (int i = 1; i <= N; i++) {
		if (dinner[i] == true) { Return += C[i]; Power -= 1; }
		else { Return += P * Power; Power += 1; }
	}
	return Return;
}

int main() {
	// Step 1. Input
	cin >> N >> P >> Q;
	for (int i = 1; i <= N; i++) cin >> C[i];

	// Step 2. Sorting
	for (int i = 1; i <= N; i++) vec.push_back(make_pair(C[i] - 2LL * P * (N - i), i));
	sort(vec.begin(), vec.end());
	reverse(vec.begin(), vec.end());

	// Step 3. Ternary Search
	long long cl = 0, cr = N, c1, c2;
	for (int i = 0; i < 50; i++) {
		c1 = (cl + cl + cr) / 3;
		c2 = (cl + cr + cr) / 3;
		long long a1 = Eval(c1);
		long long a2 = Eval(c2);
		if (a1 < a2) { cl = c1; }
		else { cr = c2; }
	}

	// Step 4. Brute Force
	long long Answer = -(1LL << 60);
	for (int i = cl - 50; i <= cl + 50; i++) {
		if (i<0 || i>N) continue;
		Answer = max(Answer, Eval(i));
	}
	cout << Answer << endl;
	return 0;
}
accuracy: 0.6
time_ms: 380
memory_kb: 16968
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define dbg(x) cerr << #x << ":" << x << endl;
#define all(v) v.begin(), v.end()

constexpr ll LINF = 1e18;

int main() {
  ll n, p, q;
  cin >> n >> p >> q;
  vector<ll> c(n);
  rep(i, n) cin >> c[i];
  ll sum = 0;
  rep(k, n) { sum += p * (q + k); }

  vector<ll> v(n);
  rep(k, n) { v[k] = c[k] - (q + k) * p - p * 2 * (n - k - 1); }
  sort(all(v));
  reverse(all(v));

  ll ans = sum;
  rep(k, n) {
    sum += v[k] + p * 2 * k;
    ans = max(ans, sum);
  }
  cout << ans << "\n";
}
accuracy: 1
time_ms: 120
memory_kb: 10880
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ll n, p, q;
    cin >> n >> p >> q;

    vector<ll> c(n);
    for(int i = 0; i < n; i++) cin >> c[i];

    vector<ll> x(n);
    for(int i = 0; i < n; i++) x[i] = p * i + c[i];
    sort(x.begin(), x.end());

    ll ans = 0;

    ll sum = 0;

    for(int k = 1; k <= n; k++) {
        sum += x[k - 1];
        ans = max(ans, k * p * q + p * k * (k - 1) - sum);
    }

    for(int i = 0; i < n; i++) ans += c[i];

    cout << ans << endl;
}
accuracy: 1
time_ms: 110
memory_kb: 10924
final_rank: 7

code_snippet: #include <string.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <ciso646>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME
#define REP3(i, l, r, s)                                               \
    for (int i = int(l), rep3_r = int(r), rep3_s = int(s); i < rep3_r; \
         i += rep3_s)
#define REP2(i, l, r) REP3(i, l, r, 1)
#define REP1(i, n) REP2(i, 0, n)
#define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1, )(__VA_ARGS__)
#define repin(i, l, r) for (int i = int(l), repin_r = int(r); i <= repin_r; ++i)

#define RREP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME
#define RREP3(i, l, r, s)                                                      \
    for (int i = int(r) - 1, rrep3_l = int(l), rrep3_s = int(s); i >= rrep3_l; \
         i -= rrep3_s)
#define RREP2(i, l, r) RREP3(i, l, r, 1)
#define RREP1(i, n) RREP2(i, 0, n)
#define rrep(...) RREP_OVERLOAD(__VA_ARGS__, RREP3, RREP2, RREP1, )(__VA_ARGS__)
#define rrepin(i, l, r) \
    for (int i = int(r), rrepin_l = int(l); i >= rrepin_l; --i)


#include <algorithm>
#include <cassert>
#include <vector>

namespace rklib {

// a <- max(a, b)
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

// a <- min(a, b)
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

// if a < 0: a <- b
// else: a <- min(a, b)
template <class T>
bool chmin_non_negative(T &a, const T &b) {
    if (a < 0 || a > b) {
        a = b;
        return true;
    }
    return false;
}

// floor(num / den)
template <class T>
T div_floor(T num, T den) {
    if (den < 0) num = -num, den = -den;
    return num >= 0 ? num / den : (num + 1) / den - 1;
}

// ceil(num / den)
template <class T>
T div_ceil(T num, T den) {
    if (den < 0) num = -num, den = -den;
    return num <= 0 ? num / den : (num - 1) / den + 1;
}

namespace internal {

template <class T>
T remainder_count(T r, T b, T m) {
    return r / m * b + std::min(b, r % m);
}

}  // namespace internal

// Number of integer x s.t.
// - x in [l, r)
// - x mod m in [a, b)
template <class T>
T remainder_count(T l, T r, T a, T b, T m) {
    assert(l >= 0);
    assert(m >= 1);

    if (l >= r || a >= b) return 0;
    if (m <= a || b < 0) return 0;
    chmax(a, T(0));
    chmin(b, m);

    auto res = internal::remainder_count(r, b, m);
    if (l >= 1) res -= internal::remainder_count(l, b, m);
    if (a >= 1) res -= internal::remainder_count(r, a, m);
    if (l >= 1 && a >= 1) res += internal::remainder_count(l, a, m);

    return res;
}

}  // namespace rklib


using namespace std;
using namespace rklib;

using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

int main() {
    int n;
    lint p, q;
    cin >> n >> p >> q;
    vector<lint> c(n + 1);
    repin(i, 1, n) cin >> c[i];

    vector<lint> a;
    repin(i, 1, n) a.push_back(c[i] + p * i);
    sort(a.begin(), a.end());

    lint csum = 0;
    rep(i, 1, n + 1) csum += c[i];

    lint ans = csum;
    repin(k, 1, n) {
        csum -= a[k - 1];
        lint tmp = csum + p * (lint(k) * k + q * k);
        chmax(ans, tmp);
    }
    cout << ans << "\n";
}
accuracy: 1
time_ms: 110
memory_kb: 13232
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using ull = unsigned long long;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
template<class T> using pqmin = priority_queue<T, vector<T>, greater<T>>;
template<class T> using pqmax = priority_queue<T>;
const ll inf=LLONG_MAX/3;
const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se secondf
#define all(x) x.begin(),x.end()
#define si(x) ll(x.size())
#define rep(i,n) for(ll i=0;i<n;i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define rng(i,l,r) for(ll i=l;i<r;i++)
#define gnr(i,l,r) for(ll i=r-1;i>=l;i--)
#define fore(i, a) for(auto &&i : a)
#define fore2(a, b, v) for(auto &&[a, b] : v)
#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)
template<class T> bool chmin(T& a, const T& b){ if(a <= b) return 0; a = b; return 1; }
template<class T> bool chmax(T& a, const T& b){ if(a >= b) return 0; a = b; return 1; }
template<class T, class U> bool chmin(T& a, const U& b){ return chmin(a, (T)b); }
template<class T, class U> bool chmax(T& a, const U& b){ return chmax(a, (T)b); }
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define VLL(name,size) vector<ll>name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>> name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>> name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>> name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define SUM(...) accumulate(all(__VA_ARGS__),0LL)
template<class T> auto min(const T& a){ return *min_element(all(a)); }
template<class T> auto max(const T& a){ return *max_element(all(a)); }
template<class T, class F = less<>> void sor(T& a, F b = F{}){ sort(all(a), b); }
template<class T> void uniq(T& a){ sor(a); a.erase(unique(all(a)), end(a)); }
void outb(bool x){cout<<(x?"Yes":"No")<<"\n";}
ll max(int x, ll y) { return max((ll)x, y); }
ll max(ll x, int y) { return max(x, (ll)y); }
int min(int x, ll y) { return min((ll)x, y); }
int min(ll x, int y) { return min(x, (ll)y); }
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))
ll gcd(ll a,ll b){return (b?gcd(b,a%b):a);}
vector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }
vector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); per(i,ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }
vll prime_table(ll n){vec(ll,isp,n+1,1);vll res;rng(i,2,n+1)if(isp[i]){res.pb(i);for(ll j=i*i;j<=n;j+=i)isp[j]=0;}return res;}

template <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }
template <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }
template <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }
template <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }
template <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }
template <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }
template <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }

template <class T> vector<T> &operator++(vector<T> &v) {
		fore(e, v) e++;
		return v;
}
template <class T> vector<T> operator++(vector<T> &v, int) {
		auto res = v;
		fore(e, v) e++;
		return res;
}
template <class T> vector<T> &operator--(vector<T> &v) {
		fore(e, v) e--;
		return v;
}
template <class T> vector<T> operator--(vector<T> &v, int) {
		auto res = v;
		fore(e, v) e--;
		return res;
}
template <class T> vector<T> &operator+=(vector<T> &l, const vector<T> &r) {
		fore(e, r) l.eb(e);
		return l;
}

template<class... Ts> void in(Ts&... t);
[[maybe_unused]] void print(){}
template<class T, class... Ts> void print(const T& t, const Ts&... ts);
template<class... Ts> void out(const Ts&... ts){ print(ts...); cout << '\n'; }
namespace IO{
#define VOID(a) decltype(void(a))
struct S{ S(){ cin.tie(nullptr)->sync_with_stdio(0); fixed(cout).precision(12); } }S;
template<int I> struct P : P<I-1>{};
template<> struct P<0>{};
template<class T> void i(T& t){ i(t, P<3>{}); }
void i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }
template<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }
template<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }
template<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){ in(get<idx>(t)...); }
template<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ ituple(t, make_index_sequence<tuple_size<T>::value>{}); }
template<class T> void o(const T& t){ o(t, P<4>{}); }
template<size_t N> void o(const char (&t)[N], P<4>){ cout << t; }
template<class T, size_t N> void o(const T (&t)[N], P<3>){ o(t[0]); for(size_t i = 1; i < N; i++){ o(' '); o(t[i]); } }
template<class T> auto o(const T& t, P<2>) -> VOID(cout << t){ cout << t; }
template<class T> auto o(const T& t, P<1>) -> VOID(begin(t)){ bool first = 1; for(auto&& x : t) { if(first) first = 0; else o(' '); o(x); } }
template<class T, size_t... idx> void otuple(const T& t, index_sequence<idx...>){ print(get<idx>(t)...); }
template<class T> auto o(T& t, P<0>) -> VOID(tuple_size<T>{}){ otuple(t, make_index_sequence<tuple_size<T>::value>{}); }
#undef VOID
}
#define unpack(a) (void)initializer_list<int>{(a, 0)...}
template<class... Ts> void in(Ts&... t){ unpack(IO::i(t)); }
template<class T, class... Ts> void print(const T& t, const Ts&... ts){ IO::o(t); unpack(IO::o((cout << ' ', ts))); }
#undef unpack

int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	LL(n,p,q);
	VEC(ll,c,n);
	ll base=0;
	vll d;
	rep(i,n){
		base+=p*(q-i);
		d.pb(c[i]-p*(q-i));
	}
	sor(d);
	ll ans=p*(n-1)*n;
	ll sd=0;
	per(k,n){
		sd+=d[k];
		chmax(ans,sd+p*(k-1)*k);
	}
	ans+=base;
	out(ans);
}
accuracy: 1
time_ms: 50
memory_kb: 13144
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
#define vec vector
constexpr ll inf=1e18;
using H=pair<ll,ll>;
#define fs first
#define sc second
class Segtree{
    //一点更新、区間加算、区間最大値のインデックス
    public:
    struct st{
        ll x,v,lz;
    };
    vec<st>dat;
    int n,rr;
    Segtree(int n):n(n){
        rr=1;while(rr<n) rr*=2;
        dat.resize(2*rr);
        for(int i=0;i<rr;i++){
            dat[i+rr]=st{i,-inf,0};
        }
    }
    void update(int i,ll x){
        update_(1,0,rr,i,i+1,x);
    }
    void add(int l,int r,ll x){
        add_(1,0,rr,l,r,x);
    }
    H query(int l,int r){
        st ret=query_(1,0,rr,l,r);
        return H{ret.x,ret.v};
    }
    void update_(int i,int l,int r,int a,int b,ll x){
        if(b<=l||r<=a) return;
        else if(a<=l&&r<=b){
            dat[i].lz=0;
            dat[i].v=x;
            return;
        }
        dat[i*2].v+=dat[i].lz;
        dat[i*2+1].v+=dat[i].lz;
        dat[i*2].lz+=dat[i].lz;
        dat[i*2+1].lz+=dat[i].lz;
        dat[i].lz=0;
        update_(i*2,l,(l+r)>>1,a,b,x);
        update_(i*2+1,(l+r)>>1,r,a,b,x);
        if(dat[i*2].v>dat[i*2+1].v){
            dat[i].v=dat[i*2].v;
            dat[i].x=dat[i*2].x;
        }
        else{
            dat[i].v=dat[i*2+1].v;
            dat[i].x=dat[i*2+1].x;
        }
    }
    void add_(int i,int l,int r,int a,int b,ll x){
        if(b<=l||r<=a) return;
        else if(a<=l&&r<=b){
            dat[i].lz+=x;
            dat[i].v+=x;
            return;
        }
        dat[i*2].v+=dat[i].lz;
        dat[i*2+1].v+=dat[i].lz;
        dat[i*2].lz+=dat[i].lz;
        dat[i*2+1].lz+=dat[i].lz;
        dat[i].lz=0;
        add_(i*2,l,(l+r)>>1,a,b,x);
        add_(i*2+1,(l+r)>>1,r,a,b,x);
        if(dat[i*2].v>dat[i*2+1].v){
            dat[i].v=dat[i*2].v;
            dat[i].x=dat[i*2].x;
        }
        else{
            dat[i].v=dat[i*2+1].v;
            dat[i].x=dat[i*2+1].x;
        }
    }
    st query_(int i,int l,int r,int a,int b){
        if(b<=l||r<=a) return st{0,-inf,0};
        else if(a<=l&&r<=b){
            return dat[i];
        }
        dat[i*2].v+=dat[i].lz;
        dat[i*2+1].v+=dat[i].lz;
        dat[i*2].lz+=dat[i].lz;
        dat[i*2+1].lz+=dat[i].lz;
        dat[i].lz=0;
        st one=query_(i*2,l,(l+r)>>1,a,b);
        st two=query_(i*2+1,(l+r)>>1,r,a,b);
        if(one.v>two.v){
            return one;
        }
        else{
            return two;
        }
    }
};
class BIT{
    public:
    int n;
    vec<ll>dat;
    BIT(int n):n(n){
        dat.assign(n+2,0);
    }
    void add(int i,ll x){
        i+=1;
        while(i<=n){
            dat[i]+=x;
            i+=(i&-i);
        }
    }
    ll query(int l,int r){
        return query_(r-1)-query_(l-1);
    }
    ll query_(int x){
        x+=1;
        ll sum=0;
        while(x>0){
            sum+=dat[x];
            x-=(x&-x);
        }
        return sum;
    }
};
signed main(){
    int n;ll p,q;
    cin>>n>>p>>q;
    vec<ll>c(n);
    for(int i=0;i<n;i++) cin>>c[i];
    Segtree seg(n);
    for(int i=0;i<n;i++){
        seg.update(i,p*(q-i)-c[i]);
    }
    ll cur=0;
    for(int i=0;i<n;i++) cur+=c[i];
    ll ans=cur;
    BIT bit(n);
    for(int i=0;i<n;i++){
        H k=seg.query(0,n);
        seg.update(k.fs,-inf);
        seg.add(k.fs+1,n,2*p);
        seg.add(0,k.fs,2*p);
        cur+=k.sc;
        ans=max(ans,cur);
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 520
memory_kb: 35492
final_rank: 17