欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1534B.Histogram Ugliness
Histogram Ugliness
Little Dormi received a histogram with bars of height for Christmas. However, the more he played with his new histogram, the more he realized its imperfections, so today he wanted to modify it to his liking.
To modify the histogram, Little Dormi is able to perform the following operation an arbitrary number of times:
- Select an index () where , and assign .
Little Dormi defines the ugliness score of his histogram (after performing some number of operations) as the sum of the vertical length of its outline and the number of operations he performed on it. And to make the histogram as perfect as possible, he would like to minimize the ugliness score after modifying it with some number of operations.
However, as his histogram is very large, Little Dormi is having trouble minimizing the ugliness score, so as Little Dormi's older brother, help him find the minimal ugliness.
Consider the following example where the histogram has columns of heights :

The blue region represents the histogram, and the red lines represent the vertical portion of the outline. Currently, the vertical length of the outline is , so if Little Dormi does not modify the histogram at all, the ugliness would be .
However, Little Dormi can apply the operation once on column and twice on column , resulting in a histogram with heights :

Now, as the total vertical length of the outline (red lines) is , the ugliness is dollars. It can be proven that this is optimal.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). Description of the test cases follows.
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 output one integer, the minimal ugliness Little Dormi can achieve with the histogram in that test case.
Note
Example is the example described in the statement.
The initial histogram for example is given below:

The ugliness is currently .
By applying the operation once on column , six times on column , and three times on column , we can end up with a histogram with heights :

The vertical length of the outline is now and Little Dormi made operations, so the final ugliness is , which can be proven to be optimal.
Samples
2
4
4 8 9 6
6
2 1 7 4 0 0
17
12
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |