#include #include #include using namespace std; using ll = long long; #include #include using B = bitset<20002>; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin>>n; vector v(n),t(n); for(int i = 0;i>v[i]>>t[i]; ll sum = 0; for(int i = 0;i idx(n); for(int i = 0;i t[j] + v[j] - 1; }); auto calc = [&](ll now) { B dp; for(int i = now;i<=mx;i++) dp.set(i); int ok = mx; B mask; for(int i = 0;i<=mx;i++) mask.set(i); for(int i = 0;it[ni]+v[ni]-1;j--) mask.reset(j); ok = min(ok,t[ni]+v[ni]-1); ndp |= (dp&mask) >> v[ni]; swap(dp,ndp); } return dp[0]; }; while(right-left>1){ ll mid = (right+left) / 2; if(calc(mid)) left = mid; else right = mid; } cout<