Stock King Xiao Fang has recently faced a series of losses, with every stock he buys going down. He has lost almost 20 years’ worth of savings, leading people to say: “The Lost 20 Years.”
Now, **Xiao Fang** has just enough money to buy one more stock and has decided to invest in **0612 CIA**.
As a member of 0612 CIA, you have discovered confidential information about future stock prices. With this valuable knowledge, you decide to help Xiao Fang by advising them on the best times to buy and sell the stock, and how much profit they can make.
However, to pique **Xiao Fang's** interest in the information you’ve calculated, you only need to determine how much money can be made.
⚠️ Note that you can only **buy and sell once**!
⚠️ Note that you can only sell after buying!
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The input consists of a single line with many numbers, representing the future daily closing prices of the stock. All numbers are guaranteed to be positive integers, and the first number must be 100. | Output a single number that represents the maximum profit for the stock expert, Xiaofang. |
100 108 115 115 120 109 110 | 20 |
Thought Process
Use EOF to read the data, and declare two variables: min, initialized to 100, and ans, initialized to 0.
When the received data is greater than min, check if the current ans is less than the received data minus min. If it is, update ans to be the received data minus min. If the received data is less than min, update min to the received data.
Lastly, output ans.
Sample Code-ZeroJudge F934: The Lost 20 Years
#include <iostream>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N, min = 100, ans = 0;
while (cin >> N) {
if (N > min) {
ans = max(ans, N-min);
}
if (N < min) min = N;
}
cout << ans << "\n";
}
//ZeroJudge F934
//Dr. SeanXD