<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day, and an integer <code>k</code>.</p>

<p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k</code> times and sell at most <code>k</code> times.</p>

<p><strong>Note:</strong> You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> k = 2, prices = [2,4,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> k = 2, prices = [3,2,6,5,0,3]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= 100</code></li>
	<li><code>1 &lt;= prices.length &lt;= 1000</code></li>
	<li><code>0 &lt;= prices[i] &lt;= 1000</code></li>
</ul>

class Solution {
public:
    template <typename T> class Array2D {
        T* data;
        unsigned N, M;

    public:
        Array2D(size_t n, size_t m) : N(n), M(m) { data = new T[N * M]; }
        ~Array2D() { delete[] data; }
        inline T* operator[](size_t i) { return &data[i * M]; }
    };
    
#if 0
    int maxProfit(int k, vector<int>& prices) {
        unsigned N = prices.size();

        struct E { int p; int m; };
        Array2D<E> c{N, k};

        c[0][0].p = 0;
        c[0][0].m = prices[0];

        for (int i = 1; i < k; i++) {
            c[0][i].p = 0;
            c[0][i].m = INT_MAX;
        }

        for (int j = 1; j < N; j++) {
            c[j][0].p = max(prices[j] - c[j-1][0].m, c[j-1][0].p);
            c[j][0].m = min(prices[j], c[j-1][0].m);
            for (int i = 1; i < k; i++) {
                for (int jj = j-1; jj >= 0; jj--){
                    c[j][i].p = max(prices[j] - c[jj][i].m + c[jj][i - 1].p, c[j-1][i].p);
                }
                if (cj[i - 1].p > cj_1[i - 1].p) {
                    cj[i].m =
                        INT_MAX; // min seen since (i-1)th stock was bought
                } else {
                    cj[i].m = min(prices[j], cj_1[i].m);
                }
            }

            printf("j= %d %d\n", j, prices[j]);
            for (int i = 0; i < k; i++) {
                printf("%d:(%d %d)  ", i, cj[i].p, cj[i].m);
            }
            printf("\n\n");

            swap(cj, cj_1);
        }

        int max_p = cj_1[0].p;
        for (int i = 1; i < k; i++) {
            max_p = max(max_p, cj_1[i].p);
        }

        delete[] cj;
        delete[] cj_1;

        return max_p;
    }
#endif
    int maxProfit(int k, vector<int>& prices) {
        unsigned N = prices.size();
        Array2D<int> c1{N, N};
        int* ck = new int[N];
        int* c0 = new int[N];

        using milli = std::chrono::milliseconds;
        auto start = std::chrono::high_resolution_clock::now();

        for (int i = 0; i < N; i++) {
            c1[i][i] = 0;
        }
        for (int l = 1; l < N; l++) {
            for (int i = 0; i < N - l; i++) {
                int j = i + l;
                c1[i][j] = max(prices[j] - prices[i], c1[i][j - 1]);
                c1[i][j] = max(c1[i][j], c1[i + 1][j]);
            }
        }

        auto finish = std::chrono::high_resolution_clock::now();
        auto tm1 = std::chrono::duration_cast<milli>(finish - start).count();
        start = std::chrono::high_resolution_clock::now();
#if 0
        printf("c1=\n");
        for (int i = 0; i < N; i++){
            for (int j = i + 1; j < N; j++){
                printf("(%d,%d)=%d  ", i, j, c1[i][j]);
            }
            printf("\n");
        }
#endif

        for (int j = 0; j < N; j++) {
            ck[j] = c1[0][j];
        }
        c0[0] = ck[0];

        for (int n = 1; n < k; n++) {
            for (int j = 1; j < N; j++) {
                c0[j] = ck[j];
                for (int m = 1; m + 1 < j; m++) {
                    c0[j] = max(c0[j], ck[m] + c1[m + 1][j]);
                }
            }

            swap(ck, c0);

#if 0
            printf("\nck k=%d\n", n+1);
            for (int i = 0; i < N; i++){
                for (int j = i + 1; j < N; j++){
                    printf("(%d,%d)=%d  ", i, j, ck[i][j]);
                }
                printf("\n");
            }
#endif
        }

        finish = std::chrono::high_resolution_clock::now();
        auto tm2 = std::chrono::duration_cast<milli>(finish - start).count();

        cout << tm1 << " ms, " << tm2 << " ms" << endl;

        return ck[N - 1];
    }
};
24551