#include #include #define rep(i,n) for(int i=0;i vi; typedef vector vl; typedef vector> vvi; typedef vector> vvl; typedef long double ld; typedef pair P; template ostream& operator<<(ostream& os, const static_modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const dynamic_modint& a) {os << a.val(); return os;} template istream& operator>>(istream& is, static_modint& a) {long long x; is >> x; a = x; return is;} template istream& operator>>(istream& is, dynamic_modint& a) {long long x; is >> x; a = x; return is;} template istream& operator>>(istream& is, vector& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;} template ostream& operator<<(ostream& os, const pair& p){os << p.first << ' ' << p.second; return os;} template ostream& operator<<(ostream& os, const vector& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const vector>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;} template ostream& operator<<(ostream& os, const set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const unordered_set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const atcoder::segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const atcoder::lazy_segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template void chmin(T& a, T b){a = min(a, b);} template void chmax(T& a, T b){a = max(a, b);} // less : #SMALLESTS <= Size // greater : #LARGESTS <= Size template> struct DoubleMultisetSize{ static_assert(is_same>::value || is_same>::value, "U must be either std::greater or std::less"); int k; multiset OK, NG; T sum_OK, sum_NG; DoubleMultisetSize(int k) : k(k), sum_OK(0), sum_NG(0) {}; void balance(){ while(int(OK.size()) > k){ auto it = prev(OK.end()); sum_NG += *it; sum_OK -= *it; NG.insert(*it); OK.erase(it); } while(int(NG.size()) > 0 and int(OK.size()) < k){ auto it = NG.begin(); sum_OK += *it; sum_NG -= *it; OK.insert(*it); NG.erase(it); } } void add(T x){ OK.insert(x); sum_OK += x; balance(); } void del(T x){ if(OK.find(x) == OK.end()){ sum_NG -= x; NG.erase(NG.find(x)); }else{ sum_OK -= x; OK.erase(OK.find(x)); balance(); } }; void change_size(int k_new){ k = k_new; balance(); } pair get_size(){return make_pair(OK.size(), NG.size());} pair get_sum(){return make_pair(sum_OK, sum_NG);} }; int main(){ int n, k; cin >> n >> k; vector a(n); cin >> a; DoubleMultisetSize> dm(k - 1); long long offset = 0; long long ans = -4004004004004004004; rep(i, n){ offset += a[i]; if(i > 0) dm.add(-offset + a[i]); if(i >= k - 1){ long long tmp = dm.get_sum().first; // for(long long x : dm.OK) cout << x << ' '; // cout << "\n"; tmp += offset * k; // cout << tmp << "\n"; chmax(ans, tmp); } } cout << ans << "\n"; return 0; }