#K73532. Construct Sum from Custom Dice
Construct Sum from Custom Dice
Construct Sum from Custom Dice
You are given a set of custom dice. Each die is labeled with a positive integer value, and you have an infinite supply of dice for each given face value. Your task is to determine whether it is possible to obtain a specified target sum by rolling any number of these dice and summing the outcomes.
Formally, given a list of n integers \(A = [a_1, a_2, \dots, a_n]\) and an integer target \(T\), determine if there exist non-negative integers \(x_1, x_2, \dots, x_n\) such that:
[ a_1x_1 + a_2x_2 + \dots + a_nx_n = T ]
If such a combination exists, output YES
; otherwise, output NO
.
Examples:
- For
n = 5
, dice faces[1, 2, 3, 5, 7]
andT = 12
, the answer isYES
because \(5+7=12\) or other combinations can work. - For
n = 3
, dice faces[3, 5, 7]
andT = 1
, the answer isNO
since no combination of the given numbers can sum to 1.
inputFormat
The first line contains two space-separated integers n and T — the number of available dice faces and the target sum respectively. The second line contains n space-separated integers representing the values on the custom dice faces.
outputFormat
Output a single line containing either YES
if the target sum can be constructed using any combination of the dice faces, or NO
otherwise.
5 12
1 2 3 5 7
YES