https://www.luogu.com.cn/record/126971634
#include <bits/stdc++.h>
#define AC return 0;
using namespace std;
const int maxn = 5e5 + 10;
int n, m, k, c1, c2, m1, m2, p, v[maxn], l, r;
char getc()
{
static char ch[10000000], *s, *t;
return (s == t) && (t = (s = ch) + fread(ch, 1, 10000000, stdin)), s == t ? EOF : *s++;
}
void read(int &x)
{
char ch = getc();
bool f = 0;
x = 0;
while (!isdigit(ch))
{
if (ch == '-')
f = 1;
ch = getc();
}
while (isdigit(ch))
{
x = x * 10 + ch - '0';
ch = getc();
}
f ? x = -x : 0;
}
void read(int &x, int &y, int &z) { read(x), read(y), read(z); }
int w[maxn];
struct node
{
int t, v;
node() {}
node(int t, int v) : t(t), v(v) {}
};
struct que
{
int h = 1, t = 0;
mutable node q[maxn >> 3];
int size()
{
return t - h + 1;
}
void push_back(node x)
{
q[++t] = x;
}
void pop_front()
{
h++;
}
void pop_back()
{
t--;
}
void clear()
{
h = 1, t = 0;
}
void jian_front(int x)
{
q[h].v -= x;
if (q[h].v <= 0)
h++;
}
node &front()
{
return q[h];
}
node &back()
{
return q[t];
}
};
que q;
int f(int x)
{
q.clear();
int ans = x * p, now = x;
for (int i = 1; i <= n; ++i)
{
now -= w[i];
while (now < 0 && (q.size()))
{
int v_tmp = min(-now, q.front().v), t_tmp = q.front().t;
if (i - t_tmp >= m2)
{
now += v_tmp;
q.jian_front(v_tmp);
ans += v_tmp * c2;
}
else if (i - t_tmp >= m1)
{
now += v_tmp;
q.jian_front(v_tmp);
ans += v_tmp * c1;
}
else return 0x3f3f3f3f;
}
q.push_back(node(i, w[i]));
}
return ans;
}
void solve()
{
int mid;
while (l + 1 < r)
{
// mid = (l + r) >> 1;
mid = (rand() % (r - l + 1)) + l;
if (f(mid) < f(mid + 1))
r = mid;
else
l = mid;
}
cout << f(r);
}
void init()
{
read(n, m1, m2);
read(c1, c2, p);
if (m2 < m1)
swap(m1, m2), swap(c1, c2);
if (c1 < c2)
c2 = c1, m2 = m1;
for (int i = 1; i <= n; ++i)
read(w[i]), l = max(l, w[i] - 1), r += w[i];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
init();
solve();
AC
}