Buying Stock
Practice
3.4 (5 votes)
Ad Hoc
Algorithms
Medium
Dynamic programming
Dynamic programming
Problem
73% Success 5676 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Alice wants to buy a stock. She knows the increase and decrease in the stock value for N consecutive days. Positive value denotes the increase in stock price and negative value denotes the decrease in stock price.
She can buy stock in the morning, the increment or decrement in the stock occurs in afternoon and the selling can be done only at night. She can buy and sell stock at most once.
She can choose not to buy stock at all. Selling is only possible if she has bought the stock before. You can safely assume that she buys the stock at the price 0 units at any particular day.

You have to calculate the maximum profit she can earn.

Input Format:
First line of the input contains an integer N denoting the number of days.
Next line contains N space separated integers denoting increase or decrease in value. (Positive number denotes increase and negative denotes decrease)

Output Format:
Maximum profit that can be earned in this trade.

Constraints :

  • \(1 \le N \le 10^{5}\)
  • \(-10^{5} \le A_{i} \le 10^{5}\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
1 votes
Tags:
Medium
Points:30
2 votes
Tags:
MediumDynamic programming
Points:30
5 votes
Tags:
Applications of Dynamic ProgrammingAlgorithmsDynamic ProgrammingMath