Best Time to Buy and Sell Stock II

Medium · Greedy · about 10 minutes

Asked at Apple, Meta, Bloomberg, Google, Amazon, Microsoft, Hudson River Trading.

prices[i] is the share price on day i. Trade as often as you like, but you can own at most one share at a time (selling and buying again on the same day is allowed). What is the largest total profit?