include iostream include string include queue include cmath include se

 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
#include <iostream>
#include <string>
#include <queue>
#include <cmath>
#include <set>
#include <iomanip>
using namespace std;
int abilt[100001] = {0};
vector<pair<int, pair<int, int> > > pairs;
bool used[100001] = {0};
int main()
{
int n, m;
cin >> n >> m;
for(int i = 0; i < n; ++i)
cin >> abilt[i];
for(int i = 0; i < n; ++i)
{
if(i - m >= 0)
pairs.push_back(make_pair(abilt[i] + abilt[i - m], make_pair(i, i - m)));
if(i + m < n)
pairs.push_back(make_pair(abilt[i] + abilt[i + m], make_pair(i, i + m)));
}
sort(pairs.rbegin(), pairs.rend());
int answer = 0;
for(int i = 0; i < pairs.size(); ++i)
{
if(pairs[i].first > 0 && !used[pairs[i].second.first] && !used[pairs[i].second.second])
{
answer += pairs[i].first;
used[pairs[i].second.first] = 1;
used[pairs[i].second.second] = 1;
}
}
cout << answer;
return 0;
}