Explanation
Let represent whether it's possible to select a group of coins such that a subset of them sum to and the rest of them in the group sum to . This way we'll have coins whose sum is , and we're interested in cases where .
Our base case is representing an empty selection. Now for each coin, we can update our DP array by considering two possibilities:
- Include the coin in Arya's part
- Include the coin in the rest of the selection
Finally we collect all the values such that is true which are eventually the values Arya can form.
Implementation
Time Complexity:
C++
#include <iostream>#include <vector>int main() {int n, k;std::cin >> n >> k;std::vector<int> coins(n);for (int &x : coins) { std::cin >> x; }std::vector dp(k + 1, std::vector<int>(k + 1));
Java
import java.io.*;import java.util.*;public class TheValuesYouCanMake {public static void main(String[] args) {Kattio io = new Kattio();int n = io.nextInt();int k = io.nextInt();int[] coins = new int[n];
Python
n, k = map(int, input().split())coins = list(map(int, input().split()))dp = [[0] * (k + 1) for _ in range(k + 1)]dp[0][0] = 1for a in coins:new_dp = [x[:] for x in dp]for i in range(k + 1):for j in range(k - i + 1):
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!