EPIC Institute of Technology Round Summer 2025 (Codeforces Round 1036, Div. 1 + Div. 2)
B
Minimise Sum
Codeforces
1000
原题
← AC →
翻译中…

This problem differs from problem G. In this problem, you must output the minimum sum of prefix minimums after at most one operation.

翻译中…

You are given an array $a$ of length $n$, with elements satisfying $\boldsymbol{0 \le a_i \le n}$. You can perform the following operation at most once:

翻译中…
  • Choose two indices $i$ and $j$ such that $i \lt j$. Set $a_i := a_i + a_j$. Then, set $a_j = 0$.
翻译中…

Output the minimum possible value of $\min(a_1) + \min(a_1,a_2) + \ldots + \min(a_1, a_2, \ldots, a_n)$ that you can get.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains an integer $n$ ($2 \leq n \leq 2\cdot 10^5$) — the length of $a$.

翻译中…

The following line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le n$) — denoting the array $a$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output an integer on a new line, the minimum possible value of $\min(a_1) + \min(a_1,a_2) + \ldots + \min(a_1, a_2, \ldots, a_n)$.

翻译中…

Note

翻译中…

In the second test case, it is optimal to perform the operation with $i=2$ and $j=3$.

翻译中…

In the third test case, it is optimal to not perform any operations. The answer is $3$.

翻译中…

样例

Input #1
3
2
1 2
3
1 2 3
4
3 0 2 3
Output #1
2
2
3
InputCopy OutputCopy
题目信息
题号
2124B
时间限制
1.5 s
内存限制
256 MB
标签
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。