I solve the problem:
According to the given numbers 1 <= n <= 30 and 1 <= w <= 10 9 and a set of numbers 1 <= v 1 , ... v n <= 10 9 find the minimum number k for which the number w can be represented as the sum k numbers from the set {v 1 ... v n }. Each number in the set can be used anytime. It is known that there is a unit in the set and that for any pair of numbers from the set one of them is divided by the other. It is guaranteed that in the optimal answer the number of items does not exceed 10 4 .
Print the number k and the terms themselves.
Sample test data:
4 90 1 2 10 50 Answer:
5 50 10 10 10 10 My version:
def solve_sum(num_array, value): dividers = [i for i in num_array if i <= value] dividers = sorted(dividers, reverse=True) res = [] n = 0 while value: res += [dividers[n]] * (value // dividers[n]) k = (value // dividers[n]) value %= dividers[n] n += 1 return str(n)+' '+" ".join(map(str, res)) results = input().split(' ') results = [int(i) for i in results] value = results[1] num_array = results[2:] print(solve_sum(num_array, value)) However, something is wrong and gives an error:
Failed test #1. Wrong answer Input: 4 90 1 2 10 50 Your output: 2 50 10 10 10 10 Correct output: 5 50 10 10 10 10 What's wrong?
n += k- Edward Izmalkov