← Home
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using int128 = __int128;
 
#pragma GCC optimize("O3", "unroll_loops")
 
#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vi vector<int>
#define pii pair<int, int>
#define rg(n) (int i = 0; i < n; i++)
#define rst cout << endl
 
template <typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& p) {
	out << p.first << " " << p.second << "| ";
	return out;
}
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& p) {
	in >> p.first >> p.second;
	return in;
}
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
	for (auto& el : v) out << el << " ";
	return out;
}
template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
	for (auto& el : v) in >> el;
	return in;
}
 
const ll INF64 = 1e18;
const int MAXN = 50001;
const int LOG = 18;
const ll MOD = 998244353;
const int BLOCK = 400;
const ld EPS = 1e-9;
const int MAXQ = 500005;
 
int k;
 
struct Node {
	int t, s, p;
	int id;
	Node(int t = -1, int s = -1, int p = -1, int id = -1) : t(t), s(s), p(p), id(id) {}
	bool operator<(const Node& oth) const {
		if (oth.t == t) return p > oth.p;
		return t < oth.t;
	}
};
ostream& operator<<(ostream& out, Node& n) {
	out << n.t << " " << n.s << " " << n.p << " " << n.id;
	return out;
}
 
int time_out[MAXN];
int cur_time = 0;
 
bool check(int p, set<Node>& arr, int nec, Node nd) {
	nd.p = p;
	arr.insert( nd );
	// for (auto el : arr) cout << el << endl;
	// rst;
 
	priority_queue<pair<pii, int>> pq;
 
	for (auto cur : arr) {
 
		// cout << cur.t << " " << cur.s << " " << cur.p << endl;
		// cout << cur_time << endl;
 
		while (!pq.empty() && cur.t > cur_time) {
			auto [top, id] = pq.top();
			int dt = min(cur.t - cur_time, top.second);
			top.second -= dt;
			cur_time += dt;
			pq.pop();
			if (top.second == 0) {
				time_out[id] = cur_time;
			}
			else {
				pq.push( {top, id} );
			}
		}
		cur_time = cur.t;
		pq.push( {{cur.p, cur.s }, cur.id} );
 
 
	}
	while (!pq.empty()) {
		auto [top, id] = pq.top();
		pq.pop();
		cur_time += top.second;
		time_out[id] = cur_time;
	}
 
	arr.erase( nd );
 
	return (time_out[nec] >= k);
}
 
signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
	freopen( "input.txt", "r", stdin );
	freopen("output.txt", "w", stdout);
 
	int n;
	cin >> n;
	set<Node> arr;
	vector<int> priority;
	int nec = -1;
	Node nd;
	for rg(n) {
		Node c;
		cin >> c.t >> c.s >> c.p;
		c.id = i;
		priority.push_back( c.p );
 
		if (c.p == -1) {
			nec = i;
			nd = c;
		}
		else {
			arr.insert( c );
		}
	}
	cin >> k;
	sort(all(priority));
	//cout << nec << endl;
	//for (auto el : arr) cout << el << endl;
 
	int l = 0, r = 1e9 + 1;
	while (r - l > 1) {
		int mid = (l + r) / 2;
	 	if (check( mid, arr, nec, nd )) {
	 		l = mid;
	 	}
	 	else {
	 		r = mid;
	 	}
		cur_time = 0;
	 }
 
	 for (int i = 1; i < priority.size(); i++) {
		 if (l == priority[i]) {
			 if (l - 1 == priority[i - 1]) {
				 l++;
			 }
		 	else {
		 		--l;
		 	}
		 }
	 }
	 check( l, arr, nec, nd );
	 cout << l << "\n";
	 for rg(n) {
	 	cout << time_out[i] << " ";
	 }
	 rst;
 
	//check( 4, arr, nec, nd );
	return 0;
}