欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF2171B.Yuu Koito and Minimum Absolute Sum
Yuu Koito and Minimum Absolute Sum
The words in shoujo manga and love songs... they're always sparkling brightly. I don't need a dictionary to understand the meaning... but I've never felt them for myself.— Yuu Koito
Yuu is trying out the student council! Unfortunately, she is being forced to do clerical work... Touko wants her to fill out the blanks in various student council documents.
You are given a partially filled array of nonnegative integers , where blank elements are denoted with . You would like to fill in the blank elements with nonnegative integers, such that the absolute value of the sum of the elements in its difference array is minimized.
More formally, let be the array of length such that for all . Find the minimum possible value of , across all possible ways to fill in the blank elements of .
Additionally, output the array that achieves this minimum. If there are multiple such arrays, output the one that is lexicographically smallest.
For two arbitrary arrays and of length , we say that is lexicographically smaller than if there exists an index () such that for all , and . In other words, and differ in at least one index, and at the first index at which they differ, is smaller than .
Input
The first line contains a single integer () — the number of test cases.
The first line of each test case contains a single integer ().
The second line of each test case contains integers, ().
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, on the first line, output the minimum possible value of . Then, on the second line, output integers, the values of in the lexicographically smallest array achieving this minimum.
Note
In the first example, we fill in the array , which yields the difference array .
The absolute value of the sum of the elements in is . It can be proven that this is the minimum possible. Furthermore, it can be proven that this is the lexicographically smallest array that achieves this minimum.
Samples
6
4
2 -1 7 1
4
-1 2 4 -1
8
2 -1 1 5 11 12 1 -1
3
-1 -1 -1
3
2 5 4
2
-1 5
1
2 0 7 1
0
0 2 4 0
0
2 0 1 5 11 12 1 2
0
0 0 0
2
2 5 4
0
5 5
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |