# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
858859 | alexdumitru | Peru (RMI20_peru) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int solve(int n, int k, int *s)
{
int deq[n], dp[n];
int l = 0, r = -1, mul = 1, ans = 0;
for(int i = 0; i < n; i++)
{
while(r >= l && s[deq[r]] <= s[i]) r--;
deq[++r] = i;
if(deq[l] <= i - k) l++;
int nextDp = i >= k ? dp[i - k] : 0;
dp[i] = (nextDp + s[deq[l]]) % MOD;
}
for(int i = n - 1; i >= 0; i--)
{
ans = (1LL * ans + 1LL * dp[i] * mul % MOD) % MOD;
mul = (1LL * mul * 23) % MOD;
}
return ans;
}
int main()
{
int n, k;
cin >> n >> k;
int *a = new int[n];
for(int i = 0; i < n; i++)
cin >> a[i];
cout << solve(n, k, a) << '\n';
return 0;
}