int solve(int N, int fee, vector<int>& prices) {
int max_cash = 0;
int bought_even_stock = -1e9;
int bought_odd_stock = -1e9;
for (int idx = 0; idx < N; idx++) {
int current_price = prices[idx];
int updated_cash = max_cash;
int updated_even_stock = bought_even_stock;
int updated_odd_stock = bought_odd_stock;
if (current_price % 2 == 0) {
updated_even_stock = max(bought_even_stock, max_cash - current_price);
if (bought_odd_stock != -1e9) {
updated_cash = max(updated_cash, bought_odd_stock + current_price - fee);
}
} else {
updated_odd_stock = max(bought_odd_stock, max_cash - current_price);
if (bought_even_stock != -1e9) {
updated_cash = max(updated_cash, bought_even_stock + current_price - fee);
}
}
max_cash = updated_cash;
bought_even_stock =
updated_odd_stock;
}
return max_cash;
}

August 2, 2026 1.1K