// TLE Θ(N^3) 後ろからやって状態数削減、遷移は高速化せず #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #include #include #include constexpr int64_t inf = std::numeric_limits::max() / 2; int main() { int n; std::cin >> n; std::vector c(n + 1); std::vector v(n + 1); for (int i = 1; i <= n; ++i) { std::cin >> c[i] >> v[i]; } std::vector dp(n + 1, std::vector(n + 1, -inf)); dp[0][0] = 0; for (uint32_t i = n; i > 0; --i) { const uint32_t max_num = uint32_t(n) / i; // dp[num][sum] = max{ pd[num-p][sum-p*i]+p*v[i] | 0<=p<=c[i] } ⋃ {-∞} for (uint32_t num = max_num; num --> 0;) { for (uint32_t sum = n; sum --> num * i;) { if (dp[num][sum] == -inf) continue; const uint32_t pmax = std::min(max_num - num, (n - sum) / i); for (uint32_t p = 1; p <= pmax; ++p) { dp[num + p][sum + i * p] = std::max(dp[num + p][sum + i * p], dp[num][sum] + p * v[i]); } } } } for (int32_t k = 1; k <= n; ++k) { std::cout << *std::max_element(dp[k].begin(), dp[k].end()) << '\n'; } return 0; }