The 2022 ICPC Asia Xian Regional Contest-J. Strange Sum

Given a sequence a1,a2,…,an.

You are going to select zero or more elements of aa so that: if you select aiai, then in any interval of length ii (formally, in a[j,j+i−1] for any 1≤j≤n−i+1) you can select at most 2 elements.

Calculate the maximal sum of the elements you select.

Input

The first line contains an integer nn (2≤n≤105).

The second line contains nn integers a1,a2,…,an (−10^9≤ai≤10^9).

Output

Output a single integer denoting the answer.

Examples

input

4
1 4 3 2

output

7

input

3
-10 -10 -10

output

0

解析:其实就是让你最多选两个数(可以不选),使得总和最大,1.如果有两个及以上的正数,选取最大的两个正数   2 .如果只有一个正数,就选它   3.如果没有,那么就是0

#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[100005];//记录正数
int main()
{
	int n,m=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		ll l;
		scanf("%lld",&l);
		if(l>0) a[m++]=l;
	}
	sort(a,a+m);
	if(m==0) printf("0\n");
	else if(m==1) printf("%lld\n",a[0]);
	else printf("%lld\n",a[m-1]+a[m-2]);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值