define _CRT_SECURE_NO_WARNI NGS include iostream include vector includ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#define ll long long
using namespace std;
ll a, b;
ll f(const vector<ll>& g) {
int n = g.size();
if (n == 1)
return 0;
if (n == 2)
return max(0ll, g[0] + g[1]);
vector<ll> dp(n, 0);
dp[1] = max(0ll, g[0] + g[1]);
for (int i = 2; i < n; ++i) {
dp[i] = max(dp[i - 1], dp[i - 2] + g[i] + g[i - 1]);
}
return dp[n - 1];
}
int main() {
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n, k;
cin >> n >> k;
vector<vector<ll> > g(k);
for (int i = 0; i < n; ++i) {
ll q;
cin >> q;
g[i % k].push_back(q);
}
ll ans = 0;
for (int i = 0; i < k; ++i)
ans += f(g[i]);
cout << ans << endl;
return 0;
}