#include <bits/stdc++.h>
#include "peru.h"
// #include "grader.cpp"
using namespace std;
const int N = 2.5e6 + 10, mod = 1e9 + 7;
int n, k, a[N], p[N], ngl[N];
int solve(int nn, int kk, int* v){
n = nn, k = kk;
for (int i = 1; i <= n; i ++)
a[i] = v[i - 1];
stack<int> st;
st.push(0);
for (int i = 1; i <= n; i ++){
while (!st.empty() and st.top() != 0 and a[st.top()] <= a[i])
st.pop();
ngl[i] = st.top();
st.push(i);
}
deque<int> dq1, dq2;
dq1.push_back(0); dq2.push_back(0);
for (int i = 1; i <= n; i ++){
while (!dq1.empty() and a[dq1.back()] <= a[i])
dq1.pop_back();
if (!dq1.empty() and dq1[0] <= i - k)
dq1.pop_front();
dq1.push_back(i);
p[i] = 1e9;
for (int j : dq1)
p[i] = min(p[i], p[max(i - k, ngl[j])] + a[j]);
}
int pw = 1, res = 0;
for (int i = n; i > 0; i --, pw = (23ll * pw) % mod)
res = (res + (1ll * p[i] * pw % mod)) % mod;
return res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |