2 条题解
-
0Guest LV 0
-
1
#include <algorithm> #include <cstdio> #include <cctype> #include <deque> using namespace std; namespace FastIO { char buf[1<<21], *p1=buf, *p2=buf; void read () {} inline int getch (void) { return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++; } template <typename T, typename... G> void read (T &x, G &... o) { int f = 1, ch = getch(); x = 0; while(!isdigit(ch)) { if(ch == '-') f = -f; ch = getch(); } while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getch(); } x = x * f; read(o...); } } #define read FastIO::read struct node { int id; long long x; }; deque < node > q; const int maxn = 1000005; int n, m, a[maxn]; long long f[maxn]; int main (void) { read(n,m); for(register int i=1; i<=n; ++i) read(a[i]); for(register int i=1; i<=m; ++i) { f[i] = a[i]; while (!q.empty() && q.back().x>f[i]) q.pop_back(); q.push_back((node){i, f[i]}); } for(register int i=m+1; i<=n; ++i) { while (!q.empty() && q.front().id+m<i) q.pop_front(); f[i] = q.front().x + a[i]; while (!q.empty() && q.back().x>f[i]) q.pop_back(); q.push_back((node){i, f[i]}); } long long ans = 1ll<<60; for(register int i=n; i>=n-m+1; --i) ans = min(ans, f[i]); printf("%lld\n", ans); return 0; }
- 1