Codeforces 865D - Buy Low Sell High
Xem dạng PDF
Gửi bài giải
Điểm:
6,00 (OI)
Giới hạn thời gian:
1.0s
Giới hạn bộ nhớ:
512M
Input:
stdin
Output:
stdout
Nguồn bài:
Dạng bài
Ngôn ngữ cho phép
C, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch
Reference: Codeforces 865D — Buy Low Sell High
You know the price of one share of a certain stock for each of the next ~N~ days.
On each day, you may perform at most one of the following actions:
- buy one share;
- sell one share that you currently own;
- do nothing.
You initially own no shares. You may own several shares at the same time, but you may not sell a share unless you own at least one.
After the final day, you must own exactly zero shares.
Determine the maximum possible profit.
Input
- The first line contains one integer ~N~, the number of days.
- The second line contains ~N~ integers: ~p_1, p_2, \ldots, p_N~, where ~p_i~ is the stock price on day ~i~.
Output
- Print one integer: the maximum possible profit after all ~N~ days.
Constraints
- ~2 \le N \le 3 \times 10^5~
- ~1 \le p_i \le 10^6~
Example 1
Input:
9
10 5 4 7 9 12 6 2 10
Output:
20
Bình luận