Please sign in first.
Official
B - Pepper Addiction Editorial by en_translator
We will divide into cases depending on pepper type.
- Let \(s_k\) be the maximum amount of type-\(k\) pepper that can be sprinkled on dishes.
- If \(s_k \le C_k\), type-\(k\) pepper can be sprinkled at most \(s_k\) grams.
- If \(s_k > C_k\), type-\(k\) pepper can be sprinkled at most \(C_k\) grams.
Obviously, there is a way to sprinkle pepper to achieve this maximum (for example, he can sprinkle pepper as much as possible in ascending order of dish numbers).
Thus, what we are asked to do in this problem is:
- Find \(s_k\) for each pepper type.
- Find the sum of the upper bound found above for each pepper type.
\(s_k\) can be found in the manner of bucket sort, and summing up the upper bounds for each pepper type can be achieved with a for loop.
Sample code (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin >> n >> m;
vector<int> c(m+1);
vector<int> s(m+1,0);
for(int i=1;i<=m;i++){
cin >> c[i];
}
for(int i=0;i<n;i++){
int a,b;
cin >> a >> b;
s[a]+=b;
}
int res=0;
for(int i=1;i<=m;i++){
res+=min(c[i],s[i]);
}
cout << res << "\n";
return 0;
}
posted:
last update: