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 c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i 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 c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} 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 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i 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].
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given n up to 200k, and b_i up to 400k. We need to pick a strictly increasing sequence of indices c1 < c2 < ... < ck, such that for any consecutive cities, the condition holds: c_{i+1} - c_i = b_{c_{i+1}} - b_{c_i}. For a single city (k=1), no condition. Maximize sum of b_{c_i}.
Observation: The condition can be rewritten: b_{c_{i+1}} - c_{i+1} = b_{c_i} - c_i? Let's check: c_{i+1} - c_i = b_{c_{i+1}} - b_{c_i} => bring terms: b_{c_{i+1}} - c_{i+1} = b_{c_i} - c_i. So the condition is that the value (b_j - j) is constant across all cities in the journey (except maybe the first? Actually if we have multiple cities, then for each adjacent pair we have equality, thus by transitivity all b_j - j are equal). Indeed, for any i, b_{c_{i+1}} - c_{i+1} = b_{c_i} - c_i, so all have same value, say d = b_{c_i} - c_i for all i.
Thus the journey must be a set of indices with strictly increasing indices, and all having the same value of b_i - i. Because they are strictly increasing indices, but the condition ensures the constant d. So essentially, we need to pick a subset of indices from the array with a common value d = b_i - i, and maximize sum of b_i (note that the indices must be increasing, but since we are free to pick any subsequence? Actually if we pick any subset of indices from those with same d, they will be naturally increasing as we order them; the condition also requires that we pick them in increasing order, but we can always order them because indices are distinct. So essentially we can pick any set of indices with the same d, and the journey is the sorted list; does it satisfy the condition? Check: For any consecutive indices i<j (both with same d), we have b_j - j = d = b_i - i => b_j - b_i = j - i, thus condition holds. So indeed any increasing sequence of indices with equal d satisfies the condition. And there is no additional restriction like must be all of them? So the problem reduces to: For each value d = b_i - i, we can form a journey consisting of any subset (non-empty) of the indices i that have that d. The journey's total beauty = sum of b_i of chosen indices. Since the journey must be a sequence, we can choose any non-empty subset; however, note that we must maintain strictly increasing indices; any subset's sorted order works. So the maximum sum for a fixed d is simply the sum of all positive b_i? Actually b_i are all positive (≥1). So sum of all b_i over indices with that d would be maximum, because all b_i are positive. So it might seem that the optimal journey is to take all indices with the same d. But is there any restriction that the journey must be contiguous? The example: n=8, b=[3,4,4,6,6,7,8,9] gives possible journeys: [1,2,4] (d values: b1-1=2, b2-2=2, b4-4=2 => all same) => sum 3+4+6=13. Could we also take [1,2,4,?] check if any other index with d=2: b3-3=1, b5-5=1, b6-6=1, b7-7=1, b8-8=1 => no, only 1,2,4 have d=2. So that's all of them. Also [3,5,6,8]: d= b3-3=1, b5-5=1, b6-6=1, b8-8=1, sum=4+6+7+9=26. Could we also take all indices with d=1? That would be indices 3,5,6,8? But also index? Let's compute b_i - i: index1:2, index2:2, index3:1, index4:2, index5:1, index6:1, index7:1? b7=8, 8-7=1, index8:9-8=1. So indices 3,5,6,7,8 all have d=1. But the journey [3,5,6,8] doesn't include 7. Could we include 7? Then we could have [3,5,6,7,8]? Check: sorted: 3,5,6,7,8. All have d=1, so condition holds for any adjacent. So that would be a valid journey. But the example listed only [3,5,6,8] as one possible three-city? Actually they said "three possible ways" but they listed [3,5,6,8] (that's four cities) and also [7] and [1,2,4]? They said "there are several three possible ways to plan a journey:" then they list:
c = [1,2,4];
c = [3,5,6,8];
c = [7] (a journey consisting of one city is also valid).
They said "several three possible ways" maybe meaning there are three examples; there are additional ways. So [3,5,6,7,8] is also valid? Let's check: b_i values: index7 = 8, so d=8-7=1, yes. So why didn't they include that? Possibly because they said "several three possible ways" but they didn't list all; they just gave examples. So it's fine. So indeed all indices with same d can be taken. But is there any restriction like we cannot skip? No.
Thus the maximum sum for a given d is sum of all b_i over i with that d. Since all b_i positive, picking all gives maximum. However, is there any downside? The journey must be a strictly increasing sequence; any subset of indices with same d works. So for each d, we can take all of them. But what about mixing different d's? That's not allowed because then the condition fails. So the optimal journey is the maximum over d of the total sum of b_i for indices with that d, or possibly a single city (which is included in the d group, but maybe a single city from a group could be less than sum of multiple from that group, but since we can take multiple, we take the group with the highest sum). However, we must also consider that we can also pick any single city, which is essentially picking a subset of size 1. So the maximum over groups (full sum) is at least as large as any single city. So the answer = max_d sum_{i: b_i - i = d} b_i.
But wait: Are there any constraints on the journey such that it must be exactly the sequence? No. So yes.
But is there any scenario where picking a subset of a group yields a higher sum than picking all? Since all b_i are positive, sum of all is greater than any sub-sum. So picking all is best for that group. So answer is max over d of total beauty for that group.
But check: Example 1: Input: 6, b = [10,7,1,9,10,15]. Compute d = b_i - i:
i=1:10-1=9
i=2:7-2=5
i=3:1-3=-2
i=4:9-4=5
i=5:10-5=5
i=6:15-6=9
Groups:
d=9: indices 1,6 => sum b_i=10+15=25
d=5: indices 2,4,5 => sum=7+9+10=26
d=-2: index3 => sum=1
Max =26, matches output.
Example 2: n=1, b=400000 => sum=400000.
Example 3: Input: 7, b = [8,9,26,11,12,29,14]. Compute d:
i=1:8-1=7
i=2:9-2=7
i=3:26-3=23
i=4:11-4=7
i=5:12-5=7
i=6:29-6=23
i=7:14-7=7
Groups:
d=7: indices 1,2,4,5,7 => sum=8+9+11+12+14=54? Let's compute: 8+9=17, +11=28, +12=40, +14=54.
d=23: indices 3,6 => sum=26+29=55.
Max=55, matches output.
So indeed solution is to compute for each value of (b_i - i) the sum of b_i, and output the maximum sum.
Edge cases: n up to 2e5, b_i up to 4e5, so b_i - i can be negative (down to maybe -2e5? Actually b_i >=1, i up to 2e5, so b_i - i can be as low as 1 - 200000 = -199999, or as high as 400000 - 1? Actually i starts from 1, so maximum b_i - i can be 400000 - 1 = 399999, but since i up to 200k, max about 400000 -1 = 399999, min about 1-200000 = -199999. So range roughly [-200k, 400k]. We can use a map (dictionary) to store sums.
Implementation: read n, array b. Then iterate i from 1 to n, compute d = b[i-1] - i (since 0-indexed), update map[d] += b[i-1]. Then find max value in map. Print that max.
Time O(n), space O(n) for map.
But careful: i is 1-indexed. So use i starting at 1.
That's it.
But wait, is there any nuance? Let's re-express condition: c_{i+1} - c_i = b_{c_{i+1}} - b_{c_i} -> rearranged gives b_{c_i} - c_i = b_{c_{i+1}} - c_{i+1}. So indeed they must share same "delta". So the grouping is correct.
Thus answer is max sum over groups.
But is it always optimal to take all cities in a group? The journey requires that cities are visited in strictly increasing order. That's fine. However, is there any additional constraint that the journey must be "continuous" in the sense that you cannot skip? The condition only applies to adjacent cities. If you have three cities i<j<k, with same d, then automatically b_j - j = b_i - i = d and b_k - k = d, thus condition holds for i->j and j->k. So you can include them all. So yes.
Thus solution is straightforward.
But wait: The problem statement says: "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 c_i and c_{i+1}, the condition c_{i+1} - c_i = b_{c_{i+1}} - b_{c_i} must hold." This is exactly what we used.
Thus answer is correct.
Double-check with a scenario where some b_i are negative? But constraints say b_i >= 1. So all positive. So sum of multiple positive numbers > any single.
Thus solution complete.
But is there any possibility that we might need to consider picking a subsequence of a group but not all, because maybe some indices are not increasing? They are always increasing if we pick in sorted order. So no issue.
Thus code:
int main() {
int n;
cin >> n;
vector b(n);
for(auto& x: b) cin >> x;
unordered_map<long long, long long> sum;
for(int i=1; i<=n; ++i) {
long long d = b[i-1] - i;
sum[d] += b[i-1];
}
long long ans = 0;
for(auto& p: sum) ans = max(ans, p.second);
cout << ans << endl;
return 0;
}
But note: d can be large negative, we can use map. Since n up to 200k, unordered_map is fine.
Alternatively, we can sort by d or use a map.
Potential overflow: b_i up to 4e5, n up to 2e5, sum can be up to 8e10? Actually max sum per group: all n could have same d, sum up to n * 4e5 = 2e5 * 4e5 = 8e10, which fits in 64-bit (less than 2^63-1). So use long long.
Edge case: all d distinct, then each group sum is b_i, ans is max b_i. That's fine.
So final answer.
But I should reflect: Could there be a journey that does not have constant d? For multiple cities, the condition must hold for each adjacent pair, implying d constant. So no.
Thus solution is correct.
Wait, but we derived b_j - j = b_i - i. However, there is also a possibility that the journey could include a single city from each of multiple groups if we treat the condition only for adjacent cities? Actually if we mix groups, then for some adjacent pair the equality would fail because d would differ. So invalid.
Thus answer stands.
Let's confirm with more test cases. Suppose n=3, b=[5, 2, 3]. d: 5-1=4, 2-2=0, 3-3=0. groups: d=4 sum=5; d=0 sum=2+3=5. ans=5. Could there be a journey with [1,2]? Check: 2-1=1, b2 - b1 =2-5=-3, not equal. So not valid. [1,3]? 3-1=2, b3-b1=3-5=-2, not equal. [2,3]? 3-2=1, b3-b2=3-2=1 => valid! So journey [2,3] sum=5, matches. So ans=5.
What about [1] only sum=5, same. So max=5.
Now what about scenario with negative b_i? Not in constraints.
Thus solution is correct.
Therefore final answer: implement as described.
But we must ensure that we consider the possibility of taking a single city from a group that may have sum less than another group's sum. Already covered.
Thus code is simple.
Now the user requested: "Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?" So we just output max sum.
But before finalize, check if any tricky nuance: Could there be a case where we can pick a subsequence that is not necessarily all indices with same d but still get a higher sum than sum of that d? For example, if some b_i are negative, but they aren't. So no.
Thus answer.
But wait: Could there be a possibility to have a journey with one city, and that city's b_i may be larger than any group sum? But group sum includes that city if we group it; sum of that group might be smaller if other cities in that group have very small b_i? But b_i are all positive, so adding more cities only increases sum. So sum of group containing that city is at least b_i. So the maximum over groups includes that b_i. So no.
Thus answer = max over d of sum_{i: b_i - i = d} b_i.
Now implementation details: Use unordered_map for O(1) average.
Potential TLE? But n=2e5, unordered_map should be fine.
Alternatively, use map to guarantee O(n log n) which is also fine. But since we have negative keys, unordered_map is okay.
But careful: unordered_map with negative keys is fine.
Now finalize.
But wait: Could there be an issue with integer overflow in d? d = b_i - i fits in int range: b_i up to 400k, i up to 200k, so d between -200k and 400k, fits in int. But we may use long long for safety.
Now final answer.
I'll write in Python for ease? The user might expect C++? The problem statement mentions "print one integer". But as ChatGPT, we can provide solution in Python as it's simpler. However, the environment typically expects code. Since the user didn't specify language, we can provide both reasoning and a solution in Python or C++. The problem is from Codeforces style, often C++ is used. But we can produce Python solution.
We need to output answer.
Given constraints, Python with dictionary is fine.
Implementation in Python:
def solve():
import sys
input = sys.stdin.readline
n = int(input())
b = list(map(int, input().split()))
sums = {}
for i, val in enumerate(b, start=1):
d = val - i
sums[d] = sums.get(d, 0) + val
ans = max(sums.values())
print(ans)
Edge case: n=0? n>=1.
That's it.
But we must consider that sums.values() may be empty? n>=1 so at least one entry.
Thus final answer.
Let me provide final answer with explanation and code. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
b = list(map(int, data[1:1 + n]))
code
group_sum = {}
for idx, beauty in enumerate(b, start=1):
d = beauty - idx
group_sum[d] = group_sum.get(d, 0) + beauty
ans = max(group_sum.values())
print(ans)