#include<bits/stdc++.h> using namespace std; using ll=long long; #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;} template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;} const ll INF=1LL<<60; void Solve(){ int N,K; cin>>N>>K; vector<ll>A(N); for(int i=0;i<N;i++)cin>>A[i]; if(K==1){ ll sum=0,ans=-INF; for(int i=0;i<N;i++){ sum+=A[i]; chmax(ans,sum); } cout<<ans<<"\n"; return; } ll pf=0,sum=0; priority_queue<ll>pq; ll ans=-INF; for(int i=0;i<N;i++){ if(pq.size()<K-1){ pf+=A[i]; sum+=pf; pq.push(pf); continue; } chmax(ans,K*(pf+A[i])-sum); pf+=A[i]; if(pf<pq.top()){ sum-=pq.top(); pq.pop(); pq.push(pf); sum+=pf; } } cout<<ans<<"\n"; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); Solve(); }