题目传送门
从最后倒着往前推,贪心模拟即可
#include<iostream> #include<cstdio> #include<vector> #include<queue> using namespace std; int m,n,ti,o[501],ans; vector<int> a[501]; priority_queue<int> p; inline int max(int s,int d) { if(s > d) return s; return d; } int main() { scanf("%d%d",&m,&n); for(int i = 1;i <= n; i++) { int y; scanf("%d",&y); ti = max(ti,y); a[y].push_back(i); } for(int i = 1;i <= n; i++) { scanf("%d",&o[i]); ans += o[i]; } for(int i = ti;i >= 1; i--) { for(int j = 0;j < a[i].size(); j++) p.push(o[a[i][j]]); if(!p.empty()) { ans -= p.top(); p.pop(); } } printf("%d",m - ans); return 0; }