Here for example: 96 24 48 27 3 144 75 144 48. Answer: 291. Limitations N <= 10 ^ 5 and time 2 seconds.

Link to the task

  • Very simple: select the ever-increasing subsequences and compare their sums. And if you just need to calculate the amount, you can manage it in one pass. - ixSci
  • Another thing is the limitation n <= 10 ^ 5 and the time is 2 seconds. And I know the algorithm of all subsequences for N * N. For too long. - Kurbanbayev
  • I heard that here you can use the dynamic programming method and use data structures. But I do not know how to use them. - Kurbanbayev
  • @Kurbanbayev, are there any restrictions on the values? - Qwertiy ♦
  • one
    If you write “limit of 2 seconds”, this immediately means that you have no practical task, and you want us to decide the Olympiad for you. I would ask the participants to show a little awareness and not to give the copied code, even if the more honest participants of the competition will be in equal conditions. - VladD

2 answers 2

The general idea is exactly the same as for the ordinary task where you need to maximize the length of the sequence. It is solved using a segment tree or Fenwick tree. References to the ideas http://e-maxx.ru/algo/fenwick_tree http://e-maxx.ru/algo/segment_tree .

We need to be able to quickly find (with log n) a key less than the given one with a maximum value. (Equivalent to finding the maximum in the segment).

pseudocode

for (int i=0;i<N;i++) Tree.update(A[i], Tree.search(A[i]) + A[i] ) 

The answer is maximum over all A [i]. There if that analysis is on this link.

Below is more expanded.

and what exactly is not clear?) If you figured out how to write a tree, then it remains only to write this cycle. Why this is so: if we continue the sequence, then it is true that the previous element was no more than the current one and of all such it is necessary to continue the most profitable (by the amount already accumulated). Why 2 statement is true - by contradiction, let it become advantageous to continue a1 <= a2 <= a3 ... <= C <= ... (C - current) and there is b1 <= b2 <= b3 <= ... <= C such that a1 + a2 + a3 + ... <b1 + b2 + b3 + ... (strict negation of 2 points), then we can replace the entire initial chain ai with bi, which will improve the answer -> get a contradiction. Hence the greedy algorithm is correct.

Algorithm: we take the current element C, we look at all the sequences q1 <= q2 <= qn <= C, from all these we choose it with the maximum sum q1 + q2 + ... + qn and try to continue it (if there are none, then we start a new sequence) .

Note that the sequence itself does not interest us, we only need to know its last element and the sum of its members.

Let's build a tree, a key is the last element, a value is a sum.

Now just go through the array from left to right, find the maximum on the segment (-inf, C] (if there is nothing, then set the answer to maximum 0). Change the value of the current element to the maximum found + C.

The answer is the maximum of all A [i] from the array. The complexity of the O (n log N) time, O (n) or O (n log n) memory (depending on the type of tree).

I do not write the code specifically.

  • He is not very clear: ( - Kurbanbayev
  • Can you explain in more detail? - Kurbanbayev
  • wrote in more detail. - pavel
  • And what is the advantage if you use Fenwick Tree? - Kurbanbayev
  • it is written faster, it uses less memory, if it is possible, then Fenwick is almost always better than any other tree (but it is not applicable wherever the segment tree is applicable). - pavel

For each element, you need to keep the maximum amount that ends strictly in it.

It is necessary somehow for the current element to calculate the answer for the logarithm.

I will assume that the placement of the minimum last number for the given response in the map . Although I can not justify that the asymptotics will be acceptable, because I have to go through the elements of the map in the loop until the answer is found.

  • Can you write the code? - Kurbanbayev
  • one
    @Kurbanbayev, why should I write code? If I was going to write this code, I would send it to codeforces. Where do you get access to it when you decide? - Qwertiy ♦
  • @Kurbanbayev, why do you need to solve problems on codeforces, if you can not write a program of two cycles using the described algorithm? - Qwertiy ♦
  • You just wrote that you need to find the answer for the logarithm of each element. I do not know how this can be done. Therefore, I ask the code. - Kurbanbayev
  • @Kurbanbayev, no. I wrote that , ideally, you need to logarithm, and then I brought up an idea with muddy assymptotics (placement in map is logarithm, but for recalculation, there will be a cycle for an element whose iteration number is unknown), about which I assume that it will meet the condition " average no more than 10 ^ 3 actions. " - Qwertiy ♦