https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/submissions/

class Solution {
public:
    int maxScore(vector<int>& cardPoints, int k) {
        int n = cardPoints.size();
        if (n == 0)
            return 0;
 
        vector<int> left(n, 0);
        vector<int> right(n, 0);
        left[0] = cardPoints[0];
        right[n - 1] = cardPoints[n - 1];
        for (int i = 1; i < n; ++i)
            left[i] = left[i - 1] + left[i] + cardPoints[i];
        for (int j = 2; j < n; ++j)
            right[n - j] = right[n - j] + right[n - j + 1] + cardPoints[n - j];
 
        int result = 0;
        for (int i = 0; i < n; ++i) {
            if (i < k-1) {
                int possible = left[i] + right[n - k + 1 + i];
                if (possible > result)
                    result = possible;
            }
            if (i <= k - 1 && left[i] > result)
                result = left[i];
        }
        for (int i = 0; i < n; ++i) {
            if (i <= k - 2) {
                int possible = left[k - i - 2] + right[n - 1 - i];
                if (possible > result)
                    result = possible;
            }
            if(i <= k - 1 && right[n - 1 - i] > result)
                result = right[n - 1 - i] ;
        }
        return result;
    }
};