C1. Magnitude (Easy Version)

The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.

You are given an array a� of length n�. Start with c=0�=0. Then, for each i� from 11 to n� (in increasing order) do exactly one of the following:

  • Option 11: set c� to c+ai�+��.
  • Option 22: set c� to |c+ai||�+��|, where |x||�| is the absolute value of x�.

Let the maximum final value of c� after the procedure described above be equal to k�. Find k�.

DeepL 翻译

**两个版本的问题不同。您可能需要同时阅读两个版本。只有两个版本的问题都解决了,你才能进行破解。

给你一个长度为 n� 的数组 a� 。从 c=0�=0 开始。然后,对从 11 到 n� 的每个 i� (依次递增)进行 ** 运算。(按递增顺序)做下面的

  • 方案 11 :将 c� 设置为 c+ai�+�� 。
  • 选项 22 :设置 c� 至 |c+ai||�+��| ,其中 |x||�| 是 x� 的绝对值。

设上述步骤后 c� 的最大终值等于 k� 。求出 k� 。

Input

The first line contains a single integer t� (1≤t≤1041≤�≤104) — the number of test cases.

The first line of each test case contains a single integer n� (2≤n≤2⋅1052≤�≤2⋅105).

The second line of each case contains n� integers a1�1, a2�2, a3�3, ……, an�� (−109≤ai≤109−109≤��≤109).

The sum of n� over all test cases does not exceed 3⋅1053⋅105.

DeepL 翻译

输入

第一行包含一个整数 t� ( 1≤t≤1041≤�≤104 ) - 测试用例数。

每个测试用例的第一行包含一个整数 n� ( 2≤n≤2⋅1052≤�≤2⋅105 )。

每个案例的第二行包含 n� 个整数 a1�1 , a2�2 , a3�3 , …… , an�� ( −109≤ai≤109−109≤��≤109 )( −109≤ai≤109−109≤��≤109 ).

所有测试用例中 n� 的总和不超过 3⋅1053⋅105 。

Output

For each test case, output a single integer — the value of k�.

DeepL 翻译

输出

对于每个测试用例,输出一个整数 - k� 的值。

Example

input

Copy

 

5

4

10 -9 -3 4

8

1 4 3 4 1 4 3 4

3

-1 -2 -3

4

-1000000000 1000000000 1000000000 1000000000

4

1 9 8 4

output

Copy

6
24
6
4000000000
22

Note

In the first test case, if we set c� to its absolute value every time we add to it, we end up with 66. It can be shown that this is the maximum result.

In the second test case, taking the absolute value will never change anything, so we can just sum the array without doing anything to get 2424.

In the third test case, it is optimal to wait until the end to set c� to its absolute value, resulting in an answer of 66.

DeepL 翻译

在第一个测试案例中,如果我们每次都将 c� 设为绝对值,那么最终结果就是 66 。可以证明这是最大结果。

在第二个测试案例中,取绝对值永远不会改变任何结果,因此我们可以不做任何操作,直接对数组求和,得到 2424 。

在第三个测试案例中,我们最好等到最后才将 c� 设为绝对值,这样得到的答案是 66 。

思路:

当前最大值可以由最小值的绝对值或最大值转移过来

当前最小值由遇负数加负数,遇整数加整数,就是最小的,如果都是整数,则最小值即是最大值

代码:

#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
void slove(){
	int n;cin>>n;
	long long a[n+1];
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	long long dp[n+1][2];
	dp[0][1]=dp[0][0]=0;
	for(int i=1;i<=n;++i){
		dp[i][0]=max(abs(a[i]+dp[i-1][0]),abs(dp[i-1][1]+a[i]));//当前最大值会从前一个最小值的绝对值或者前一个最大值转移过来
		dp[i][1]=dp[i-1][1]+a[i];//当前最小值从上一个最小值传递过来
	}
	cout<<dp[n][0]<<endl;//输出最大值
}
int main( )
{
	int T;cin>>T;
	while(T--){
		slove();
	}
    return 0;
}

  • 25
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值