// Modification of arknave.cpp by Noah to use a Li-Chao tree. // The runtime of this is O(nk log n), but unfortunately it's quite a bit slower in practice. #include using namespace std; // ans[i][j] = high score after i mult and j add cards using State = std::vector>; typedef int64_t point; const int64_t inf = 400000000000000000ll; const int maxn = 200200; struct MaxPlusConv { MaxPlusConv(const std::vector& a, const std::vector& b) : A(a), B(b), line(a.size()*4) {} vector line; const vector &A, &B; std::vector merge() { for (int i = 0; i < A.size(); i++) add_line(i+1, 1, 0, A.size()); vector ans = A; ans[0] = 0; for (int i = 1; i < ans.size(); i++) { ans[i] = max(max(ans[i-1], ans[i]), -get(i+1, 1, 0, A.size())); } return ans; } int64_t f(point a, int64_t x) { if (a == 0) return inf*2; int ix = x-a-1; if (ix < 0) { return inf+a;} if (ix >= B.size()) { return inf-a;} return -(A[a-1] + B[ix]); } // li-chao tree from cp-algorithms void add_line(point nw, int v = 1, int l = 0, int r = maxn) { int m = (l + r) / 2; bool lef = f(nw, l) < f(line[v], l); bool mid = f(nw, m) < f(line[v], m); if(mid) { swap(line[v], nw); } if(r - l == 1) { return; } else if(lef != mid) { add_line(nw, 2 * v, l, m); } else { add_line(nw, 2 * v + 1, m, r); } } int64_t get(int x, int v = 1, int l = 0, int r = maxn) { int m = (l + r) / 2; if(r - l == 1) { return f(line[v], x); } else if(x < m) { return min(f(line[v], x), get(x, 2 * v, l, m)); } else { return min(f(line[v], x), get(x, 2 * v + 1, m, r)); } } }; void add(State& state, std::vector& v) { std::ranges::sort(v, std::greater<>()); for (size_t i = 1; i < v.size(); ++i) { v[i] += v[i - 1]; } int32_t m = state.size(); for (int32_t i = 0; i < m; ++i) { MaxPlusConv m(state[i], v); state[i] = m.merge(); } v.clear(); } void multiply(State& state, int64_t v) { state.push_back(state.back()); int32_t m = state.size(); int32_t n = state[0].size(); for (int32_t i = m - 2; i >= 0; --i) { for (int32_t j = 0; j < n; ++j) { state[i + 1][j] = std::max(state[i + 1][j], state[i][j] * v); } } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int32_t n, maxK; std::cin >> n >> maxK; State ans(1, std::vector(n + 1, 0)); std::vector buffer; for (int32_t i = 0; i < n; ++i) { char op; int64_t v; std::cin >> op >> v; if (op == 'a') { buffer.push_back(v); } else { if (!buffer.empty()) { add(ans, buffer); } multiply(ans, v); } } if (!buffer.empty()) { add(ans, buffer); } std::vector result(n + ans.size() + 1, 0); for (size_t i = 0; i < std::min(maxK + 1, ans.size()); ++i) { for (int32_t j = 0; j <= n; ++j) { result[i + j] = std::max(result[i + j], ans[i][j]); } } for (int32_t x = 1; x <= n; ++x) { result[x] = std::max(result[x], result[x - 1]); std::cout << result[x] << '\n'; } return 0; }