Recall that the sequence b is a a subsequence of the sequence a if b can be derived from a by removing zero or more elements without changing the order of the remaining elements. For example, if a=[1,2,1,3,1,2,1], then possible subsequences are: [1,1,1,1], [3] and [1,2,1,3,1,2,1], but not [3,2,3] and [1,1,1,1,2].

You are given a sequence a consisting of n positive and negative elements (there is no zeros in the sequence).

Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.

In other words, if the maximum length of alternating subsequence is k then your task is to find the maximum sum of elements of some alternating subsequence of length k.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤2⋅105) — the number of elements in a. The second line of the test case contains n integers a1,a2,…,an (−109≤ai≤109,ai≠0), where ai is the i-th element of a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output

For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of a.

Example

input

4

5

1 2 3 -1 -2

4

-1 -2 -1 -3

10

-2 8 3 8 -4 -15 5 -2 -3 1

6

1 -1000000000 1 -1000000000 1 -1000000000

output

2

-1

6

-2999999997

Note

In the first test case of the example, one of the possible answers is [1,2,3–,−1–––,−2].

In the second test case of the example, one of the possible answers is [−1,−2,−1–––,−3].

In the third test case of the example, one of the possible answers is [−2–––,8,3,8–,−4–––,−15,5–,−2–––,−3,1–].

In the fourth test case of the example, one of the possible answers is [1–,−1000000000,1,-1000000000,1,−1000000000].

题目大意:

找到最长的+ - + - + - + - + -(+ -交替即可)的序列,求最长序列的最大值。

这道题好像没什么好说的,就是找出每一段全正或全负的子序列最大值作为最长序列的数即可,可以用双指针算法。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll n, a[N];
int main() {
//freopen("in.txt", "r", stdin);
int t; cin >> t;
while (t--) {
cin >> n;
ll ans = 0, now = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (i == 1)
now = a[i];
else {
if (a[i] * a[i - 1] > 0)
now = max(now, a[i]);
else
ans += now, now = a[i];
}
}
ans += now;//别忘记加上最后一位
cout << ans << endl;
}
}

Codeforce1343C. Alternating Subsequence的更多相关文章

  1. [CC-MCO16306]Fluffy and Alternating Subsequence

    [CC-MCO16306]Fluffy and Alternating Subsequence 题目大意: 给定一个\(1\sim n(n\le3\times10^5)\)的排列\(a\). 对于一个 ...

  2. CF# 334 Alternative Thinking

    A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  3. Codeforces Round #334 (Div. 2) C. Alternative Thinking 贪心

    C. Alternative Thinking Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/6 ...

  4. Alternative Thinking 找规律

    Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...

  5. CodeForces - 603A-Alternative Thinking (思维题)

    Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...

  6. 【33.10%】【codeforces 604C】Alternative Thinking

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. CF #636 (Div. 3) 对应题号CF1343

    unrated 选手悠闲做题,然后只做出四个滚蛋了 符合 div3 一贯风格,没啥难算法 E最后就要调出来了,但还是赛后才A的 CF1343A Candies 传送门 找到一个 \(x\),使得存在一 ...

  8. Codeforces Round #636 (Div. 3)

    比赛链接:https://codeforces.com/contest/1343 A - Candies 题意 有一数列 x + 2x + 4x + ... + 2k-1x = n,输出 k ≥ 2 ...

  9. codeforc 603-A. Alternative Thinking(规律)

    A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes   Kevin ha ...

  10. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

随机推荐

  1. 超实用:通过文字就可以操纵这款AI表格

    公众号「架构成长指南」,专注于生产实践.云原生.分布式系统.大数据技术分享. 工具介绍 今天给大家分享超实用的AI表格ChatExcel,这个工具是由北大团队在2022年3月开始开发的AI表格处理神器 ...

  2. 龙芯发布 .NET 8 SDK 8.0.100-ea1(试用版)

    随着.NET 8的发布,国内的社区朋友们也很关心龙芯.NET 团队对于Loongarch .NET 8的发布时间,目前从龙芯.NET编译器团队已经在龙芯.NET 官网上发布龙芯.NET 8 SDK-8 ...

  3. OpenSSL 使用AES对文件加解密

    AES(Advanced Encryption Standard)是一种对称加密算法,它是目前广泛使用的加密算法之一.AES算法是由美国国家标准与技术研究院(NIST)于2001年发布的,它取代了原先 ...

  4. 快速认识,后端王者语言:Java

    Java作为最热门的开发语言之一,长居各类排行榜的前三.所以,就算你目前不是用Java开发,你应该了解Java语言的特点,能用来做什么,以备不时之需. Java 是一种高级.多范式编程语言,以其编译为 ...

  5. MongoDB是一个NoSQL数据库,有着多种不同的命令和操作。以下是一些常见的MongoDB命令:

    show dbs:列出所有数据库 use db_name:切换到指定的数据库 db.dropDatabase():删除当前数据库 db.createCollection("collectio ...

  6. MySQL索引命名规范

    [强制]主键索引名为 pk_字段名:唯一索引名为 uk_字段名:普通索引名则为 idx_字段名 说明:pk_ 即 primary key:uk_ 即 unique key:idx_ 即 index 的 ...

  7. Socket.D 基于消息的响应式应用层网络协议

    首先根据 Socket.D 官网的副标题,Socket.D 的自我定义是: 基于事件和语义消息流的网络应用协议. 官网定义的特点是: 基于事件,每个消息都可事件路由 所谓语义,通过元信息进行语义描述 ...

  8. Nginx服务器常用参数设置

    Nginx作为一个高性能的Web服务器和反向代理,它的性能可以通过调整底层操作系统的参数来进一步优化.以下是一些常见的操作系统级别的调整,通常针对Linux系统: File Descriptors L ...

  9. 组合式api-子父组件之间通信props和emit

    整体来说和vue2也是比较相似的. 使用props传递数据到子组件 父组件给定数据. 子组件中使用defineProps来接收父组件传递的数据. 子组件emit触发事件通知父组件 思想和vue2完全一 ...

  10. 2024年 Kubernetes 四大趋势预测

    Kubernetes 在生产环境中的复杂性已经成为常态,在2023年这个平台工程盛行的时代,容器管理的最大亮点可能在于其灵活性,然而在运维政策和治理等方面仍然存在诸多挑战.Kubernetes 最大的 ...