e. guess the tree
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
iahub and iahubina went to a picnic in a forest full of trees. less than 5 minutes passed before iahub remembered of trees from programming. moreover, he invented a new problem and iahubina has to solve it, otherwise iahub won't give her the food.
iahub asks iahubina: can you build a rooted tree, such that
each internal node (a node with at least one son) has at least two sons; node i has ci nodes in its subtree? iahubina has to guess the tree. being a smart girl, she realized that it's possible no tree can follow iahub's restrictions. in this way, iahub will eat all the food. you need to help iahubina: determine if there's at least one tree following iahub's restrictions. the required tree must contain n nodes.
input
the first line of the input contains integer n (1?≤?n?≤?24). next line contains n positive integers: the i-th number represents ci (1?≤?ci?≤?n).
output
output on the first line yes (without quotes) if there exist at least one tree following iahub's restrictions, otherwise output no (without quotes).
sample test(s)
input
41 1 1 4
output
yes
input
51 1 5 2 1
output
no
题意:rt
思路:首先注意,根据每个点至少有两个孩子这个性质可以知道,为1的点的数量一定会>=n/2
所以24个点的状态就减少为12个点的状态,敲好可以状压
只考虑不为1的点,先按降序排序,然后一个一个选孩子,如果已经轮到i来选孩子,先检查它自己有没有被前面的点选为孩子,
如果没有选,则不需要继续了,因为它不可能有父亲了
如果选了,则继续为i选2个或以上的孩子,选过的点标记一下就可以了
整个过程用递归算就可以了