欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1231C.Increasing Matrix
Increasing Matrix
In this problem, a rectangular matrix is called increasing if, for each row of , when go from left to right, the values strictly increase (that is, ) and for each column , when go from top to bottom, the values strictly increase (that is, ).
In a given matrix of non-negative integers, it is necessary to replace each value of with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.
It is guaranteed that in a given value matrix all values of are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
Input
The first line contains integers and () — the number of rows and columns in the given matrix .
The following lines contain each of non-negative integers — the values in the corresponding row of the given matrix: ().
It is guaranteed that for all , and are true.
Output
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
Note
In the first example, the resulting matrix is as follows:
1 3 5 6 7
3 6 7 8 9
5 7 8 9 10
8 9 10 11 12
In the second example, the value must be put in the middle cell.
In the third example, the desired resultant matrix does not exist.
Samples
4 5
1 3 5 6 7
3 0 7 0 9
5 0 0 0 10
8 9 10 11 12
144
3 3
1 2 3
2 0 4
4 5 6
30
3 3
1 2 3
3 0 4
4 5 6
-1
3 3
1 2 3
2 3 4
3 4 2
-1
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |