题意:邮递员送快递,有容量限制,贪心取远的先。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,k;
struct node {
ll x;
ll m;
};
bool cmp(node a,node b) {
return a.x<b.x;
}
int main() {
vector<node> a(1500),b(1500);
cin>>n>>k;
ll c,d;
while(n--) {
cin>>c>>d;
if(c>0)
a.push_back((node) {
c,d
});
if(c==0)
continue;
if(c<0)
b.push_back((node) {
-c,d
});
}
sort(a.begin(),a.end(),cmp);
sort(b.begin(),b.end(),cmp);
ll ans = 0;
ll tmp = 0;
ll i = a.size()-1;
while(i>=0) {
ans += a[i].m/k*a[i].x*2;
if(a[i].m%k) {
int s = k-a[i].m%k;
ans+=a[i].x*2;
while(s) {
i--;
if(i<0)
break;
if(s>=a[i].m) s -= a[i].m;
else a[i].m -= s,s = 0;
}
} else
i--;
}
// cout<<ans<<endl;
i=b.size()-1;
while(i>=0) {
ans += b[i].m/k*b[i].x*2;
if(b[i].m%k) {
int s = k-b[i].m%k;
ans+=b[i].x*2;
while(s) {
i--;
if(i<0)
break;
if(s>=b[i].m) s -= b[i].m;
else b[i].m -= s,s = 0;
}
} else
i--;
}
cout<<ans<<endl;
return 0;
}