首先特判掉 w<0 、 w>∑ni 、 n=1 的情况。然后一定是合法的。
不妨升序排列,按照 w 和
如果 n1≤w 比较简单,先选 1 到只剩两次,这时
如果 n1>w ,直接选 1 的话就超了
#include<cstdio>
#include<algorithm>
using namespace std;
int a[110],b[110];
int cmp(int x,int y)
{return a[x]<a[y];
}
int main()
{//freopen("in.txt","r",stdin);int s=0,w,n,y;scanf("%d%d",&n,&w);for (int i=1;i<=n;++i) scanf("%d",&a[i]);for (int i=1;i<=n;++i) b[i]=i;sort(b+1,b+n+1,cmp);for (int i=1;i<=n;++i) s+=a[i];if (w<0||w>s||(w==1&&a[b[1]]>1)){printf("No\n");return 0;}if (n==1){if (w==a[1]){printf("Yes\n");for (int i=1;i<=2*a[1];++i) printf("1\n");}else printf("No\n");return 0;}printf("Yes\n");if (a[b[1]]<=w){for (int i=1;i<=2*(a[b[1]]-1);++i) printf("%d ",b[1]);y=a[b[1]]-1;if (y==w-1) printf("%d ",b[1]);for (int i=2;i<=n;++i)while (a[b[i]]--){printf("%d %d ",b[i],b[i]);y++;if (y==w-1) printf("%d ",b[1]);}printf("%d\n",b[1]);}else{printf("%d ",b[2]);for (int i=1;i<=2*(a[b[1]]-1);++i) printf("%d ",b[1]);y=1;printf("%d ",b[2]);if (y==w-1) printf("%d ",b[1]);for (int i=1;i<=a[b[2]]-1;++i){printf("%d %d ",b[2],b[2]);y++;if (y==w-1) printf("%d ",b[1]);}for (int i=3;i<=n;++i)while (a[b[i]]--) printf("%d %d ",b[i],b[i]);printf("%d ",b[1]);}
}