Official
F - Medicine Editorial by en_translator
On day one, he has to take \(\left( \sum b_i \right)\) pills. Simulate how the pills to take reduces as time passes to find when is the first day that he has to take \(K\) pills or less.
The smaller the medicine’s \(a_i\) is, the earlier he stops taking it, so sort them in ascending order of \(a_i\) firsthand. One can sort \((a_i,b_i)\) in the input in ascending order as follows, for example in C++:
vector<pair<int,int>> p(N);
for(int i=0;i<N;i++){
cin>>p[i].first>>p[i].second;
}
sort(p.begin(),p.end());
Then you can inspect each element of \(p\) in order to perform the simulation.
Sample code (C++)
#include <bits/stdc++.h>
using namespace std;
int main() {
int N,K;
cin>>N>>K;
vector<pair<int,int>> p(N);
for(int i=0;i<N;i++){
cin>>p[i].first>>p[i].second;
}
sort(p.begin(),p.end());
long long sum = 0;
for(int i=0;i<N;i++){
sum += p[i].second;
}
if(sum<=K)cout<<1<<endl;
else{
for(int i=0;i<p.size();i++){
if(sum<=K){
cout<<p[i-1].first+1<<endl;
return 0;
}
sum -= p[i].second;
}
cout<<p.back().first+1<<endl;
}
return 0;
}
posted:
last update: