dp ^^
time: O(N); space: O(1)\n\n\n\n\nlong long maximumTotalCost(vector<int>& n)\n{       \n\tlong long pp{}, p{n[0]};\n\tfor(int i{1}; i<size(n); ++i)\n\t{\n\t\taut
**time: `O(N)`; space: `O(1)`**\n\n![image](https://assets.leetcode.com/users/images/6b15cf32-30e9-40ea-b5b6-e26a800d0308_1722938357.4201438.png)\n\n```\nlong long maximumTotalCost(vector<int>& n)\n{       \n\tlong long pp{}, p{n[0]};\n\tfor(int i{1}; i<size(n); ++i)\n\t{\n\t\tauto t = max(p+n[i], pp+n[i-1]-n[i]);\n\t\tpp = p;\n\t\tp  = t;\n\t}\n\treturn p;\n}\n```\n**Notation:**\n* `p` - the best previous solution;\n* `pp` - the best previous previous solution;\n\n\n![image](https://assets.leetcode.com/users/images/74e99a98-7ddd-4c51-9b16-5967a9592654_1722938774.7456365.png)\n\n```\nlong long maximumTotalCost(vector<int>& n)\n{  \n\tlong long plus{n[0]}, minus{n[0]};\n\tfor(int i{1}; i<size(n); ++i)\n\t{\n\t\tauto old_plus = plus;\n\t\tplus  = max(plus, minus) + n[i];\n\t\tminus = old_plus - n[i];\n\t}\n\treturn max(plus, minus); \n}\n```\n**Notation:**\n* `plus` - the best solution ending with `+`;\n* `minus` - the best solution ending with `-`;\n\nThis two solution remind order reduction technique in differential equations:\n\n![image](https://assets.leetcode.com/users/images/7ac0f59e-2bf0-4458-b851-ee12bd615122_1722947211.5739186.png)\n\n  We can either store 2 pieces of data for the previous moment in time `t-dt` or store 1 piece of data for 2 previous moments in time `t-dt` and `t-2*dt`.
['C', 'C++']