Description
There is a building on one line, the first building has a layer, and each floor has 1 items of value.
It can take 1 units of time to complete the following 3 moves:
1. Go up or down one floor in the same building;
2. If you are on the top floor, you can access the 1 floor;
3. Move from the current building to the same floor of the adjacent building. If the adjacent building does not have a high current position, it will fall to the top level of the adjacent building.
Initially on the top floor of the first building, the unit time can be moved, take the item does not need time, and an item is taken once and then disappear.
The maximum total value that can be obtained.
Input
The first line is two positive integers.
The line is represented by two integers in each row.
Output
The output line is an integer representing the maximum total value.
Sample Input
3 3 2 1 1 5 3 4
Sample Output
14
HINT
Solution
Enumerate the farthest reaches of the building, and then greedy.
#include <cmath>#include<ctime>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#defineN 1000005using namespaceStd;typedefLong Longll;structhouse{ll H,v,n;} A[n];intn,u;ll Cnt[n],v[n],m,k,ans,sum,tmp;inlineintRead () {intret=0;CharC=GetChar (); while(!IsDigit (c)) C=GetChar (); while(IsDigit (c)) {ret= (ret<<1) + (ret<<3) +c-'0'; C=GetChar (); } returnret;} inline LL Rd () {LL ret=0LL;CharC=GetChar (); while(!IsDigit (c)) C=GetChar (); while(IsDigit (c)) {ret= (RET<<1LL) + (RET<<3LL) + (LL) (C-'0'); C=GetChar (); } returnret;} InlineBOOLcmp (House x,house y) {if(X.V!=Y.V)returnX.v>y.v; returnx.n<Y.N;} Inlinevoidinit () {n=read (); M=rd () +1LL; if(ll) (n) >m) n= (int) (m); for(intI=1; i<=n;++i) {a[i].h=rd (); V[i]=a[i].v=rd (); a[i].n=(ll) (i); } sort (A+1, A +1+n,cmp); for(intI=1; i<=n;++i) { ++cnt[a[i].n];--m; } for(intI=1; i<=n&&m;++i) {tmp=min (a[i].h-1ll,m); CNT[A[I].N]+=tmp;m-=tmp; } for(intI=1; i<=n;++i) ans+=cnt[i]*V[i]; for(u=1; u<=n;++u)if(cnt[a[u].n]!=a[u].h) Break; Sum=ans; for(intK=n;k>1;--k) {m=cnt[k];sum-=cnt[k]*V[k]; for(; u<=n;++u)if(a[u].n<k) {tmp=min (a[u].h-cnt[a[u].n],m); CNT[A[U].N]+=tmp;m-=tmp; Sum+=a[u].v*tmp; if(!m) Break; } ans=Max (ans,sum); } printf ("%lld\n", ans);}intMain () {Freopen ("training.in","R", stdin); Freopen ("Training.out","W", stdout); Init (); Fclose (stdin); Fclose (stdout); return 0;}
[Daily Training]training