A14905 | Maximum Cake Tastiness
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There are $n$ pieces of cake on a line. The $i$ -th piece of cake has weight $a_i$ ( $1 \leq i \leq n$ ).
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 $a[l, r]$ of pieces of cake ( $1 \leq l \leq r \leq n$ ), and reverse it.
The subsegment $a[l, r]$ of the array $a$ is the sequence $a_l, a_{l+1}, \dots, a_r$ .
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 $[5, 2, 1, 4, 7, 3]$ , you can reverse the subsegment $a[2, 5]$ , getting $[5, \underline{7}, \underline{4}, \underline{1}, \underline{2}, 3]$ . The tastiness of the cake is now $5 + 7 = 12$ (while before the operation the tastiness was $4+7=11$ ).
Find the maximum tastiness of the cake after doing the operation at most once.
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 $a[l, r]$ of pieces of cake ( $1 \leq l \leq r \leq n$ ), and reverse it.
The subsegment $a[l, r]$ of the array $a$ is the sequence $a_l, a_{l+1}, \dots, a_r$ .
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 $[5, 2, 1, 4, 7, 3]$ , you can reverse the subsegment $a[2, 5]$ , getting $[5, \underline{7}, \underline{4}, \underline{1}, \underline{2}, 3]$ . The tastiness of the cake is now $5 + 7 = 12$ (while before the operation the tastiness was $4+7=11$ ).
Find the maximum tastiness of the cake after doing the operation at most once.
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 50$ ) — the number of test cases.
The first line of each test case contains a single integer $n$ ( $2 \le n \le 1000$ ) — the number of pieces of cake.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \leq a_i \leq 10^9$ ) — $a_i$ is the weight of the $i$ -th piece of cake.
The first line of each test case contains a single integer $n$ ( $2 \le n \le 1000$ ) — the number of pieces of cake.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \leq a_i \leq 10^9$ ) — $a_i$ is the weight of the $i$ -th piece of cake.
输出格式
For each test case, print a single integer: the maximum tastiness of the cake after doing the operation at most once.
输入输出样例
输入 #1
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
输出 #1
12 156 160 1998673 2000000000
In the first test case, after reversing the subsegment $a[2, 5]$ , you get a cake with weights $[5, \underline{7}, \underline{4}, \underline{1}, \underline{2}, 3]$ . The tastiness of the cake is now $\max(5+7, 7+4, 4+1, 1+2, 2+3) = 12$ . 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 $78+78 = 156$ .
In the third test case, after reversing the subsegment $a[1, 2]$ , you get a cake with weights $[\underline{54}, \underline{69}, 91]$ . The tastiness of the cake is now $\max(54+69, 69+91) = 160$ . There is no way to make the tastiness of the cake greater than $160$ by performing at most one operation.
In the second test case, it's optimal not to do any operation. The tastiness is $78+78 = 156$ .
In the third test case, after reversing the subsegment $a[1, 2]$ , you get a cake with weights $[\underline{54}, \underline{69}, 91]$ . The tastiness of the cake is now $\max(54+69, 69+91) = 160$ . There is no way to make the tastiness of the cake greater than $160$ by performing at most one operation.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted