Code:
#include<bits/stdc++.h> #define js ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; typedef long long ll; const int maxn=1e4+7; int n,k; ll c[maxn],v[maxn],t[maxn]; bool solve(int x) { for(int i=1;i<=n;++i) t[i]=v[i]-c[i]*x; sort(t+1,t+1+n,greater<int>()); ll sum=0; for(int i=1;i<=k;++i) sum+=t[i]; return sum>=0; } int main() { js; int T; cin>>T; while(T--) { cin>>n>>k; for(int i=1;i<=n;++i) cin>>c[i]>>v[i]; int l=1,r=1e4; while(l<=r) { int mid=(l+r)>>1; if(solve(mid)) l=mid+1; else r=mid-1; } cout<<r<<endl; } }