E. Printer

time limit per test

4 seconds

memory limit per test

256 megabytes

input

input.txt

output

output.txt

Let's consider a network printer that functions like that. It starts working at time 0. In each second it can print one page of a text. At some moments of time the printer receives printing tasks. We know that a printer received n tasks. Let's number the tasks by consecutive integers from 1 to n. Then the task number i is characterised by three integers: ti is the time when the task came, si is the task's volume (in pages) and pi is the task's priority. The priorities of all tasks are distinct.

When the printer receives a task, the task goes to the queue and remains there until all pages from this task are printed. The printer chooses a page to print each time when it either stops printing some page or when it is free and receives a new task. Among all tasks that are in the queue at this moment, the printer chooses the task with the highest priority and next second prints an unprinted page from this task. You can assume that a task goes to the queue immediately, that's why if a task has just arrived by time t, the printer can already choose it for printing.

You are given full information about all tasks except for one: you don't know this task's priority. However, we know the time when the last page from this task was finished printing. Given this information, find the unknown priority value and determine the moments of time when the printer finished printing each task.

Input

The first line contains integer n (1 ≤ n ≤ 50000). Next n lines describe the tasks. The i-th of these lines contains three integers tisi and pi, separated by single spaces (0 ≤ ti ≤ 109, 1 ≤ si, pi ≤ 109). Exactly one task (let's assume that his number is x) has number -1 written instead of the priority. All priorities are different. The last line contains integer T — the time when the printer finished printing the last page of task x (1 ≤ T ≤ 1015). Numbers ti are not necessarily distinct. The tasks in the input are written in the arbitrary order.

Output

In the first line print integer px — the priority of the task number x (1 ≤ px ≤ 109, remember that all priorities should be distinct). Then print n integers, the i-th of them represents the moment of time when the last page of the task number i finished printing.

It is guaranteed that at least one solution exists. If there are multiple solutions, print any of them.

Examples

input

Copy


3 4 3 -1 0 2 2 1 3 3 7


output

Copy


4 7 8 4


input

Copy


3 3 1 2 2 3 3 3 1 -1 4


output

Copy


4 7 6 4


Note

Let's consider the first test case. Let's assume that the unknown priority equals 4, then the printer's actions for each second are as follows:

  • the beginning of the 1-st second (time 0). The queue has task 2. The printer prints the first page of this task;
  • the beginning of the 2-nd second (time 1). The queue has tasks 2 and 3. The printer prints the first page of task 3;
  • the beginning of the 3-rd second (time 2). The queue has tasks 2 and 3. The printer prints the second page of task 3;
  • the beginning of the 4-th second (time 3). The queue has tasks 2 and 3. The printer prints the third (last) page of task 3. Thus, by the end of the 4-th second this task will have been printed;
  • the beginning of the 5-th second (time 4). The queue has tasks 2 and 1. The printer prints the first page of task 1;
  • the beginning of the 6-th second (time 5). The queue has tasks 2 and 1. The printer prints the second page of task 1;
  • the beginning of the 7-th second (time 6). The queue has tasks 2 and 1. The printer prints the third (last) page of task 1. Thus, by the end of the 7-th second this task will have been printed;
  • the beginning of the 8-th second (time 7). The queue has task 2. The printer prints the second (last) page of task 2. Thus, by the end of the 8-th second this task will have been printed.

In the end, task number 1 will have been printed by the end of the 7-th second, as was required. And tasks 2 and 3 are printed by the end of the of the 8-th and the 4-th second correspondingly.

 

题意:

有n份文件需要打印,每行输入该文件传入时间第ti秒,需要打印的时间si秒,和打印优先级pi。每一秒打印机都打印优先级最高的文件,传入的N份文件中有一份优先级为-1代表未知。已知那份优先级未知的文件打印结束的时间为T,求优先级未知的文件的优先级可以为多少(注意优先级不能相同),按顺序输出每份文件打印结束的时间。
分析:

二分优先级,如果该任务提前完成则说明他的优先级给他的过高,晚的时间完成说明他的优先级过低,check函数里面用优先队列模拟打印任务的过程即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=500005;
LL n,et;
int pos;
struct Node
{
LL st,v,en;
LL p;
int id;
friend bool operator <(const Node c,const Node d)
{
return c.p<d.p;
}

};
LL ans[N];
bool cmp1(Node x,Node y)
{
return x.st<y.st;
}
bool cmp2(Node x,Node y)
{
return x.id<y.id;
}
priority_queue<Node> pq;
LL now=0;
int flag=0;

int solve_pq(LL limit)
{
while(!pq.empty()&&now<=limit)
{
Node temp=pq.top();
pq.pop();
if(temp.v+now<=limit)
{
now+=temp.v;
temp.v=0;
ans[temp.id]=now;
if(temp.id==pos&&flag==0)
{
if(now==et)
{
return 1;
}
else if(now<et)
{
return 0;
}
else
{
return -1;
}
}

}
else
{
temp.v-=(limit-now);
now=limit;
pq.push(temp);
return -2;
}
}

now=limit;
return -2;
}
Node b[N];
int check(LL mid)
{
now=b[1].st;
while(!pq.empty())
pq.pop();

for(int i=1; i<n; i++)
{
if(pos==b[i].id)
b[i].p=mid;

if(pq.empty()||pq.top().p<b[i].p)
{
if(now+b[i].v<=b[i+1].st)
{
now+=b[i].v;
b[i].v=0;
ans[b[i].id]=now;
if(b[i].id==pos&&flag==0)
{

if(now==et)
{
return 1;
}
else if(now<et)
{
return 0;
}
else
{
return -1;
}
}
if(now<b[i+1].st)
{
int rt=solve_pq(b[i+1].st);
if(rt!=-2)
{
return rt;
}
}
else
now=b[i+1].st;
}
else
{
b[i].v-=(b[i+1].st-now);
now=b[i+1].st;
pq.push(b[i]);

}
}
else
{
pq.push(b[i]);
int rt=solve_pq(b[i+1].st);
if(rt!=-2)
{
return rt;
}
}

}
if(b[n].id==pos)
b[n].p=mid;

if(pq.empty()||pq.top().p<b[n].p)
{
now+=b[n].v;
b[n].v=0;
ans[b[n].id]=now;
if(b[n].id==pos&&flag==0)
{

if(now==et)
{
return 1;
}
else if(now<et)
{
return 0;
}
else
{
return -1;
}
}
if(!pq.empty())
{
int rt=solve_pq(1e18);
if(rt!=-2)
{
return rt;
}
}
}
else
{
pq.push(b[n]);
int rt=solve_pq(1e18);
if(rt!=-2)
{
return rt;
}
}
}
map<LL,int> mp;
Node a[N];
int main()
{

//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);

LL maxx=0;
scanf("%d",&n);

for(int i=1; i<=n; i++)
{
scanf("%lld%lld%lld",&a[i].st,&a[i].v,&a[i].p);
mp[a[i].p]=1;
a[i].id=i;
maxx=max(a[i].p,maxx);
if(a[i].p==-1)
{
pos=i;
}
}
scanf("%lld",&et);
sort(a+1,a+n+1,cmp1);

LL l=0,r=maxx+1;
LL res=0;
while(l<r)
{
for(int i=1; i<=n; i++)
{
b[i]=a[i];
}
LL mid=(l+r)>>1;
/*while(mp[mid]==1)
mid++; */
int temp=check(mid);
if(temp==1)
{
res=mid;
break;
}
else if(temp==0)
r=mid;
else
l=mid+1;
}
for(int i=1; i<=n; i++)
{
b[i]=a[i];
}
printf("%lld\n",res);

flag=1;
check(res);
for(int i=1; i<=n; i++)
{

printf("%lld ",ans[i]);
}
return 0;
}