DP solution memoization C++
\n# Code\n\nclass Solution {\npublic:\n    // Function to calculate the maximum total cost using recursion and memoization\n    long long calculate(vector<int> 
\n# Code\n```\nclass Solution {\npublic:\n    // Function to calculate the maximum total cost using recursion and memoization\n    long long calculate(vector<int> &nums, int current, int sign, vector<vector<long long>> &dp) {\n        int n = nums.size();\n        // Base case: If we have processed all elements\n        if (current >= n) {\n            return 0;\n        }\n        // If the result is already computed, return it\n        if (dp[current][sign + 1] != INT_MIN) {\n            return dp[current][sign + 1];\n        }\n        \n        // Calculate the maximum cost by either switching the sign or keeping the same sign\n        long long add_sign = nums[current] * sign + calculate(nums, current + 1, -sign, dp);\n        long long continue_same = nums[current] * sign + calculate(nums, current + 1, sign, dp);\n        \n        // Store the result in dp table\n        dp[current][sign + 1] = max(add_sign, continue_same);\n        \n        return dp[current][sign + 1];\n    }\n    \n    // Function to initiate the calculation\n    long long maximumTotalCost(vector<int>& nums) {\n        int n = nums.size();\n        // Initialize the dp table with INT_MIN to indicate uncomputed states\n        vector<vector<long long>> dp(n, vector<long long>(3, INT_MIN));\n        // Start the calculation from the first element with a positive sign\n        return calculate(nums, 0, 1, dp);\n    }\n};\n\n```
['C++']