All problems

Problem 968

Partition to K Equal Sum Subsets

Medium · 20 pts · Array, Backtracking, Bitmask, Dynamic Programming

Problem

Decide whether the array can be split into K non-empty groups with equal sums, using every element exactly once. Each group must sum to total / K, so that must be a whole number and no element may exceed it. With at most 16 elements, use a bitmask DP: for every subset of used elements, store the current partial sum of the group being filled (mod the target), and extend it by any unused element that still fits.

Input

The first line contains N and K. The second line contains the N positive integers.

Output

Print YES if such a partition exists, otherwise NO.

Constraints

1 <= K <= N <= 16; 1 <= A[i] <= 10^4

Example input

7 4
4 3 2 3 5 2 1

Example output

YES

Explanation

4 3 2 3 5 2 1 sums to 20, and with K = 4 each group must sum to 5: (5), (1, 4), (2, 3) and (2, 3) works, so the answer is YES.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Number of Music Playlists Next: Increasing Triplet Subsequence