#include using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) #define FOR(n) for(int i = 0; i < (int)n; i++) #define repi(i,a,b) for(int i = (int)a; i < (int)b; i++) #define pb push_back #define m0(x) memset(x,0,sizeof(x)) #define fill(x,y) memset(x,y,sizeof(x)) #define bg begin() #define ed end() #define all(x) x.bg,x.ed //#define mp make_pair #define vi vector #define vvi vector #define vll vector #define vvll vector #define vs vector #define vvs vector #define vc vector #define vvc vector #define pii pair #define pllll pair #define vpii vector> #define vpllll vector> #define vpis vector> #define vplls vector> #define vpsi vector> #define vpsll vector> template void chmax(T &a, const T &b) {a = (a > b? a : b);} template void chmin(T &a, const T &b) {a = (a < b? a : b);} using ll = long long; using ld = long double; using ull = unsigned long long; const ll INF = numeric_limits::max() / 2; const ld pi = 3.1415926535897932384626433832795028; const ll mod = 1e9 + 7; int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1}; int dy[] = {0, -1, 0, 1, -1, 1, -1, 1}; #define int long long template struct Abstruct_SegmentTree { function f; const T E; vector dat; int n; Abstruct_SegmentTree(int n_, T e, function F) : E(e), dat(4*n_, e) { f = F; int x = 1; while(x < n_) x *= 2; n = x; } void update(int i, T x) { i += n - 1; dat[i] = x; while(i > 0) { i = (i - 1) / 2; dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } return; } T query(int a, int b) { return query_sub(a, b, 0, 0, n); } T query_sub(int a, int b, int k, int l, int r) { if(b <= l || r <= a) { return E; }else if(a <= l && r <= b) { return dat[k]; }else { T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r); return f(vl, vr); } } inline T operator[](const int i) { return query(i, i+1); } void print() { cout << "{"; for(int i = 0; i < n; i++) { cout << (*this)[i]; if(i != n-1) cout << ", "; } cout << "}"; cout << "\n"; } }; auto f = [](pii p, pii q) -> pii { if(p.first != q.first) { if(p.first > q.first) return p; return q; } if(p.second > q.second) return p; return q; }; void solve() { int n, k; cin >> n >> k; vi a(n); FOR(n) cin >> a[i]; //最初に奇数が乗ったらダメ vector used(n, false); int cnt = 0; int now = 0; int ans = 0; Abstruct_SegmentTree seg(n, {-1, -1}, f); FOR(n) seg.update(i, {a[i], i}); for(int i = 1; i < n-k+1; i++) { if(i&1) { while(cnt < k-1) { pii p = seg.query(i, n); now += p.first; used[p.second] = true; seg.update(p.second, {-1, -1}); cnt++; } if(used[i]) { pii p = seg.query(i, n); now += p.first; used[p.second] = true; seg.update(p.second, {-1, -1}); cnt++; chmax(ans, now); }else { chmax(ans, now + a[i]); } if(used[i]) cnt--, now -= a[i]; }else { if(used[i]) cnt--, now -= a[i]; } } cout << ans << endl; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }