如果∑d≠2n-2说明错了
否则,我们先构造出这棵树的直径,若一个di>2则我们将d-2个节点与其相连,所以answer=n-1-∑(max(2,d[i])-2)
#include<stdio.h>
inline int max(int a,int b){ return a>b?a:b; }
int main(){
int n,d[20],ds=0,m;
scanf("%d",&n);
for(int i=0;i<n;++i) scanf("%d",d+i),ds+=d[i];
if(ds!=(n-1)<<1) return 0&puts("-1"); m=n-1;
for(int i=0;i<n;++i) m-=max(2,d[i])-2;
printf("%d
",m);
}