An array of numbers separated by space
will be passed as input. A number N is also passed as input. The program has to
print the maximum sum of the numbers in the array which is divisible by N. If
there is no such maximum sum of the numbers, the program should print -1 as
output.
Input
Format:
The first line contains the array of
numbers separated by space. The second line contains the value of N
Boundary
Conditions:
The length of the array of numbers will be from 3 to 200. 1 <= N <= 1000
Output
Format:
The maximum sum of the numbers in the
array that is divisible by N.
Example
Input/Output 1:
Input:
10 20 40 70
3
Output:
120
Explanation: The maximum sum of numbers
that is divisible by 3 is 120 (10+40+70) and hence it is printed as the output.
Solution
1:
a,n= [int(i) for i in input().split()],int(input())
x=sorted([sum(a)-i for i in a if
(sum(a)-i)%n==0])
print(-1 if len(x)==0 else x[-1])
Solution
2:
nm,n,d= [int(i) for i in input().split()],int(input().strip()),[[0]*n]
for i in range(1,len(nm)+1):
t=[]
for j in range(n):
x=(j+n-nm[i-1]%n)%n
if d[i-1][x]%n!=x and x!=0:
t.append(max(d[i-1][x],d[i-1][j]))
else:
t.append(max(d[i-1][x]+nm[i-1],d[i-1][j]))
d.append(t)
if d[-1][0]!=0:
print(d[-1][0])
else:
print('-1')
Solution
3:
import itertools
a,k,l,m=[int(i) for i in
input().split()],int(input()),[],[]
for i in range(1,len(a)+1):
l.append(list(itertools.combinations(a,i)))
for i in l:
for j in i:
s=sum(j)
if s%k==0:
m.append(s)
print(max(m)) if len(m)!=0 else
print('-1')