#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2.5e6 + 10;
const ll inf = 1e18, mod = 1e9 + 7;
ll a[maxn], dp[maxn], to[maxn];
int solve(int n, int k, int *S)
{
for (int i = 0; i < n; i ++)
a[i + 1] = S[i];
stack < int > sc;
for (int i = 1; i <= n; i ++)
{
while(!sc.empty() && a[sc.top()] < a[i])
{
to[sc.top()] = i;
sc.pop();
}
sc.push(i);
}
deque < int > dq;
multiset < int > mt;
for (int i = 1; i <= n; i ++)
{
while(!dq.empty() && a[dq.back()] <= a[i])
{
ll sum = a[dq.back()];
dq.pop_back();
if (!dq.empty())
{
sum = sum + dp[dq.back()];
mt.erase(mt.find(sum));
}
}
if (!dq.empty())
{
ll sum = dp[dq.back()] + a[i];
mt.insert(sum);
}
dq.push_back(i);
while(!dq.empty() && dq.front() <= i - k)
{
ll sum = dp[dq.front()];
dq.pop_front();
if (!dq.empty())
{
sum = sum + a[dq.front()];
mt.erase(mt.find(sum));
}
}
dp[i] = inf;
if (!mt.empty())
dp[i] = *mt.begin();
dp[i] = min(dp[i], dp[max(0, i - k)] + a[dq.front()]);
}
/**for (int i = 1; i <= n; i ++)
cout << dp[i] << " ";
cout << endl;*/
ll cur = 1, ans = 0;
for (int i = n; i > 0; i --)
{
ans = (ans + (dp[i] % mod) * cur) % mod;
cur = (cur * (ll)23) % mod;
}
return ans;
}
static int s[2500005];
static int n, k;
int main()
{
cin>> n >> k;
for(int i = 0; i < n; i++)
{
cin>> s[i];
}
int ans = solve(n, k, s);
cout<< ans <<"\n";
return 0;
}
Compilation message
/usr/bin/ld: /tmp/ccTpGfau.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccxKWNKv.o:peru.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status