#C459. Subsequence Sum
Subsequence Sum
Subsequence Sum
You are given an array A
of N
integers and an integer K
. Your task is to determine whether there exists a subsequence of A
(not necessarily contiguous) whose sum is exactly K
.
A subsequence is derived from the array by deleting zero or more elements without changing the order of the remaining elements. Use an efficient dynamic programming approach to solve the problem.
The problem can be expressed mathematically as finding a subset \( S \subseteq \{1,2,\ldots,N\} \) such that:
\[ \sum_{i \in S} A_i = K \]inputFormat
The first line contains two integers N
and K
separated by a space.
The second line contains N
space-separated integers, representing the array A
.
outputFormat
Output a single line containing YES
if there exists a subsequence of A
that sums to K
. Otherwise, output NO
.
5 9
1 2 3 4 5
YES