Listen to M says it's a greedy, and then I look at it.
I hear there's a classic greedy model.
#define Pendant
If it can be updated, then update, if not update, then from the previous state to find a replaceable (if any) is replaced
For this question, you can think of it this way
(Set the historical weight from bottom to top is W)
For two pendants, if 1 is above 2, there is c1>=w1+w,c2>=w c_1>=w_1+w,c_2>=w
For two pendants, if 2 is above 1, there is c2>=w2+w,c1>=w c_2>=w_2+w,c_1>=w
For either case, the following formula is obvious, then if 1 is better on top, then there is
C1− (w2+w) >c2− (w1+w) c_1-(w_2+w) >c_2-(w_1+w)
Vice versa
Simplification
is the
C1+W1>C2+W2 c_1+w_1>c_2+w_2
Then we can press this order, and then @ go up a few lines of the model
Well
Using Heaps to maintain
On the Code
#include <iostream> #include <cstdio> #include <algorithm> #include <
Cstring> #include <queue> using namespace std;
typedef long Long LL;
CONST LL MAXN = 200000 + 5; struct WP {LL c,w;}
W[MAXN];
BOOL Init (WP A,wp b) {return A.C + A.W < B.C + b.w;} priority_queue <int> q;
ll G[MAXN];
ll N;
ll l = 0;
int main () {scanf ("%lld", &n);
for (ll i = 1;i <= n;i + +) scanf ("%lld%lld", &W[I].C,&W[I].W);
Sort (w + 1,w + n + 1,init);
ll ZL = 0;
for (ll i = 1;i <= n;i + +) {if (W[i].c >= ZL) {Q.push (W[I].W);
ZL + = (W[I].W);
} else if (W[I].W < Q.top ()) {ZL-= Q.top ();
Q.pop ();
Q.push (W[I].W);
ZL + = W[I].W;
}} printf ("%lld\n%lld\n", Q.size (), ZL);
return 0; }