Problems > One Perfect Trade

One Perfect Trade

Difficulty: 1200 3000 ms 128 MB

dp greedy

Given daily prices, buy once and sell later once. Print the maximum profit (0 if no profitable trade exists).

Input: the first line has N; the second line has N prices.

Output: the best single-trade profit.

Constraints:

  • 1 ≤ N ≤ 105
  • 1 ≤ pi ≤ 109

Sample Tests

Input 1
6
7 1 5 3 6 4
Output 1
5
Input 2
3
7 6 5
Output 2
0

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.