class Solution {
public:
int maxProfit(vector<int>& prices) {
vector<vector<vector<int>>> dp(prices.size() + 1);
for (int i = 0; i < dp.size(); ++i) {
vector<vector<int>> tmp(3, vector<int>(2));
dp[i] = tmp;
}
for (int i = 0; i <= prices.size(); ++i) {
dp[i][0][0] = 0;
for (int k = 1; k <= 2; ++k) {
if (i == 0) {
dp[0][k][0] = 0;
dp[0][k][1] = INT_MIN;
continue;
}
dp[i][k][0] = max(dp[i - 1][k][0], dp[i - 1][k][1] + prices[i - 1]);
dp[i][k][1] = max(dp[i - 1][k][1], dp[i - 1][k-1][0] - prices[i - 1]);
}
}
return dp[prices.size()][2][0];
}
};