#pragma GCC optimize("Ofast") #pragma GCC target("avx2") char rbuf[1<<14]; #define rd(v) long v=0;{int _c;while(_c=*rp++-48,_c>=0)v=v*10+_c;} #define wt(v) {long _z=v;do*--wp=_z%10+48;while(_z/=10);} #define rep(v,e) for(long v=0;v=s;) #define chmin(v,a) (v=v<=a?v:a) #define LIM 5001 int dp[LIM]; main(){ rep(i,LIM){ dp[i]=LIM; } dp[0]=0; read(0,rbuf,sizeof rbuf); char*rp=rbuf; rd(n); long n0=0,n1=0,s=0,c; do{ c=*rp; rp+=2; if(c=='0'){ ++n0; }else if(c=='1'){ ++n1; }else{ if(n0+n1){ s+=n1; n1+=n0; n-=n0+1; rrep3(j,n1,n+2){ chmin(dp[j],dp[j-n1]+n0); } n0=0; n1=0; } } }while(c); long r=dp[s]; if(r>=LIM){ write(1,"-1",2); }else{ char wbuf[64],*wp=wbuf+sizeof wbuf; wt(r); write(1,wp,wbuf+sizeof wbuf-wp); } _exit(0); }