Weird Sum

3.7

41 votes
2D dynamic programming, Algorithms, Dynamic Programming
Problem

We are given N numbers. We have to select K of them. We also have a number M. Suppose the numbers we selected were A1,A2,A3..Ak. (note that we write these numbers in order in which they appeared in the original array). Define S=ki=1Ai(imodM).
We have to maximize S.

Constraints : 

 N104
 K103
 |Ai|107
 M107

Input Format : 

 In the first line, three space separated integers N,K,M are given. In the next line, N space separated integers A1,A2,A3,An are given.

Output Format :

Print a single integer corresponding to the answer. 

 

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

9*1 + 8*2 + 2*0 + 7*1 = 32

Editor Image

?