欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1654A.Maximum Cake Tastiness
Maximum Cake Tastiness
There are pieces of cake on a line. The -th piece of cake has weight ().
The tastiness of the cake is the maximum total weight of two adjacent pieces of cake (i. e., $\max(a_1+a_2,\, a_2+a_3,\, \ldots,\, a_{n-1} + a_{n})$).
You want to maximize the tastiness of the cake. You are allowed to do the following operation at most once (doing more operations would ruin the cake):
- Choose a contiguous subsegment of pieces of cake (), and reverse it.
The subsegment of the array is the sequence .
If you reverse it, the array will become $a_1, a_2, \dots, a_{l-2}, a_{l-1}, \underline{a_r}, \underline{a_{r-1}}, \underline{\dots}, \underline{a_{l+1}}, \underline{a_l}, a_{r+1}, a_{r+2}, \dots, a_{n-1}, a_n$.
For example, if the weights are initially , you can reverse the subsegment , getting $[5, \underline{7}, \underline{4}, \underline{1}, \underline{2}, 3]$. The tastiness of the cake is now (while before the operation the tastiness was ).
Find the maximum tastiness of the cake after doing the operation at most once.
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 number of pieces of cake.
The second line of each test case contains integers () — is the weight of the -th piece of cake.
Output
For each test case, print a single integer: the maximum tastiness of the cake after doing the operation at most once.
Note
In the first test case, after reversing the subsegment , you get a cake with weights $[5, \underline{7}, \underline{4}, \underline{1}, \underline{2}, 3]$. The tastiness of the cake is now . This is the maximum possible tastiness of the cake one can obtain by performing the operation at most once.
In the second test case, it's optimal not to do any operation. The tastiness is .
In the third test case, after reversing the subsegment , you get a cake with weights . The tastiness of the cake is now . There is no way to make the tastiness of the cake greater than by performing at most one operation.
Samples
5
6
5 2 1 4 7 3
3
32 78 78
3
69 54 91
8
999021 999021 999021 999021 999652 999021 999021 999021
2
1000000000 1000000000
12
156
160
1998673
2000000000
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |