def max_profit_optimized(prices):
cur_max, max_so_far = <NUM_LIT:0>, <NUM_LIT:0><EOL>for i in range(<NUM_LIT:1>, len(prices)):<EOL><INDENT>cur_max = max(<NUM_LIT:0>, cur_max + prices[i] - prices[i-<NUM_LIT:1>])<EOL>max_so_far = max(max_so_far, cur_max)<EOL><DEDENT>return max_so_far<EOL>
input: [7, 1, 5, 3, 6, 4]
diff : [X, -6, 4, -2, 3, -2]
:type prices: List[int]
:rtype: int