SnowflakeOA #45
Selecting Stocks
Estimated MediumJudge-ready
Topics (AI-labelled)Knapsack
Choose stocks with the available funds, buying at most one share per company, with a total purchase price not exceeding saving. The profit is the predicted future sale proceeds minus the purchase price, and buying no shares at all is allowed. Find the maximum profit, not the maximum total sales.
Input
The first line contains n saving, the second line contains currentValue, and the third line contains futureValue. This site adds 1≤n≤1000, 0≤saving≤10000, and 0≤each price≤10⁹, and a current or future price may be 0.
Output
Output the single integer required by the problem.
Example 1
Input:
5 250
175 133 109 210 97
200 125 128 228 133
Output:
55
Example 2
Input:
1 4
1
3
Output:
2
Example 3
Input:
2 0
0 1
5 0
Output:
5
Example notes
The answers to the three public examples are, in order: 55; 2; 5. The examples were checked by independent enumeration, state search, or direct simulation.
Editorial
Loading solution…