Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type pc=array[1..3] of longint;
- var h:array[1..40000] of pc;
- a,t,c:pc;
- i,n,k,z:longint;
- res,m:int64;
- procedure swap(var a,b:pc);
- var y:pc;
- begin
- y:=a;
- a:=b;
- b:=y;
- end;
- procedure sd;
- var i,x:longint;
- begin
- i:=1;
- x:=1;
- while 2*x<=k do
- begin
- if h[2*x][1]<h[i][1] then i:=2*x;
- if (2*x+1<=k) and (h[2*x+1][1]<h[i][1]) then i:=2*x+1;
- if x=i then break;
- swap(h[i],h[x]);
- x:=i;
- end;
- end;
- procedure remove;
- begin
- h[1]:=h[k];
- Dec(k);
- sd;
- end;
- procedure add(a:pc);
- var c:longint;
- begin
- Inc(k);
- h[k]:=a;
- c:=k;
- while (c>1) and (h[c div 2][1]>h[c][1]) do
- begin swap(h[c],h[c div 2]); c:=c div 2; end;
- end;
- begin
- assign(input,'horizon.in'); reset(input);
- assign(output,'horizon.out'); rewrite(output);
- readln(n);
- k:=0;
- res:=0;
- for i:=1 to n do
- begin
- for z:=1 to 3 do read(a[z]);
- add(a);
- end;
- a:=h[1];
- remove;
- while k>0 do
- begin
- t:=h[1];
- if (t[1]>=a[2]) then
- begin
- m:=a[2]-a[1];
- res:=res+m*a[3];
- a:=t;
- remove;
- continue;
- end;
- if a[3]<=t[3] then
- begin
- m:=t[1]-a[1];
- res:=res+m*a[3];
- if a[2]>t[2] then
- begin
- c[1]:=t[2];
- c[2]:=a[2];
- c[3]:=a[3];
- add(c);
- end;
- a:=t;
- remove;
- end
- else begin
- if a[2]<t[2] then
- begin
- t[1]:=a[2];
- h[1]:=t;
- sd;
- end
- else remove;
- end;
- end;
- m:=a[2]-a[1];
- res:=res+m*a[3];
- writeln(res);
- close(input); close(output);
- end.
Advertisement
Add Comment
Please, Sign In to add comment