Description
小A作为一个乖乖好学生,回到家后总是一丝不苟地完成老师布置的作业。
这天,老师给小A布置了n项作业,每一项作业都有截止时间di和价值vi(你可以理解成每做完一份作业就要快递过去给老师,且快递是不耗费时间的),每完成一项作业便可获得其价值。但小A发现,自己每一个单位时间内只能完成其中的一项作业,请你告诉小A,他最多可以获得多少价值。
Input
第一行有一个正整数n,表示作业的数量。
接下来n行,每行两个正整数表示di和vi。
Output
输出一行,表示小A最多可以获得的价值。
Sample Input
5
1 9
7 6
8 8
1 8
8 6
Sample Output
29
Hint
【样例说明】
考虑第三组数据,因为只有一个项目所以只好每天都安排这个。
小A第一天完成第一项作业,第二天完成第二项作业,第三天完成第3项作业,第四天完成第5项作业。
【数据规模与约定】
对于前30%的数据,n<=100.
对于前60%的数据,n<=1000,di<=n
对于100%的数据,n<=100000,di,vi<=1000000000.
分析:
题解。
代码:
const
maxn=300001;
var
a:array [0..maxn] of longint;
x,y:array [0..maxn] of longint;
i,j,t,n,ss,tt:longint;
ans:int64;
procedure qsort(l,r:longint);
var
i,j,key,temp:longint;
begin
if l>=r then exit;
i:=l;j:=r;
key:=x[l+random(r-l+1)];
repeat
while (x[i]<key) do inc(i);
while (x[j]>key) do dec(j);
if i<=j then
begin
temp:=x[i];x[i]:=x[j];x[j]:=temp;
temp:=y[i];y[i]:=y[j];y[j]:=temp;
inc(i);dec(j);
end;
until i>j;
qsort(l,j);
qsort(i,r);
end;
procedure insert1(x:longint);
var i,temp:longint;
begin
inc(t);
a[t]:=x;
i:=t;
while (i>1) and (a[i]>a[i shr 1]) do
begin
temp:=a[i];
a[i]:=a[i shr 1];
a[i shr 1]:=temp;
i:=i shr 1;
end;
end;
function delete1:longint;
var i,temp,s:longint;
flag:boolean;
begin
delete1:=a[1];
a[1]:=a[t];
dec(t);
i:=1;
flag:=true;
while flag and (i shl 1<=t) do
begin
if (i shl 1+1>t) or (a[i shl 1]>a[i shl 1+1]) then s:=i shl 1
else s:=i shl 1+1;
if a[s]>a[i] then
begin
temp:=a[i];
a[i]:=a[s];
a[s]:=temp;
i:=s;
end
else flag:=false;
end;
end;
begin
readln(n);
for i:=1 to n do
begin
readln(ss,tt);
x[i]:=ss;
y[i]:=tt;
end;
qsort(1,n);
for i:=1 to n do
if x[i]>n then x[i]:=n;
j:=n;
for i:=x[n] downto 1 do
begin
while (x[j]>=i) and (j>=1) do
begin
insert1(y[j]);
dec(j);
end;
if t>=1 then ans:=ans+delete1;
end;
writeln(ans);
end.