coin game

【Title description】

Little H takes part in a mysterious game. There are n piles of coins in the game, and the ith pile is worth ai. Each time the small H can choose the coins whose numbers differ by k and take them at the same time. Note that the coins will not be re-marked after they are removed. Little H wants to know the maximum value of coins that can be taken away.

【Input format】

Input file coin.in

The first line contains two integers n,k.

The second line contains n integers. The ith integer represents ai.

【Output format】

Output file coin.out

One integer per line , representing the maximum value of the coin taken.

【Sample input】

7 3

7 5 8 6 4 3 2

【Example output】

33

【data range】

For 20% of the data, n<=20.

For 40% of the data, n<=2000.

For the other 20% of the data, k<=10.

 

If you understand the meaning of the question carefully, you can find that we can divide the serial number of each coin modulo k into k groups, and the coins in the same group will conflict (if some of them are taken, some of them may not be taken),

Coins between different groups will not conflict, so the problem is simplified, let's consider each group, if the number of coins in the group is even, then all the coins in this group can be taken away

(Understand well), if it is an odd number, then there will be one that cannot be taken away. After careful analysis, it can be found that only the odd number in the group can be left. (If the odd number is taken away, then the previous

It will be an odd number that is taken away from the surface, which does not meet the meaning of the question), then the idea is obvious, and the specific reference code is easy to understand.

attached code

 1 #include<Cstdio>
 2 #include<Iostream>
 3 using namespace std;
 4 int n,k,js[100010]; 
 5 long long sum[100010],a[100010],z;
 6 int main()
 7 {
 8     scanf("%d%d",&n,&k);
 9     for(int i=1;i<=n;++i)
10     {
11         scanf("%I64d",&a[i]);
12         z+=a[i];
13     }
14     for(int i=0;i<k;++i)
15     {
16         sum[i]=99999999999;
17     }
18     for(int i=1;i<=n;++i)
19     {
20         js[i%k]++;
21         if(a[i]<sum[i%k]&&js[i%k]%2==1)
22         {
23             sum[i%k]=a[i];
24         }
25     }
26     for(int i=0;i<k;++i)
27     {
28         if(js[i]%2==1)
29         z-=sum[i];
30     }
31     printf("%I64d",z);
32     return 0;
33 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325024630&siteId=291194637