Journey Planning CodeForces - 1321B(sort)

Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.

Tanya plans her journey as follows. First of all, she will choose some city c1 to start her journey. She will visit it, and after that go to some other city c2>c1, then to some other city c3>c2, and so on, until she chooses to end her journey in some city ck>ck−1. So, the sequence of visited cities [c1,c2,…,ck] should be strictly increasing.

There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value bi associated with it. If there is only one city in Tanya’s journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities ci and ci+1, the condition ci+1−ci=bci+1−bci must hold.

For example, if n=8 and b=[3,4,4,6,6,7,8,9], there are several three possible ways to plan a journey:

c=[1,2,4];
c=[3,5,6,8];
c=[7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.

Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?

Input
The first line contains one integer n (1≤n≤2⋅105) — the number of cities in Berland.

The second line contains n integers b1, b2, …, bn (1≤bi≤4⋅105), where bi is the beauty value of the i-th city.

Output
Print one integer — the maximum beauty of a journey Tanya can choose.

Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c=[2,4,5].

The optimal journey plan in the second example is c=[1].

The optimal journey plan in the third example is c=[3,6].
思路:题目要求在这里插入图片描述其实我们移项之后就会发现,保证Ci+1-Bci+1=Ci-Nci就可以了。那么我们做差之后排序,可以走的城市就会靠在一起了,然后我们找寻最大值就行。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=2e5+100;
struct node{
	int v;
	int pos;
	int _v;
	bool operator<(const node &a)const{
		return _v<a._v;
	}
}p[maxx];
int n;

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&p[i].v),p[i].pos=i,p[i]._v=p[i].pos-p[i].v;
	sort(p+1,p+1+n);
	ll _max=0,sum;int j;
	for(int i=1;i<=n;)
	{
		sum=0;
		for(j=i;j<=n&&p[j]._v==p[i]._v;j++) sum+=p[j].v;
		_max=max(_max,sum);
		i=j;
	}
	cout<<_max<<endl;
	return 0;
}

努力加油a啊,(o)/~

发布了502 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/104609843
今日推荐