#357easyGreedy
Stock Trading - Unlimited Transactions
Time Limit: 2sMemory: 256MB
Problem
You are given daily stock prices. You may buy and sell as many times as you like, but you must sell your current position before buying again (no holding multiple shares).
Find the maximum total profit you can achieve.
Input Format
- First line: integer
n(number of days). - Second line:
nspace-separated integers representing daily prices.
Output Format
A single integer — the maximum profit.
Examples
Example 1
Input(First line: integer n (number of days).)
6 7 1 5 3 6 4
Output
7
Buy at 1, sell at 5 (+4). Buy at 3, sell at 6 (+3). Total = 7.
Example 2
Input(First line: integer n (number of days).)
5 1 2 3 4 5
Output
4
Buy at 1, sell at 5 (+4). Or equivalently: capture every daily gain.
Constraints
- •1 ≤ n ≤ 10^5
- •0 ≤ prices[i] ≤ 10^4
Loading interactive editor…