This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp          make_pair
#define sep         ' '
#define endl        '\n'
#define F	        first
#define S	        second
#define pb          push_back
#define all(x)      (x).begin(),(x).end()
#define kill(res)	cout << res << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define fast_io     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io     freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 2e3 + 50;
const ll Mod = 1e9 + 7;
ll n, k, w[N], d[N];
vector<pll> vec;
vector<ll> best[N], help[N];
int main(){
    fast_io;
    cin >> n >> k;
    for(ll i = 1; i <= n; i++){
        ll ww, dd;
        cin >> ww >> dd;
        vec.pb({dd, ww});
    }
    sort(all(vec));
    for(ll i = 0; i < n; i++){
        w[i+1] = vec[i].S; d[i+1] = vec[i].F;
    }
    best[0].pb(0);
    for(ll i = 1; i <= n; i++){
        for(ll j = 0; j < d[i]; j++){
            for(ll x: best[j]) help[j+1].pb(x+w[i]);
        }
        for(ll j = 0; j <= n; j++){
            for(ll x: help[j]) best[j].pb(x);
            help[j].clear(); sort(all(best[j]));
            while(best[j].size() > k) best[j].pop_back();
        }
    }
    ll cnt = 0, cur = n, cur2 = 0;
    while(cnt + best[cur].size() <= k){
        for(ll x: best[cur]) cout << cur << sep << x << endl;
        cnt += best[cur].size(); cur--;
    }
    k -= cnt;
    while(k--) cout << cur << sep << best[cur][cur2++] << endl;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:58:34: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   58 |             while(best[j].size() > k) best[j].pop_back();
      |                   ~~~~~~~~~~~~~~~^~~
Main.cpp:63:34: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'll' {aka 'long long int'} [-Wsign-compare]
   63 |     while(cnt + best[cur].size() <= k){
      |           ~~~~~~~~~~~~~~~~~~~~~~~^~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |