fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int Mod=998244353;
  5. const ll INF = -10000000000000;
  6.  
  7. void solve() {
  8. ll n,k;
  9. cin >> n >> k;
  10. string s;
  11. cin >> s;
  12. bool ok = false;
  13. for(int i=0;i<n && !ok;i++) if(s[i]=='0') ok = true;
  14. if(!ok){
  15. vector<ll> a(n);
  16. for(int i=0;i<n;i++) cin >> a[i];
  17. vector<ll> best(n);
  18. best[0]=a[0];
  19. ll res=best[0];
  20. for(int i=1;i<n;i++){
  21. best[i]=max(best[i-1]+a[i],a[i]);
  22. res=max(res,best[i]);
  23. }
  24. if(res==k){
  25. cout << "YES\n";
  26. for(int i=0;i<n;i++) cout << a[i] << " ";
  27. cout << '\n';
  28. return;
  29. }
  30. cout << "NO\n";
  31. return;
  32. }
  33. vector<ll> a(n);
  34. for(int i=0;i<n;i++) {
  35. cin >> a[i];
  36. if(s[i]=='0') a[i]=INF;}
  37. vector<ll> best(n);
  38. best[0]=a[0];
  39. ll res=best[0];
  40. for(int i=1;i<n;i++){
  41. best[i]=max(best[i-1]+a[i],a[i]);
  42. res=max(res,best[i]);
  43. }
  44. if(res>k){
  45. cout << "NO\n";
  46. return;
  47. }
  48. cout << "YES\n";
  49. int pos = 0 ;
  50. for(pos=0;pos<n && s[pos]!='0';pos++);
  51. ll n1 =0 , n2=0;
  52. for(int i=0;i<pos;i++){
  53. n2=max(n1+a[i],a[i]);
  54. n1=n2;
  55. }
  56. n1=max(n1,0LL);
  57. ll post = 0,prefix=0;
  58. for(int i=pos+1;i<n;i++){
  59. prefix+=a[i];
  60. post=max(prefix,post);
  61. }
  62. ll put = k-n1-post;
  63. a[pos]=put;
  64. for(int i=0;i<n;i++) cout << a[i] << " ";
  65. cout << '\n';
  66. }
  67.  
  68. int main(){
  69. ios::sync_with_stdio(false);
  70. cin.tie(nullptr);
  71.  
  72. int t;
  73. cin >> t;
  74. while (t--) solve();
  75.  
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0.01s 5324KB
stdin
10
3 5
011
0 0 1
5 6
11011
4 -3 0 -2 1
4 4
0011
0 0 -4 -5
6 12
110111
1 2 0 5 -1 9
5 19
00000
0 0 0 0 0
5 19
11001
-8 6 0 0 -5
5 10
10101
10 0 10 0 10
1 1
1
0
3 5
111
3 -1 3
4 5
1011
-2 0 1 -5
stdout
YES
4 0 1 
YES
4 -3 5 -2 1 
YES
4 -10000000000000 -4 -5 
NO
YES
19 -10000000000000 -10000000000000 -10000000000000 -10000000000000 
YES
-8 6 13 -10000000000000 -5 
YES
10 -10 10 -10000000000000 10 
NO
YES
3 -1 3 
YES
-2 4 1 -5