当前位置: 首页 > news >正文

Codeforces Round 1023 (Div. 2) C. Maximum Subarray Sum

Codeforces Round 1023 (Div. 2) C. Maximum Subarray Sum

题目

You are given an array a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1,a2,,an of length n n n and a positive integer k k k, but some parts of the array a a a are missing. Your task is to fill the missing part so that the maximum subarray sum ∗ ^{\text{∗}} of a a a is exactly k k k, or report that no solution exists.

Formally, you are given a binary string s s s and a partially filled array a a a, where:

  • If you remember the value of a i a_i ai, s i = 1 s_i = 1 si=1 to indicate that, and you are given the real value of a i a_i ai.
  • If you don’t remember the value of a i a_i ai, s i = 0 s_i = 0 si=0 to indicate that, and you are given a i = 0 a_i = 0 ai=0.

All the values that you remember satisfy ∣ a i ∣ ≤ 10 6 |a_i| \le 10^6 ai106. However, you may use values up to 10 18 10^{18} 1018 to fill in the values that you do not remember. It can be proven that if a solution exists, a solution also exists satisfying ∣ a i ∣ ≤ 10 18 |a_i| \le 10^{18} ai1018.

∗ ^{\text{∗}} The maximum subarray sum of an array a a a of length n n n, i.e. a 1 , a 2 , … a n a_1, a_2, \ldots a_n a1,a2,an is defined as max ⁡ 1 ≤ i ≤ j ≤ n S ( i , j ) \max_{1 \le i \le j \le n} S(i, j) max1ijnS(i,j) where S ( i , j ) = a i + a i + 1 + … + a j S(i, j) = a_i + a_{i + 1} + \ldots + a_j S(i,j)=ai+ai+1++aj.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 10 4 1 \le t \le 10^4 1t104). The description of the test cases follows.

The first line of each test case contains two numbers n , k n,k n,k ( 1 ≤ n ≤ 2 ⋅ 10 5 , 1 ≤ k ≤ 10 12 1 \le n \le 2 \cdot 10^5,1 \le k \le 10^{12} 1n2105,1k1012).

The second line of each test case contains a binary ( 01 \texttt{01} 01) string s s s of length n n n.

The third line of each test case contains n n n numbers a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1,a2,,an ( ∣ a i ∣ ≤ 10 6 |a_i| \le 10^6 ai106). If s i = 0 s_i = \texttt{0} si=0, then it’s guaranteed that a i = 0 a_i = 0 ai=0.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 10 5 2 \cdot 10^5 2105.

Output

For each test case, first output Yes \texttt{Yes} Yes if a solution exists or No \texttt{No} No if no solution exists. You may print each character in either case, for example YES \texttt{YES} YES and yEs \texttt{yEs} yEs will also be accepted.

If there’s at least one solution, print n n n numbers a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1,a2,,an on the second line. ∣ a i ∣ ≤ 10 18 |a_i| \le 10^{18} ai1018 must hold.

Example

Input

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

Output

Yes
4 0 1
Yes
4 -3 5 -2 1
Yes
2 2 -4 -5
No
Yes
5 1 9 2 2
Yes
-8 6 6 7 -5
Yes
10 -20 10 -20 10
No
Yes
3 -1 3
Yes
-2 4 1 -5

Note

In test case 1 1 1, only the first position is not filled. We can fill it with 4 4 4 to get the array [ 4 , 0 , 1 ] [4, 0, 1] [4,0,1] which has maximum subarray sum of 5 5 5.

In test case 2 2 2, only the third position is not filled. We can fill it with 5 5 5 to get the array [ 4 , − 3 , 5 , − 2 , 1 ] [4, -3, 5, -2, 1] [4,3,5,2,1]. Here the maximum subarray sum comes from the subarray [ 4 , − 3 , 5 ] [4, -3, 5] [4,3,5] and it is 6 6 6, as required.

In test case 3 3 3, the first and second positions are unfilled. We can fill both with 2 2 2 to get the array [ 2 , 2 , − 4 , − 5 ] [2, 2, -4, -5] [2,2,4,5] which has a maximum subarray sum of 4 4 4. Note that other outputs are also possible such as [ 0 , 4 , − 4 , − 5 ] [0, 4, -4, -5] [0,4,4,5].

In test case 4 4 4, it is impossible to get a valid array. For example, if we filled the third position with 0 0 0, we get [ 1 , 2 , 0 , 5 , − 1 , 9 ] [1, 2, 0, 5, -1, 9] [1,2,0,5,1,9], but this has a maximum subarray sum of 16 16 16, not 12 12 12 as required.

题目解析及思路

题目要求判断未确定的数组是否能使其最大子数组和恰好=k

对于二进制串s,如果si = 0,则说明ai可以调整

首先考虑把所有不确定值全部赋成-INF,求一遍最大子数组和,如果已经>k 说明没有构造方案,否则实际只需要考虑一个不确定位置即可(其他依旧-INF)

因为可以调到很大值,使最大子数组和=k

代码

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;void solve(){int n,k;string s;cin>>n>>k>>s;vector<int> a(n);for(int &x:a) cin>>x;int pos = -1;int mx = 0,cur = 0;for(int i=0;i<n;i++){if(s[i] == '0'){//只留最后一个就可以了pos = i;a[i] = -1e13;}}for(int i=0;i<n;i++){cur = max(cur+a[i],a[i]);mx = max(mx,cur);}//如果已经>k或者不为k但是没有不确定的位置//都没有构造方案if(mx > k || (mx != k && pos == -1)){cout<<"NO"<<endl;return;}if(pos != -1){mx = 0,cur = 0;//对于提前找到的不确定位置//求他两边的最大子数组和for(int i=pos+1;i<n;i++){cur += a[i];mx = max(mx,cur);}//左边最大和int l = mx;mx = 0,cur = 0;for(int i=pos-1;i>=0;i--){cur += a[i];mx = max(mx,cur);}//右边最大和int r = mx;//构造的值a[pos] = k - l - r;}cout<<"YES"<<endl;for(int i=0;i<n;i++){cout<<a[i]<<" ";}cout<<endl;}
signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int t;cin>>t;while(t--){solve();}
}
http://www.xdnf.cn/news/1015813.html

相关文章:

  • 2025秋招后端突围:JVM核心面试题与高频考点深度解析
  • 电脑在使用过程中频繁死机怎么办
  • Java并发编程实战 Day 21:分布式并发控制
  • 华为云Flexus+DeepSeek征文 | 基于Dify构建个人在线旅游助手
  • 《AI日报 · 0613|ChatGPT支持导出、Manus免费开放、GCP全球宕机》
  • 常用的排序算法
  • UDS协议中0x31服务(Routine Control)详解及应用
  • AI 重构的陷阱:如何避免旧项目越改越烂?
  • 从弦到膜:在1D和2D云环境中探索波动方程-AI云计算数值分析和代码验证
  • SpringBoot的5种签到打卡实现方案(完整版)
  • 红帽认证工程师(RHCE):掌握Linux自动化的关键
  • 浅谈为windows7平台打包基于pyside6的UI程序
  • AD工程面板拖动以及固定位置
  • 通过XML方式在Word段落前添加空白段落
  • “交错推理”降低首token耗时,并且显著提升推理准确性!!
  • DMC-E 系列总线控制卡----雷赛板卡介绍(五)
  • 组合模式深度解析:Java设计模式实战指南与树形结构处理架构设计
  • 在ros中动态调整雷达,线激光雷达等设备的静态坐标关系
  • NaluCFD 介绍和使用指南
  • 复习embedding编码范式及理解代理Agentic RAG及传统RAG的区别
  • 【leetcode】101. 对称二叉树
  • 编译,多面体库
  • Java SE(13)——工具类
  • 基于深度学习的智能语音合成系统:技术与实践
  • Android中的DX、D8、R8
  • HTML5实现好看的邀请函网页源码
  • 1.13使用 Node.js 操作 SQLite
  • 7. TypeScript接口
  • gazebo仿真中对无人机集成的相机进行标定(VINS-Fusion)
  • 西电新增信息力学与感知学院,26考研正式招生