#include #include #include #include #include namespace nachia{ template struct DequeOperateAggregation{ private: Val me; std::vector A[2]; std::vector Prod[2]; void recalcProd(){ Prod[0].resize(A[0].size() + 1, me); for(std::size_t i=0; i(A[1].rbegin() + lessHalf, A[1].rend()); A[1] = std::vector(A[1].end() - lessHalf, A[1].end()); recalcProd(); } A[0].pop_back(); Prod[0].pop_back(); } void popBack(){ if(A[1].empty()){ int lessHalf = A[0].size() / 2; A[1] = std::vector(A[0].rbegin() + lessHalf, A[0].rend()); A[0] = std::vector(A[0].end() - lessHalf, A[0].end()); recalcProd(); } A[1].pop_back(); Prod[1].pop_back(); } Val allProd(){ return Prod[0].back() + Prod[1].back(); } }; } // namespace nachia using namespace std; using i64 = long long; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; struct S { i64 a=-INF; int i=-1; }; S operator+(S l, S r){ return l.a > r.a ? l : r; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, K; cin >> N >> K; vector A(N); rep(i,N) cin >> A[i]; vector sumA(N+2); rep(i,N) sumA[i+1] = sumA[i] + A[i]; sumA[N+1] = sumA[N]; nachia::DequeOperateAggregation Q({ -INF, -1 }); Q.pushBack({ -sumA[0] }); Q.pushBack({ -sumA[1] }); i64 ans = 0; rep(i,N){ auto s = Q.allProd(); i64 a = s.a + sumA[i+1]; Q.pushBack({ a - sumA[i+2] }); ans = max(ans, a); if(i >= K-2) Q.popFront(); } cout << ans << '\n'; return 0; }