PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GD0000104. Mua thấp bán cao (Buy Low Sell High)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Mua thấp bán cao (Buy Low Sell High)
Buy Low Sell High
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You know the stock price for each of days. On each day, you may buy one share, sell one owned share, or do nothing (at most one transaction per day). You start and end with zero shares, and cannot sell an unowned share. Find the maximum achievable profit.
Input
Line 1 contains . Line 2 contains prices .
Output
Print the maximum profit, which may be zero.
Subtasks
- Subtask 1 (20 points): ; for every .
- Subtask 2 (30 points): ; for every .
- Subtask 3 (50 points): ; for every .
Examples
Input
3
1 2 3
Output
2
Explanation
Buy on day for and sell on day for , earning . This is optimal with at most one action per day.