ll@n,@W,@d,a[2][5001]{},z=0; a[0...1][1..W]=-ll_inf; rep(n){ ll@t,@w,@v; rrep(i,W+1-w){ a[t][i+w]>?=a[t][i]+v; } } rep(i,W+1){ rep(j,max(0,i-d),min(W-i,i+d)+1){ z>?=a[0][i]+a[1][j]; } } wt(z);