传送门:Codeforces 1321B
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:
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].
题目大意:
有 n 个城市,每个城市有各自的魅力值 b ,一个人从一个城市出发到其他城市,不过他有规则的到达城市,下一个城市的序号一定要比前一个大,同时美丽值还要满足 bi+1 - bi = ci+1 - c i 的等式。问你他可以到达的所有城市美丽值和的最大值是多少。
解题思路:
我们可以将等式转换一下,变成:bi+1 - ci+1 = bi - c i
这样看的话,对于每个城市的 bi - ci 的值是一定的记为 ai,那么我们的规则就变成 我们到达的城市只能是 a 值相同的几个城市。
所以我们只要把 a 值算出来,按 a 值由小到大排序,计算相同 a 值下,b 值和的最大值即可。
AC代码:
#include
#include
#include
#include
#include
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=2e5+10;
int n;
struct Node
{
ll b,a;
}c[N];
bool cmp(Node x,Node y)
{
return x.a<y.a;
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lld",&c[i].b);
c[i].a=i-c[i].b;
}
if(n==1)
{
printf("%lld\n",c[0].b);
return 0;
}
sort(c,c+n,cmp);
ll maxn=0,sum=c[0].b;
for(int i=1;i<n;i++)
{
if(c[i].a==c[i-1].a)
{
sum+=c[i].b;
}
else
{
maxn=max(maxn,sum);
sum=c[i].b;
}
}
maxn=max(maxn,sum);
printf("%lld\n",maxn);
return 0;
}