Problem:
C: 買い出し - Shopping -
物語
磯野の姉であるサゾエさんは，『あれ』や『あれ』で遊んでいる磯野と中島のために夕食を作ってあげることにしました．あいにく，冷蔵庫には食材が残り僅かしか無く，サゾエさんは買い出しに行くことにしました．幾つか食材を買おうとしているサゾエさんですが，大変陽気であるため一度に一品のみレジで会計してしまいます．このままでは遊んでいた磯野と中島が買い物を終える前に帰ってきてしまい，夕食を準備することが出来ないかもしれません．そこで，サゾエさんの友人であるあなたは，サゾエさんのためにサゾエさんの買い物にかかる最小の時間を求めてください．
問題
とあるスーパーに
N
個のレジがあり，それぞれ
1
〜
N
までの番号がついている．
また，客が
M
人居て，それぞれ
1
〜
M
までの番号がついている．
i
番目の客は，時刻
a_i
に
c_i
番目のレジに並び，会計に
b_i
の時間がかかる．1人の客は一度だけ会計をする．
ここで，ある客が並んだレジに既に人が並んでいる場合，その客は既に並んでいる人全員の会計が終了した後に会計をする．
i
番目の客と，
i+1
番目の客が同じレジに並んでいたとすると，
i
番目の客の会計開始時刻が
t
であった場合，
i
番目の客の会計終了時刻は
t+b_i
，
i+1
番目の客の会計開始時刻は
t+b_i
，そして，
i+1
番目の客の会計終了時刻は，
t+b_i+b_{i+1}
となる．
サゾエさんは1人の客である．しかし，サゾエさんは例外的に
K
回会計をする．また，会計にかかる時間は0，すなわちサゾエさんの会計開始時刻を
t
とすると，会計終了時刻は
t
となり，次の客の会計開始時刻は
t
となる．その後，サゾエさんが再度レジに並ぶ際，会計終了時刻
t
から品定めにかかる時間
D
だけ経ってから並ばなければいけない．つまり，再度レジに並ぶことができる時刻は
t+D
となる．
また，サゾエさんは時刻
S
にスーパーにやって来る．そして，時刻
S
から
D
だけ経った時刻
S+D
に，初めてサゾエさんはレジに並ぶことが出来る．
N
，
M
，
K
，
D
，
S
と，
1
番目〜
M
番目までの客の情報がそれぞれ与えられるので，サゾエさんがスーパーに来てから最後の会計を終えるまでにかかる時間の最小値を求めよ．この時，異なる2人の客が同じ時刻に同じレジに並ぶことは無いとし，サゾエさんとほかの人が同時にレジに並ぼうとした場合，サゾエさんは常にほかの客に順番を譲ることとする．
入力形式
入力は次の形式で与えられる．
N
M
K
D
S
a_1
b_1
c_1
...
a_M
b_M
c_M
1行目にスーパーにあるレジの数
N
（
1 ≤ N ≤ 10^{15}
），訪れる客の数
M
(
1 ≤ M ≤ 10^5
)，サゾエさんがレジを回る回数
K
(
1 ≤ K ≤ 10^4
)，サゾエさんの品定めにかかる時間
D
(
1 ≤ D ≤ 10^4
)，サゾエさんがスーパーに来る時刻
S
(
1 ≤ S ≤ 10^4
)が空白区切りで与えられる．
続く
M
行に訪れる客の情報が与えられる．
M
行のうち，
i
(
1 ≤ i ≤ M
)行目には，
i
番目の客がレジに来る時刻
a_i
(
1 ≤ a_i ≤ 10^4
)，会計にかかる時間
b_i
(
1 ≤ b_i ≤ 10^4
)，やって来るレジの番号
c_i
(
1 ≤ c_i ≤ N
)が空白区切りで与えられる．ここで
2 ≤ M
の時，全ての
i
(
1 ≤ i ≤ M−1
)について，
a_i ≤ a_{i+1}
が成立する．
なお，入力が非常に大きくなる場合があるため，入力の受け取りには高速な関数を用いることを推奨する．
出力形式
サゾエさんがスーパーに来てから最後の会計を終えるまでにかかる時間の最小値を1行で出力せよ．
入力例1
3 9 3 2 3
1 2 3
1 1 2
2 3 1
3 4 2
4 1 3
4 1 1
5 1 1
6 2 3
7 2 2
出力例1
6
時間5にレジ3，時間7にレジ1，時間9にレジ2に並ぶ．
サゾエさんの入店時間が時間3なので初めてレジに並んでから時間6で3回の会計が終了する．
また，時間9に並ぶレジはレジ1，もしくはレジ3でも最適解になる
入力例2
3 9 3 1 3
1 2 3
1 1 2
2 3 1
3 4 2
4 1 3
4 1 1
5 1 1
6 2 3
7 2 2
出力例2
5
入力例3
1 3 3 2 1
1 1 1
2 2 1
3 2 1
出力例3
9

Submitted solutions:

code_snippet: // AOJ #2760 Shopping
// 2025.4.21

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define gc() getchar_unlocked()

int Cin() {
	int n = 0, c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

ll Cinll() {
	ll n = 0, c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

struct C { int a, b; ll c; };

int main(){
    ll N = Cinll();
    int M = Cin(), K = Cin(), D = Cin(), S = Cin();

    vector<C> v(M);
    unordered_set<ll> used;
    used.reserve(M);
    for(int i = 0; i < M; i++){
        v[i].a = Cin(), v[i].b = Cin(), v[i].c = Cin();
        used.insert(v[i].c);
    }
    if ((ll)used.size() < N) {
        cout << (ll)K * D << endl;
        return 0;
    }

    sort(v.begin(), v.end(), [](auto &x, auto &y){ return x.a < y.a; });

    vector<ll> f(N+1, 0);
    multiset<pair<ll,ll>> st;
    for (ll j = 1; j <= N; j++) st.insert({0, j});

    int idx = 0;
    ll t = S + D;
    ll last = 0;

    for (int i = 0; i < K; i++){
        while (idx < M && v[idx].a <= t) {
            int a = v[idx].a, b = v[idx].b;
            ll  c = v[idx].c;
            auto it = st.find({f[c], c});
            st.erase(it);
            ll nf = max(f[c], (ll)a) + b;
            f[c] = nf;
            st.insert({nf, c});
            idx++;
        }

        auto it = st.begin();
        ll free_t = it->first;
        ll reg = it->second;

        if (free_t <= t) {
            last = t;
            st.erase(it);
            f[reg] = t;
            st.insert({t, reg});
        } else last = free_t;
        t = last + D;
    }
    cout << (last - S) << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 15076
final_rank: 3

code_snippet: // AOJ #2760 Shopping
// 2025.3.2

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n = 0; int c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

ll Cinll() {  // 整数の入力
	ll n = 0; int c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(ll n) { // 整数の表示（出力）
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

struct Cust { int a, b; };

struct Reg {
    vector<Cust> v;
    int ptr;
    ll st;
    Reg() : ptr(0), st(0) {}
};

ll proc(Reg &r, ll t) {
    while(r.ptr < (int)r.v.size() && r.v[r.ptr].a <= t){
        r.st = max(r.st, (ll)r.v[r.ptr].a) + r.v[r.ptr].b;
        r.ptr++;
    }
    return max(t, r.st);
}

int main(){
    ll N = Cinll();
    int M = Cin(), K = Cin(), D = Cin(), S = Cin();
    unordered_map<ll, vector<Cust>> mp;
    mp.reserve(M*2);
    for (int i = 0; i < M; i++){
        int a = Cin(), b = Cin(); ll c = Cinll();
        mp[c].push_back({a, b});
    }
    if(N > (int)mp.size()){
        Cout((ll)K * D);
        return 0;
    }

    vector<Reg> Rg;
    for(auto &p : mp){
        auto &vec = p.second;
        sort(vec.begin(), vec.end(), [](const Cust &c1, const Cust &c2){
            return c1.a < c2.a;
        });
        Reg r;
        r.v = move(vec);
        Rg.push_back(move(r));
    }
    int R = Rg.size();

    ll jt = S + D;
    vector<ll> dp(R, 0);
    for (int r = 0; r < R; r++) dp[r] = proc(Rg[r], jt);

    for (int i = 2; i <= K; i++){
        ll m = LLONG_MAX, s = LLONG_MAX;
        int im = -1;
        for (int r = 0; r < R; r++){
            if(dp[r] < m){
                s = m;
                m = dp[r];
                im = r;
            } else if(dp[r] < s) s = dp[r];
        }
        vector<ll> ndp(R, 0);
        for (int r = 0; r < R; r++){
            ll base = ( (R==1 || r != im) ? m : s ) + D;
            ndp[r] = proc(Rg[r], base);
        }
        dp.swap(ndp);
    }
    ll t = *min_element(dp.begin(), dp.end());
    cout << t - S << endl;
    return 0;
}
accuracy: 0.6428571428571429
time_ms: 10
memory_kb: 11464
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <random>
#include <cassert>
#include <bitset>
#include <numeric>
#include <queue>
#include <unordered_map>
#include <climits>
#include <array>
#include <memory>
#include <iomanip>
#include <stack>
#include <set>
#include <map>



int main() {
	long long int n;
	int m, k, d, s; std::cin >> n >> m >> k >> d >> s;
	std::vector<std::tuple<int, int, long long int>> customer(m);
	for (auto& [a, b, c] : customer) {
		std::cin >> a >> b >> c;
	}

	std::unordered_map<long long int, int> endTime;
	const auto comparator = [](const std::pair<int, long long int>& a, const std::pair<int, long long int>& b) {return a.first > b.first; };
	std::priority_queue<std::pair<int, long long int>, std::vector<std::pair<int, long long int>>, decltype(comparator)> queue(comparator);
	for (const auto& [a, b, c] : customer) {
		endTime[c] = 0;
		queue.emplace(0, c);
	}
	if (endTime.size() < n) {
		endTime[-1] = 0;
		queue.emplace(0, -1);
	}
	int count{ 0 };
	int next{ s + d };
	for (const auto [a, b, c] : customer) {
		while (a > next && count < k) {
			while (endTime[queue.top().second] != queue.top().first) {
				queue.pop();
			}
			const auto time{ std::max(next, queue.top().first) };
			++count;
			next = time + d;
		}
		const auto start{ std::max(a, endTime[c]) };
		const auto end{ start + b };
		endTime[c] = end;
		queue.emplace(end, c);
	}
	for (; count < k; ++count) {
		while (endTime[queue.top().second] != queue.top().first) {
			queue.pop();
		}
		const auto time{ std::max(next, queue.top().first) };
		next = time + d;
	}
	const auto result{ next - d - s };
	std::cout << result << '\n';
}
accuracy: 1
time_ms: 60
memory_kb: 13988
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class RangeMin {
public:
	int size_ = 1;
	vector<long long> dat;

	void init(int sz) {
		size_ = 1;
		while (size_ <= sz) size_ *= 2;
		dat.resize(size_ * 2, 0);
	}

	void update(int pos, long long x) {
		pos += size_;
		dat[pos] = x;
		while (pos >= 2) {
			pos >>= 1;
			dat[pos] = min(dat[pos * 2], dat[pos * 2 + 1]);
		}
	}

	long long query_(int l, int r, int a, int b, int u) {
		if (l <= a && b <= r) return dat[u];
		if (r <= a || b <= l) return (1LL << 60);
		long long v1 = query_(l, r, a, (a + b) / 2, u * 2);
		long long v2 = query_(l, r, (a + b) / 2, b, u * 2 + 1);
		return min(v1, v2);
	}

	long long query(int l, int r) {
		return query_(l, r, 0, size_, 1);
	}
};

long long N;
long long M, A[1 << 18], B[1 << 18], C[1 << 18];
long long K;
long long D;
long long S;
long long Time[1 << 18];
int bar = 1;

int main() {
	// Step 1. Input
	cin >> N >> M >> K >> D >> S;
	for (int i = 1; i <= M; i++) cin >> A[i] >> B[i] >> C[i];

	// Step 2. Compression
	vector<long long> Zahyou;
	for (int i = 1; i <= M; i++) Zahyou.push_back(C[i]);
	sort(Zahyou.begin(), Zahyou.end());
	Zahyou.erase(unique(Zahyou.begin(), Zahyou.end()), Zahyou.end());
	if ((long long)Zahyou.size() < N) Zahyou.push_back(N + 1);
	for (int i = 1; i <= M; i++) C[i] = lower_bound(Zahyou.begin(), Zahyou.end(), C[i]) - Zahyou.begin();

	// Step 3. Simulation
	RangeMin Z; Z.init(Zahyou.size() + 2);
	long long CurrentTime = S + D;
	for (int i = 1; i <= K; i++) {
		while (bar <= M && A[bar] <= CurrentTime) {
			Time[C[bar]] = max(Time[C[bar]], A[bar]) + B[bar];
			Z.update(C[bar], Time[C[bar]]);
			bar += 1;
		}
		long long FinishTime = Z.query(0, Zahyou.size());
		CurrentTime = max(CurrentTime, FinishTime) + D;
	}

	// Step 4. Output
	cout << CurrentTime - D - S << endl;
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 12960
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define pp pair<ll,ll>
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
ll MOD=998244353;
int inf=1000001000;
ll INF=1e18+5;
ll mod=INF;

int main() {
    ll n;
    cin >> n;
    int mm,k,d,s;
    cin >> mm >> k >> d >> s;
    vector<pair<ll,pair<ll,ll>>> a(mm);
    rep(i,mm){
        cin >> a[i].first >> a[i].second.first >> a[i].second.second;
    }
    ll cc=0;
    map<ll,int> m;
    set<ll> ss;
    priority_queue<pp,vector<pp>,greater<pp>> q;
    priority_queue<pp,vector<pp>,greater<pp>> qq;
    rep(i,mm){
        m[a[i].second.second]=0;
        qq.push(mk(a[i].first,i));
        if (ss.find(a[i].second.second)==ss.end()){
            q.push(mk(0,a[i].second.second));
            cc++;
            ss.insert(a[i].second.second);
        }
    }
    if (cc<n){
        cout << d*k << endl;
        return 0;
    }
    qq.push(mk(s+d,INF));
    int y=1,ans;
    bool r=true;
    while(true){
        if (!r) break;
        ll f=qq.top().first,g=qq.top().second;
        qq.pop();
        if (g==INF){
            while(true){
                ll ff=q.top().first,gg=q.top().second;
                q.pop();
                if (m[gg]!=ff) continue;
                if (y==k) {
                    r=false;
                    ans=max(f+d,ff+d);
                }
                y++;
                // cout << ff << " " << f << endl;
                // cout << max(f+d,ff+d) << endl;
                qq.push(mk(max(f+d,ff+d),INF));
                break;
            }
        }
        else {
            m[a[g].second.second]=max(m[a[g].second.second]+a[g].second.first,f+a[g].second.first);
            q.push(mk(m[a[g].second.second],a[g].second.second));
        }
    }
    cout << ans-s-d << endl;
}
accuracy: 0.6428571428571429
time_ms: 120
memory_kb: 17364
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define pp pair<ll,ll>
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
ll MOD=998244353;
int inf=1000001000;
ll INF=1e18+5;
ll mod=INF;

int main() {
    ll n;
    cin >> n;
    int mm,k,d,s;
    cin >> mm >> k >> d >> s;
    vector<pair<ll,pair<ll,ll>>> a(mm);
    rep(i,mm){
        cin >> a[i].first >> a[i].second.first >> a[i].second.second;
    }
    ll cc=0;
    map<ll,int> m;
    set<ll> ss;
    priority_queue<pp,vector<pp>,greater<pp>> q;
    priority_queue<pp,vector<pp>,greater<pp>> qq;
    rep(i,mm){
        m[a[i].second.second]=0;
        qq.push(mk(a[i].first,i));
        if (ss.find(a[i].second.second)==ss.end()){
            q.push(mk(0,a[i].second.second));
            cc++;
            ss.insert(a[i].second.second);
        }
    }
    if (cc<n){
        cout << s+d*k << endl;
        return 0;
    }
    qq.push(mk(s+d,INF));
    int y=1,ans;
    bool r=true;
    while(true){
        if (!r) break;
        ll f=qq.top().first,g=qq.top().second;
        qq.pop();
        if (g==INF){
            while(true){
                ll ff=q.top().first,gg=q.top().second;
                q.pop();
                if (m[gg]!=ff) continue;
                if (y==k) {
                    r=false;
                    ans=max(f+d,ff+d);
                }
                y++;
                qq.push(mk(max(f+d,ff+d),INF));
                break;
            }
        }
        else {
            m[a[g].second.second]=max(m[a[g].second.second]+a[g].second.first,f+a[g].second.first);
            q.push(mk(m[a[g].second.second],a[g].second.second));
        }
    }
    cout << ans-s-d << endl;
}
accuracy: 0.07142857142857142
time_ms: 10
memory_kb: 5216
final_rank: 19

code_snippet: #include <bits/stdc++.h>

template <typename T,typename E>
struct LazySegmentTree{
private:
    typedef std::function<T(T,T)> F;
    typedef std::function<T(T,E)> G;
    typedef std::function<E(E,E)> H;
    typedef std::function<E(E,int)> P;
    int n;
    F cal;//function for merge
    G upd;//function for update
    H ecal;//function for evaluate
    P rcal;//function for range calculate
    std::vector<T> init;
    T initv;
    E opinit;
    std::vector<T> node;
    std::vector<E> lazy;
public:
    explicit LazySegmentTree(//特定の要素で初期化する場合
            int sz,
            F cal,
            G upd,
            H ecal,
            P rcal=[](E a,int b){return a;},
            T initv=std::numeric_limits<long long>::max(),
            E opinit=0
    ):
    cal(cal),
    upd(upd),
    ecal(ecal),
    rcal(rcal),
    initv(initv),
    opinit(opinit)
    {
        n=1;
        while(n<sz)n=n*2;
        node.resize(static_cast<unsigned int>(2 * n - 1), initv);
        for (int i = 0; i <sz ; ++i) node[i+n-1]=(T)0;
        for (int i = n-2; i >= 0 ; --i) node[i]=cal(node[2*i+1],node[2*i+2]);

        lazy.resize(static_cast<unsigned int>(2 * n - 1), opinit);
        for (int i = 0; i <sz ; ++i) lazy[i+n-1]=opinit;
        for (int i = n-2; i >= 0 ; --i) lazy[i]=ecal(lazy[2*i+1],lazy[2*i+2]);
    }

    void update(int p,int q,E val,int k=0,int l=0,int r=-1){//[p,q):0-indexed
        if(r<0)r=n;
        eval(k,r-l);
        if(r<=p||l>=q)return;
        if(p<=l&&r<=q){
            lazy[k]=ecal(lazy[k],val);
            eval(k,r-l);
        }
        else{
            update(p,q,val,2*k+1,l,(l+r)/2);
            update(p,q,val,2*k+2,(l+r)/2,r);
            node[k]=cal(node[2*k+1],node[2*k+2]);
        }
    }

    T query(int p,int q,int k=0,int l=0,int r=-1){//[p,q):0-indexed
        if(r<0)r=n;
        if(r<=p||l>=q)return initv;

        eval(k,r-l);
        if(p<=l&&r<=q)return node[k];
        T vl=query(p,q,2*k+1,l,(l+r)/2);
        T vr=query(p,q,2*k+2,(l+r)/2,r);
        return cal(vl,vr);
    }

    void eval(int k,int len){//k:0-indexed
        if(lazy[k]==opinit)return;
        node[k]=upd(node[k],rcal(lazy[k],len));
        if(k<n-1){
            lazy[2*k+1]=ecal(lazy[2*k+1],lazy[k]);
            lazy[2*k+2]=ecal(lazy[2*k+2],lazy[k]);
        }
        lazy[k]=opinit;
    }
};

using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,m,k,d,s;
    cin>>n>>m>>k>>d>>s;
    vector<pair<ll,pair<ll,ll>>> data;
    int ind=0;
    map<ll,int> zatu;
    for(int i = 0;i < m;++i) {
        ll a,b,c;
        cin>>a>>b>>c;
        if(zatu.find(c)==zatu.end()){
            zatu[c]=ind;
            ++ind;
        }
        data.emplace_back(a,make_pair(b,c));
    }
    if(ind<n){
        zatu[-1]=ind;
        ++ind;
    }
    sort(data.begin(),data.end());

    LazySegmentTree<ll,ll> st(
        ind,
        [](ll a,ll b)->ll{return min(a,b);},
        [](ll a,ll b)->ll{return max(a-b,0LL);},
        [](ll a,ll b)->ll{return a+b;}
    );

    ll szt=s+d;
    ll pszt=0;
    while(k>0&&szt<data[0].first){
        --k;
        szt+=d;
    }

    st.update(zatu[data[0].second.second],zatu[data[0].second.second]+1,-data[0].second.first);
    pszt=data[0].first;
    for(int i = 1;i < m;++i) {
        while(k>0&&szt<data[i].first){
            --k;
            st.update(0,ind,szt-pszt);
            pszt=szt;
            szt+=st.query(0,ind);
            szt+=d;
        }
        if(k<=0)break;

        st.update(0,ind,data[i].first-pszt);
        st.update(zatu[data[i].second.second],zatu[data[i].second.second]+1,-data[i].second.first);
        pszt=data[i].first;
    }
    while(k>0){
        --k;
        st.update(0,ind,szt-pszt);
        pszt=szt;
        szt+=st.query(0,ind);
        szt+=d;
    }

    cout<<szt-d-s<<endl;

    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 15408
final_rank: 8

code_snippet: #include <bits/stdc++.h>

template <typename T,typename E>
struct LazySegmentTree{
private:
    typedef std::function<T(T,T)> F;
    typedef std::function<T(T,E)> G;
    typedef std::function<E(E,E)> H;
    typedef std::function<E(E,int)> P;
    int n;
    F cal;//function for merge
    G upd;//function for update
    H ecal;//function for evaluate
    P rcal;//function for range calculate
    std::vector<T> init;
    T initv;
    E opinit;
    std::vector<T> node;
    std::vector<E> lazy;
public:
    explicit LazySegmentTree(//特定の要素で初期化する場合
            int sz,
            F cal,
            G upd,
            H ecal,
            P rcal=[](E a,int b){return a;},
            T initv=std::numeric_limits<long long>::max(),
            E opinit=0
    ):
    cal(cal),
    upd(upd),
    ecal(ecal),
    rcal(rcal),
    initv(initv),
    opinit(opinit)
    {
        n=1;
        while(n<sz)n=n*2;
        node.resize(static_cast<unsigned int>(2 * n - 1), initv);
        for (int i = 0; i <sz ; ++i) node[i+n-1]=(T)0;
        for (int i = n-2; i >= 0 ; --i) node[i]=cal(node[2*i+1],node[2*i+2]);

        lazy.resize(static_cast<unsigned int>(2 * n - 1), opinit);
        for (int i = 0; i <sz ; ++i) lazy[i+n-1]=opinit;
        for (int i = n-2; i >= 0 ; --i) lazy[i]=ecal(lazy[2*i+1],lazy[2*i+2]);
    }

    void update(int p,int q,E val,int k=0,int l=0,int r=-1){//[p,q):0-indexed
        if(r<0)r=n;
        eval(k,r-l);
        if(r<=p||l>=q)return;
        if(p<=l&&r<=q){
            lazy[k]=ecal(lazy[k],val);
            eval(k,r-l);
        }
        else{
            update(p,q,val,2*k+1,l,(l+r)/2);
            update(p,q,val,2*k+2,(l+r)/2,r);
            node[k]=cal(node[2*k+1],node[2*k+2]);
        }
    }

    T query(int p,int q,int k=0,int l=0,int r=-1){//[p,q):0-indexed
        if(r<0)r=n;
        if(r<=p||l>=q)return initv;

        eval(k,r-l);
        if(p<=l&&r<=q)return node[k];
        T vl=query(p,q,2*k+1,l,(l+r)/2);
        T vr=query(p,q,2*k+2,(l+r)/2,r);
        return cal(vl,vr);
    }

    void eval(int k,int len){//k:0-indexed
        if(lazy[k]==opinit)return;
        node[k]=upd(node[k],rcal(lazy[k],len));
        if(k<n-1){
            lazy[2*k+1]=ecal(lazy[2*k+1],lazy[k]);
            lazy[2*k+2]=ecal(lazy[2*k+2],lazy[k]);
        }
        lazy[k]=opinit;
    }
};

using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,m,k,d,s;
    cin>>n>>m>>k>>d>>s;
    vector<pair<ll,pair<ll,ll>>> data;
    int ind=0;
    map<ll,int> zatu;
    for(int i = 0;i < m;++i) {
        ll a,b,c;
        cin>>a>>b>>c;
        if(zatu.find(c)==zatu.end()){
            zatu[c]=ind;
            ++ind;
        }
        data.emplace_back(a,make_pair(b,c));
    }
    sort(data.begin(),data.end());

    LazySegmentTree<ll,ll> st(
        ind,
        [](ll a,ll b)->ll{return min(a,b);},
        [](ll a,ll b)->ll{return max(a-b,0LL);},
        [](ll a,ll b)->ll{return a+b;}
    );

    ll szt=s+d;
    ll pszt=0;
    while(k>0&&szt<data[0].first){
        --k;
        szt+=d;
    }

    st.update(zatu[data[0].second.second],zatu[data[0].second.second]+1,-data[0].second.first);
    pszt=data[0].first;
    for(int i = 1;i < m;++i) {
        while(k>0&&szt<data[i].first){
            --k;
            st.update(0,ind,szt-pszt);
            pszt=szt;
            szt+=st.query(0,ind);
            szt+=d;
        }
        if(k<=0)break;

        st.update(0,ind,data[i].first-pszt);
        st.update(zatu[data[i].second.second],zatu[data[i].second.second]+1,-data[i].second.first);
        pszt=data[i].first;
    }
    while(k>0){
        --k;
        st.update(0,ind,szt-pszt);
        pszt=szt;
        szt+=st.query(0,ind);
        szt+=d;
    }

    cout<<szt-d-s<<endl;

    return 0;
}
accuracy: 0.7380952380952381
time_ms: 120
memory_kb: 14708
final_rank: 10

code_snippet: #include <bits/stdc++.h>

template <typename T,typename E>
struct LazySegmentTree{
private:
    typedef std::function<T(T,T)> F;
    typedef std::function<T(T,E)> G;
    typedef std::function<E(E,E)> H;
    typedef std::function<E(E,int)> P;
    int n;
    F cal;//function for merge
    G upd;//function for update
    H ecal;//function for evaluate
    P rcal;//function for range calculate
    std::vector<T> init;
    T initv;
    E opinit;
    std::vector<T> node;
    std::vector<E> lazy;
public:
    explicit LazySegmentTree(//特定の要素で初期化する場合
            int sz,
            F cal,
            G upd,
            H ecal,
            P rcal=[](E a,int b){return a*b;},
            T initv=std::numeric_limits<long long>::max(),
            E opinit=0
    ):
    cal(cal),
    upd(upd),
    ecal(ecal),
    rcal(rcal),
    initv(initv),
    opinit(opinit)
    {
        n=1;
        while(n<sz)n=n*2;
        node.resize(static_cast<unsigned int>(2 * n - 1), initv);
        for (int i = 0; i <sz ; ++i) node[i+n-1]=(T)0;
        for (int i = n-2; i >= 0 ; --i) node[i]=cal(node[2*i+1],node[2*i+2]);

        lazy.resize(static_cast<unsigned int>(2 * n - 1), opinit);
        for (int i = 0; i <sz ; ++i) lazy[i+n-1]=opinit;
        for (int i = n-2; i >= 0 ; --i) lazy[i]=ecal(lazy[2*i+1],lazy[2*i+2]);
    }

    void update(int p,int q,E val,int k=0,int l=0,int r=-1){//[p,q):0-indexed
        if(r<0)r=n;
        eval(k,r-l);
        if(r<=p||l>=q)return;
        if(p<=l&&r<=q){
            lazy[k]=ecal(lazy[k],val);
            eval(k,r-l);
        }
        else{
            update(p,q,val,2*k+1,l,(l+r)/2);
            update(p,q,val,2*k+2,(l+r)/2,r);
            node[k]=cal(node[2*k+1],node[2*k+2]);
        }
    }

    T query(int p,int q,int k=0,int l=0,int r=-1){//[p,q):0-indexed
        if(r<0)r=n;
        if(r<=p||l>=q)return initv;

        eval(k,r-l);
        if(p<=l&&r<=q)return node[k];
        T vl=query(p,q,2*k+1,l,(l+r)/2);
        T vr=query(p,q,2*k+2,(l+r)/2,r);
        return cal(vl,vr);
    }

    void eval(int k,int len){//k:0-indexed
        if(lazy[k]==opinit)return;
        node[k]=upd(node[k],rcal(lazy[k],len));
        if(k<n-1){
            lazy[2*k+1]=ecal(lazy[2*k+1],lazy[k]);
            lazy[2*k+2]=ecal(lazy[2*k+2],lazy[k]);
        }
        lazy[k]=opinit;
    }
};

using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,m,k,d,s;
    cin>>n>>m>>k>>d>>s;
    vector<pair<ll,pair<ll,ll>>> data;
    int ind=0;
    map<ll,int> zatu;
    for(int i = 0;i < m;++i) {
        ll a,b,c;
        cin>>a>>b>>c;
        if(zatu.find(c)==zatu.end()){
            zatu[c]=ind;
            ++ind;
        }
        data.emplace_back(a,make_pair(b,c));
    }
    sort(data.begin(),data.end());

    LazySegmentTree<ll,ll> st(
        ind,
        [](ll a,ll b)->ll{return min(a,b);},
        [](ll a,ll b)->ll{return max(a-b,0LL);},
        [](ll a,ll b)->ll{return a+b;}
    );

    ll szt=s+d;
    ll pszt=0;
    while(k>0&&szt<data[0].first){
        --k;
        szt+=d;
    }

    st.update(zatu[data[0].second.second],zatu[data[0].second.second]+1,-data[0].second.first);
    pszt=data[0].first;
    for(int i = 1;i < m;++i) {
        while(k>0&&szt<data[i].first){
            --k;
            st.update(0,ind,szt-pszt);
            pszt=szt;
            szt+=st.query(0,ind);
            szt+=d;
        }
        if(k<=0)break;

        st.update(0,ind,data[i].first-pszt);
        st.update(zatu[data[i].second.second],zatu[data[i].second.second]+1,-data[i].second.first);
        pszt=data[i].first;
    }
    while(k>0){
        --k;
        st.update(0,ind,szt-pszt);
        pszt=szt;
        szt+=st.query(0,ind);
        szt+=d;
    }

    cout<<szt-d-s<<endl;

    return 0;
}
accuracy: 0.6428571428571429
time_ms: 110
memory_kb: 14548
final_rank: 12

code_snippet: #include <bits/stdc++.h>

template <typename T,typename E>
struct LazySegmentTree{
private:
    typedef std::function<T(T,T)> F;
    typedef std::function<T(T,E)> G;
    typedef std::function<E(E,E)> H;
    typedef std::function<E(E,int)> P;
    int n;
    F cal;//function for merge
    G upd;//function for update
    H ecal;//function for evaluate
    P rcal;//function for range calculate
    std::vector<T> init;
    T initv;
    E opinit;
    std::vector<T> node;
    std::vector<E> lazy;
public:
    explicit LazySegmentTree(//特定の要素で初期化する場合
            int sz,
            F cal,
            G upd,
            H ecal,
            P rcal=[](E a,int b){return a*b;},
            T initv=std::numeric_limits<long long>::max(),
            E opinit=0
    ):
    cal(cal),
    upd(upd),
    ecal(ecal),
    rcal(rcal),
    initv(initv),
    opinit(opinit)
    {
        n=1;
        while(n<sz)n=n*2;
        node.resize(static_cast<unsigned int>(2 * n - 1), initv);
        for (int i = 0; i <sz ; ++i) node[i+n-1]=(T)0;
        for (int i = n-2; i >= 0 ; --i) node[i]=cal(node[2*i+1],node[2*i+2]);

        lazy.resize(static_cast<unsigned int>(2 * n - 1), opinit);
        for (int i = 0; i <sz ; ++i) lazy[i+n-1]=opinit;
        for (int i = n-2; i >= 0 ; --i) lazy[i]=ecal(lazy[2*i+1],lazy[2*i+2]);
    }

    void update(int p,int q,E val,int k=0,int l=0,int r=-1){//[p,q):0-indexed
        if(r<0)r=n;
        eval(k,r-l);
        if(r<=p||l>=q)return;
        if(p<=l&&r<=q){
            lazy[k]=ecal(lazy[k],val);
            eval(k,r-l);
        }
        else{
            update(p,q,val,2*k+1,l,(l+r)/2);
            update(p,q,val,2*k+2,(l+r)/2,r);
            node[k]=cal(node[2*k+1],node[2*k+2]);
        }
    }

    T query(int p,int q,int k=0,int l=0,int r=-1){//[p,q):0-indexed
        if(r<0)r=n;
        if(r<=p||l>=q)return initv;

        eval(k,r-l);
        if(p<=l&&r<=q)return node[k];
        T vl=query(p,q,2*k+1,l,(l+r)/2);
        T vr=query(p,q,2*k+2,(l+r)/2,r);
        return cal(vl,vr);
    }

    void eval(int k,int len){//k:0-indexed
        if(lazy[k]==opinit)return;
        node[k]=upd(node[k],rcal(lazy[k],len));
        if(k<n-1){
            lazy[2*k+1]=ecal(lazy[2*k+1],lazy[k]);
            lazy[2*k+2]=ecal(lazy[2*k+2],lazy[k]);
        }
        lazy[k]=opinit;
    }
};

using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,m,k,d,s;
    cin>>n>>m>>k>>d>>s;
    vector<pair<ll,pair<ll,ll>>> data;
    int ind=0;
    map<ll,int> zatu;
    for(int i = 0;i < m;++i) {
        ll a,b,c;
        cin>>a>>b>>c;
        if(zatu.find(c)==zatu.end()){
            zatu[c]=ind;
            ++ind;
        }
        data.emplace_back(a,make_pair(b,c));
    }
    sort(data.begin(),data.end());

    LazySegmentTree<ll,ll> st(
        ind,
        [](ll a,ll b)->ll{return min(a,b);},
        [](ll a,ll b)->ll{return max(a-b,0LL);},
        [](ll a,ll b)->ll{return a+b;}
    );

    ll szt=s+d;

    while(k>0&&szt<data[0].first){
        --k;
        szt+=d;
    }

    st.update(zatu[data[0].second.second],zatu[data[0].second.second]+1,-data[0].second.first);

    for(int i = 1;i < m;++i) {
        while(k>0&&szt<data[i].first){
            --k;
            st.update(0,ind,szt-data[i-1].first);
            szt+=st.query(0,ind);
            szt+=d;
        }
        if(k<=0)break;

        st.update(0,ind,data[i].first-data[i-1].first);
        st.update(zatu[data[i].second.second],zatu[data[i].second.second]+1,-data[i].second.first);
    }
    ll pszt=data[m-1].first;
    while(k>0){
        --k;
        st.update(0,ind,szt-pszt);
        pszt=szt;
        szt+=st.query(0,ind);
        szt+=d;
    }

    cout<<szt-d-s<<endl;

    return 0;
}
accuracy: 0.40476190476190477
time_ms: 110
memory_kb: 14564
final_rank: 16

code_snippet: #include <bits/stdc++.h>

template <typename T,typename E>
struct LazySegmentTree{
private:
    typedef std::function<T(T,T)> F;
    typedef std::function<T(T,E)> G;
    typedef std::function<E(E,E)> H;
    typedef std::function<E(E,int)> P;
    int n;
    F cal;//function for merge
    G upd;//function for update
    H ecal;//function for evaluate
    P rcal;//function for range calculate
    std::vector<T> init;
    T initv;
    E opinit;
    std::vector<T> node;
    std::vector<E> lazy;
public:
    explicit LazySegmentTree(//特定の要素で初期化する場合
            int sz,
            F cal,
            G upd,
            H ecal,
            P rcal=[](E a,int b){return a*b;},
            T initv=std::numeric_limits<long long>::max(),
            E opinit=0
    ):
    cal(cal),
    upd(upd),
    ecal(ecal),
    rcal(rcal),
    initv(initv),
    opinit(opinit)
    {
        n=1;
        while(n<sz)n=n*2;
        node.resize(static_cast<unsigned int>(2 * n - 1), initv);
        for (int i = 0; i <sz ; ++i) node[i+n-1]=(T)0;
        for (int i = n-2; i >= 0 ; --i) node[i]=cal(node[2*i+1],node[2*i+2]);

        lazy.resize(static_cast<unsigned int>(2 * n - 1), opinit);
        for (int i = 0; i <sz ; ++i) lazy[i+n-1]=opinit;
        for (int i = n-2; i >= 0 ; --i) lazy[i]=ecal(lazy[2*i+1],lazy[2*i+2]);
    }

    void update(int p,int q,E val,int k=0,int l=0,int r=-1){//[p,q):0-indexed
        if(r<0)r=n;
        eval(k,r-l);
        if(r<=p||l>=q)return;
        if(p<=l&&r<=q){
            lazy[k]=ecal(lazy[k],val);
            eval(k,r-l);
        }
        else{
            update(p,q,val,2*k+1,l,(l+r)/2);
            update(p,q,val,2*k+2,(l+r)/2,r);
            node[k]=cal(node[2*k+1],node[2*k+2]);
        }
    }

    T query(int p,int q,int k=0,int l=0,int r=-1){//[p,q):0-indexed
        if(r<0)r=n;
        if(r<=p||l>=q)return initv;

        eval(k,r-l);
        if(p<=l&&r<=q)return node[k];
        T vl=query(p,q,2*k+1,l,(l+r)/2);
        T vr=query(p,q,2*k+2,(l+r)/2,r);
        return cal(vl,vr);
    }

    void eval(int k,int len){//k:0-indexed
        if(lazy[k]==opinit)return;
        node[k]=upd(node[k],rcal(lazy[k],len));
        if(k<n-1){
            lazy[2*k+1]=ecal(lazy[2*k+1],lazy[k]);
            lazy[2*k+2]=ecal(lazy[2*k+2],lazy[k]);
        }
        lazy[k]=opinit;
    }
};

using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,m,k,d,s;
    cin>>n>>m>>k>>d>>s;
    vector<pair<ll,pair<ll,ll>>> data;
    int ind=0;
    map<ll,int> zatu;
    for(int i = 0;i < m;++i) {
        ll a,b,c;
        cin>>a>>b>>c;
        if(zatu.find(c)==zatu.end()){
            zatu[c]=ind;
            ++ind;
        }
        data.emplace_back(a,make_pair(b,c));
    }
    sort(data.begin(),data.end());

    LazySegmentTree<ll,ll> st(
        ind,
        [](ll a,ll b)->ll{return min(a,b);},
        [](ll a,ll b)->ll{return max(a-b,0LL);},
        [](ll a,ll b)->ll{return a+b;}
    );

    ll szt=s+d;

    while(k>0&&szt<data[0].first){
        --k;
        szt+=d;
    }

    st.update(zatu[data[0].second.second],zatu[data[0].second.second]+1,-data[0].second.first);

    for(int i = 1;i < m;++i) {
        while(k>0&&szt<data[i].first){
            --k;
            st.update(0,ind,szt-data[i-1].first);
            szt+=st.query(0,ind);
            szt+=d;

        }
        if(k<=0)break;

        st.update(0,ind,data[i].first-data[i-1].first);
        st.update(zatu[data[i].second.second],zatu[data[i].second.second]+1,-data[i].second.first);
    }
    while(k>0){
        --k;
        st.update(0,ind,szt-data[m-1].first);
        szt+=st.query(0,ind);
        szt+=d;
    }

    cout<<szt-d-s<<endl;

    return 0;
}
accuracy: 0.40476190476190477
time_ms: 110
memory_kb: 14664
final_rank: 17

code_snippet: #include <bits/stdc++.h>

template <typename T,typename E>
struct LazySegmentTree{
private:
    typedef std::function<T(T,T)> F;
    typedef std::function<T(T,E)> G;
    typedef std::function<E(E,E)> H;
    typedef std::function<E(E,int)> P;
    int n;
    F cal;//function for merge
    G upd;//function for update
    H ecal;//function for evaluate
    P rcal;//function for range calculate
    std::vector<T> init;
    T initv;
    E opinit;
    std::vector<T> node;
    std::vector<E> lazy;
public:
    explicit LazySegmentTree(//特定の要素で初期化する場合
            int sz,
            F cal,
            G upd,
            H ecal,
            P rcal=[](E a,int b){return a*b;},
            T initv=std::numeric_limits<long long>::max(),
            E opinit=0
    ):
    cal(cal),
    upd(upd),
    ecal(ecal),
    rcal(rcal),
    initv(initv),
    opinit(opinit)
    {
        n=1;
        while(n<sz)n=n*2;
        node.resize(static_cast<unsigned int>(2 * n - 1), initv);
        for (int i = 0; i <sz ; ++i) node[i+n-1]=(T)0;
        for (int i = n-2; i >= 0 ; --i) node[i]=cal(node[2*i+1],node[2*i+2]);

        lazy.resize(static_cast<unsigned int>(2 * n - 1), opinit);
        for (int i = 0; i <sz ; ++i) lazy[i+n-1]=opinit;
        for (int i = n-2; i >= 0 ; --i) lazy[i]=ecal(lazy[2*i+1],lazy[2*i+2]);
    }

    void update(int p,int q,E val,int k=0,int l=0,int r=-1){//[p,q):0-indexed
        if(r<0)r=n;
        eval(k,r-l);
        if(r<=p||l>=q)return;
        if(p<=l&&r<=q){
            lazy[k]=ecal(lazy[k],val);
            eval(k,r-l);
        }
        else{
            update(p,q,val,2*k+1,l,(l+r)/2);
            update(p,q,val,2*k+2,(l+r)/2,r);
            node[k]=cal(node[2*k+1],node[2*k+2]);
        }
    }

    T query(int p,int q,int k=0,int l=0,int r=-1){//[p,q):0-indexed
        if(r<0)r=n;
        if(r<=p||l>=q)return initv;

        eval(k,r-l);
        if(p<=l&&r<=q)return node[k];
        T vl=query(p,q,2*k+1,l,(l+r)/2);
        T vr=query(p,q,2*k+2,(l+r)/2,r);
        return cal(vl,vr);
    }

    void eval(int k,int len){//k:0-indexed
        if(lazy[k]==opinit)return;
        node[k]=upd(node[k],rcal(lazy[k],len));
        if(k<n-1){
            lazy[2*k+1]=ecal(lazy[2*k+1],lazy[k]);
            lazy[2*k+2]=ecal(lazy[2*k+2],lazy[k]);
        }
        lazy[k]=opinit;
    }
};

using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,m,k,d,s;
    cin>>n>>m>>k>>d>>s;
    vector<pair<ll,pair<ll,ll>>> data;
    int ind=0;
    map<ll,int> zatu;
    for(int i = 0;i < m;++i) {
        ll a,b,c;
        cin>>a>>b>>c;
        if(zatu.find(c)==zatu.end()){
            zatu[c]=ind;
            ++ind;
        }
        data.emplace_back(a,make_pair(b,c));
    }
    sort(data.begin(),data.end());

    LazySegmentTree<ll,ll> st(
        ind,
        [](ll a,ll b)->ll{return min(a,b);},
        [](ll a,ll b)->ll{return max(a-b,0LL);},
        [](ll a,ll b)->ll{return a+b;}
    );

    ll szt=s;

    while(k>0&&szt<data[0].first){
        --k;
        szt+=d;
    }

    st.update(zatu[data[0].second.second],zatu[data[0].second.second]+1,-data[0].second.first);

    for(int i = 1;i < m;++i) {
        while(k>0&&szt<data[i].first){
            --k;
            st.update(0,ind,szt-data[i-1].first);
            szt+=st.query(0,ind);
            szt+=d;

        }
        if(k<=0)break;

        st.update(0,ind,data[i].first-data[i-1].first);
        st.update(zatu[data[i].second.second],zatu[data[i].second.second]+1,-data[i].second.first);
    }

    while(k>0){
        --k;
        st.update(0,ind,szt-data[m-1].first);
        szt+=st.query(0,ind);
        szt+=d;
    }

    cout<<szt-k-s<<endl;

    return 0;
}
accuracy: 0.38095238095238093
time_ms: 120
memory_kb: 14588
final_rank: 18

code_snippet: #include "iostream"
#include "random"
#include "string"
#include "bitset"
#include "algorithm"
#include "map"
#include "queue"
#include "list"
#include "set"
#include "climits"
#include "iomanip"
#include "stack"
#include "functional"
#include "unordered_map"
#include "unordered_set"

using namespace std;
using ll = long long int;
using PII = pair<ll, ll>;

int a[100001];
int b[100001];
long long int c[100001];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	long long int N, M, K, D, S;
	cin >> N >> M >> K >> D >> S;
	int minus = S;
	for (int i = 0; i < M; i++) {
		cin >> a[i] >> b[i] >> c[i];
	}
	map<long long int, int>m;
	set<pair<int, long long int>>s;
	int index = 0;
	while (K) {
		while (index < M&&a[index] <= S + D) {
			auto it = s.find({ m[c[index]], c[index] });
			if (it != s.end()) {
				s.erase(it);
			}
			s.insert({ max(a[index],m[c[index]]) + b[index],c[index] });
			m[c[index]] = max(a[index], m[c[index]]) + b[index];
			index++;
		}
		if (m.size() != N) {
			S += D;
		}
		else {
			if (s.empty()) {
				S += D;
			}
			else {
				auto box = *s.begin();
				S = max(box.first, (int)S + (int)D);
			}
		}
		K--;
	}
	cout << S - minus << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 16696
final_rank: 7

code_snippet: #include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	long long N;
	int M, K, D, S;
	cin >> N >> M >> K >> D >> S;
	vector<int> A(M), B(M); vector<long long> PreC(M);
	for (int i = 0; i < M; ++i) {
		cin >> A[i] >> B[i] >> PreC[i];
	}
	vector<long long> SC = PreC;
	sort(SC.begin(), SC.end());
	SC.erase(unique(SC.begin(), SC.end()), SC.end());
	vector<int> C(M);
	for (int i = 0; i < M; ++i) {
		C[i] = lower_bound(SC.begin(), SC.end(), PreC[i]) - SC.begin();
	}
	N = min(N, (long long)(SC.size() + 1));
	vector<int> wait(N);
	priority_queue<pair<int, int> > que;
	for (int i = 0; i < N; ++i) {
		que.push(make_pair(0, i));
	}
	int firstS = S, lastS = -1;
	for (int i = 0; i < M; ++i) {
		while (S + D < A[i] && K > 0) {
			while (-que.top().first != wait[que.top().second]) que.pop();
			pair<int, int> u = que.top();
			S = max(S + D, -u.first);
			--K;
			if (K == 0) lastS = S;
		}
		wait[C[i]] = max(wait[C[i]], A[i]) + B[i];
		que.push(make_pair(-wait[C[i]], C[i]));
	}
	while (K > 0) {
		while (-que.top().first != wait[que.top().second]) que.pop();
		pair<int, int> u = que.top();
		S = max(S + D, -u.first);
		--K;
		if (K == 0) lastS = S;
	}
	cout << lastS - firstS << endl;
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 8064
final_rank: 2

code_snippet: #include <cstdio>
#include <cstdint>
#include <cctype>
#include <climits>
#include <cinttypes>
#include <set>
#include <map>
#include <algorithm>
#include <vector>
#include <utility>

#define fprintf(...) void(0)

int main() {
  uintmax_t N;
  size_t M;
  int K, D, S;
  scanf("%ju %zu %d %d %d", &N, &M, &K, &D, &S);

  if (N > M)
    return !printf("%jd\n", intmax_t(K)*D);

  std::map<intmax_t, std::vector<std::pair<int, int>>> regi;
  std::set<intmax_t> used;
  for (size_t i=0; i<M; ++i) {
    int a, b;
    intmax_t c;
    scanf("%d %d %jd", &a, &b, &c);
    regi[c].emplace_back(a, a+b);
    used.emplace(c);
  }
  if (used.size() < N)
    return !printf("%jd\n", intmax_t(K)*D);

  std::set<std::pair<int, int>> tsurai;
  for (const auto &pp: regi) {
    std::vector<std::pair<int, int>> cur;
    int P=0;
    int last=0;
    for (const auto &p: pp.second) {
      int s=p.first, e=p.second;
      if (P+last <= s) {
        cur.emplace_back(P+last, s);
        P = 0;
      } else {
        P -= s-last;
      }

      P += e-s;
      last = s;
    }
    cur.emplace_back(last+P, INT_MAX);
    for (const auto &p: cur)
      tsurai.insert(p);
  }

  std::vector<std::pair<int, int>> disjoint;
  for (const auto &p: tsurai) {
    fprintf(stderr, "- [%d, %d)\n", p.first, p.second);
    if (disjoint.empty()) {
      disjoint.push_back(p);
      continue;
    }
    if (p.first <= disjoint.back().second) {
      disjoint.back().second = std::max(disjoint.back().second, p.second);
    } else {
      disjoint.push_back(p);
    }
  }

  for (const auto &p: disjoint) {
    fprintf(stderr, "[%d, %d)\n", p.first, p.second);
  }

  auto it=disjoint.cbegin();
  int SS=S;
  for (int i=0; i<K; ++i) {
    S += D;
    while (it->second <= S) ++it;
   
    fprintf(stderr, "+ [%d, %d)\n", it->first, it->second);
    fprintf(stderr, "> %d\n", S);
    S = std::max(S, it->first);
    fprintf(stderr, "< %d\n", S);
  }
  printf("%d\n", S-SS);
}
accuracy: 0.40476190476190477
time_ms: 70
memory_kb: 12612
final_rank: 15

code_snippet: #include <cstdio>
#include <cstdint>
#include <cctype>
#include <climits>
#include <cinttypes>
#include <set>
#include <map>
#include <algorithm>
#include <vector>
#include <utility>

#define fprintf(...) void(0)

int main() {
  intmax_t N;
  int M, K, D, S;
  scanf("%jd %d %d %d %d", &N, &M, &K, &D, &S);

  if (N > M)
    return !printf("%jd\n", intmax_t(K)*D);

  std::map<intmax_t, std::vector<std::pair<int, int>>> regi;
  for (int i=0; i<M; ++i) {
    int a, b;
    intmax_t c;
    scanf("%d %d %jd", &a, &b, &c);
    regi[c].emplace_back(a, a+b);
  }

  std::set<std::pair<int, int>> tsurai;
  for (const auto &pp: regi) {
    std::vector<std::pair<int, int>> cur;
    int P=0;
    int last=0;
    for (const auto &p: pp.second) {
      int s=p.first, e=p.second;
      if (P+last == s) {
        cur.emplace_back(s, s);
        P = 0;
      } else if (P+last < s) {
        cur.emplace_back(P+last, s);
        P = 0;
      } else {
        P -= s-last;
      }

      P += e-s;
      last = s;
    }
    cur.emplace_back(last+P, INT_MAX);
    for (const auto &p: cur)
      tsurai.insert(p);
  }

  std::vector<std::pair<int, int>> disjoint;
  for (const auto &p: tsurai) {
    if (disjoint.empty()) {
      disjoint.push_back(p);
      continue;
    }
    if (p.first <= disjoint.back().second) {
      disjoint.back().second = p.second;
    } else {
      disjoint.push_back(p);
    }
  }

  for (const auto &p: disjoint) {
    fprintf(stderr, "[%d, %d)\n", p.first, p.second);
  }

  auto it=disjoint.cbegin();
  int SS=S;
  for (int i=0; i<K; ++i) {
    S += D;
    while (it->second <= S) ++it;
   
    fprintf(stderr, "[%d, %d)\n", it->first, it->second);
    S = std::max(S, it->first);
    if (i+1 == K)
      return !printf("%d\n", S-SS);
  }
}
accuracy: 0.40476190476190477
time_ms: 70
memory_kb: 10880
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

using lint = long long;
using Time = pair< lint, int >;

struct Customer {
	lint a, b, c;
	bool operator > (const Customer& obj) const { return a == obj.a ? c > obj.c : a > obj.a; }
};

lint N, M, K, D, S, last[100005];

int main() {
	cin >> N >> M >> K >> D >> S;
	
	if (N > M) {
		cout << K * D << endl;
		return 0;
	}
	
	priority_queue< Customer, vector< Customer >, greater< Customer > > que;
	for (int i=0; i<M; ++i) {
		lint a, b, c;
		cin >> a >> b >> c;
		--c;
		que.push(Customer{a, b, c});
	}
	que.push(Customer{S+D, 0, N});
	
	set< Time > reg;
	for (int i=0; i<N; ++i) reg.insert(Time(0, i));
	memset(last, 0, sizeof(last));
	
	lint ans = K * D;
	while (!que.empty() && K > 0) {
		Customer cust = que.top(); que.pop();
		if (cust.c == N) { // sazoe
			lint fast = (*reg.begin()).first;
			ans += max(0LL, fast - cust.a);
			que.push(Customer{max(fast, cust.a)+D, 0, N});
			--K;
		}
		else { // others
			int i = cust.c;
			reg.erase(Time(last[i], i));
			last[i] = max(last[i], cust.a) + cust.b;
			reg.insert(Time(last[i], i));
		}
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 130
memory_kb: 11976
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

using lint = long long;
using Time = pair< lint, int >;

struct Customer {
	lint a, c;
	bool operator > (const Customer& obj) const { return a == obj.a ? c > obj.c : a > obj.a; }
};

lint N, M, K, D, S, last[100005];
queue< lint > b_que[100005];

int main() {
	cin >> N >> M >> K >> D >> S;
	
	if (N > M) {
		cout << K * D << endl;
		return 0;
	}
	
	priority_queue< Customer, vector< Customer >, greater< Customer > > que;
	for (int i=0; i<M; ++i) {
		lint a, b, c;
		cin >> a >> b >> c;
		--c;
		que.push(Customer{a, c});
		b_que[c].push(b);
	}
	que.push(Customer{S+D, N});
	
	set< Time > reg;
	for (int i=0; i<N; ++i) {
		reg.insert(Time(0, i));
		last[i] = 0;
	}
	
	lint ans = K * D;
	while (!que.empty() && K > 0) {
		Customer cust = que.top(); que.pop();
		if (cust.c == N) { // sazoe
			lint fast = (*reg.begin()).first;
			ans += max(0LL, fast - cust.a);
			que.push(Customer{max(fast, cust.a)+D, N});
			--K;
		}
		else { // others
			int i = (*reg.begin()).second;
			reg.erase(reg.begin());
			last[i] = max(last[i], cust.a) + b_que[i].front();
			b_que[i].pop();
			reg.insert(Time(last[i], i));
		}
	}
	cout << ans << endl;
}
accuracy: 0.023809523809523808
time_ms: 30
memory_kb: 70332
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
using ll = long long;
template <typename T>
constexpr T INF = numeric_limits<T>::max() / 16;
template <typename Monoid>
class SegmentTree
{
private:
    static constexpr int sz(const int n)
    {
        int ans = 1;
        for (; n > ans; ans <<= 1) {}
        return ans;
    }

public:
    using BaseMonoid = Monoid;
    using T = typename Monoid::T;

    SegmentTree(const int n) : data_num(n), half(sz(n)), value(half << 1, Monoid::id()) {}
    template <typename InIt>
    SegmentTree(const InIt first, const InIt last) : data_num(distance(first, last)), half(sz(data_num)), value(half << 1, Monoid::id())
    {
        copy(first, last, value.begin() + half);
        for (int i = half - 1; i >= 1; i--) { up(i); }
    }
    T get(const int a) const
    {
        assert(0 <= a and a < data_num);
        return value[a + half];
    }
    void set(int a, const T& val)
    {
        assert(0 <= a and a < data_num);
        value[a += half] = val;
        while (a >>= 1) { up(a); }
    }
    T accumulate(int L, int R) const
    {
        assert(0 <= L and L < R and R <= data_num);
        T accl = Monoid::id(), accr = Monoid::id();
        for (L += half, R += half; L < R; L >>= 1, R >>= 1) {
            if (L & 1) { accl = acc(accl, value[L++]); }
            if (R & 1) { accr = acc(value[--R], accr); }
        }
        return acc(accl, accr);
    }
    template <typename F, typename QueryOp, typename AccOp>
    F query(int L, int R, const QueryOp& query_op, const AccOp& acc_op) const
    {
        assert(0 <= L and L < R and R <= data_num);
        constexpr F id = F{};  // Fの単位元(AccOpについての)
        F accl = id, accr = id;
        for (L += half, R += half; L < R; L >>= 1, R >>= 1) {
            if (L & 1) { accl = acc_op(accl, query_op(value[L++])); }
            if (R & 1) { accr = acc_op(query_op(value[--R]), accr); }
        }
        return acc_op(accl, accr);
    }
    template <typename Pred>
    int partitionPoint(const int L, const int R, const Pred& pred) const
    {
        auto prec = fix([&](auto&& self, const int index, const int left, const int right, const T& offset) -> pair<T, int> {
            if (right <= L or R <= left or pred(acc(offset, value[index]))) { return {Monoid::id(), -1}; }
            if (index >= half) { return {value[index], index - half}; }
            const pair<T, int> lans = self(self, index << 1, left, (left + right) >> 1, offset);
            if (lans.second != -1) { return lans; }
            return self(self, index << 1 | 1, (left + right) >> 1, right, acc(offset, lans.first));
        });
        const int ans = prec(1, 0, half, Monoid::id()).second;
        return ans == -1 ? R : ans;
    }

    vector<T> data() const { return vector<T>(value.begin() + half, value.begin() + half + data_num); }
    void debug() const
    {
        for (int l = 1; l < (half << 1); l <<= 1) {
            for (int i = l; i < (l << 1); i++) { cout << value[i] << ","; }
            cout << endl;
        }
    }

private:
    void up(const int i) { value[i] = acc(value[i << 1], value[i << 1 | 1]); }
    const int data_num;  // Num of valid data on leaves.
    const int half;
    vector<T> value;
    const Monoid acc{};
};

template <typename T>
ostream& operator<<(ostream& os, const SegmentTree<T>& seg)
{
    os << "[";
    for (const auto e : seg.data()) { os << e << ","; }
    return (os << "]" << endl);
}

struct Min
{
    using T = ll;
    T operator()(const T& a, const T& b) const { return min(a, b); }
    static constexpr T id() { return INF<T>; }
};
template <typename T, typename A>
inline ostream& operator<<(ostream& os, const vector<T, A>& v)
{
    os << "[";
    for (const auto& p : v) { os << p << ","; }
    return (os << "]" << endl);
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, M, K, D, S;
    cin >> N >> M >> K >> D >> S;
    if (N > M) {
        for (int i = 0, d = 0; i < M; i++) { cin >> d >> d >> d; }
        cout << D * K << endl;
    } else {
        vector<ll> a(M), b(M), c(M);
        for (int i = 0; i < M; i++) { cin >> a[i] >> b[i] >> c[i]; }
        ll ans = S + D;
        vector<ll> zero(N, 0);
        SegmentTree<Min> seg(zero.begin(), zero.end());
        int pos = 0;
        for (int i = 0; i < K; i++) {
            for (; pos < M and a[pos] <= ans; pos++) { seg.set(c[pos] - 1, max(seg.get(c[pos] - 1), a[pos]) + b[pos]); }
            ans = max(ans, seg.accumulate(0, N)) + D;
        }
        cout << ans - S - D << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 8044
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define int long long

const int inf = 1LL<<55;

struct SegmentTree {
  vector<int> data;
  int sz;
  SegmentTree(int n) {
    sz = 1; while(sz < n) sz *= 2;
    data.resize(2*sz-1, inf);
  }
  void update(int k, int x) {
    k += sz-1;
    data[k] = x;
    while(k > 0) {
      k = (k-1)/2;
      data[k] = min(data[2*k+1], data[2*k+2]);
    }
  }
  int query(int a, int b, int k, int l, int r) {
    if(r <= a || b <= l) return inf;
    if(a <= l && r <= b) return data[k];
    int vl = query(a, b, 2*k+1, l, (l+r)/2);
    int vr = query(a, b, 2*k+2, (l+r)/2, r);
    return min(vl, vr);
  }
  int query(int a, int b) {
    return query(a, b, 0, 0, sz);
  }
};

signed main() {
  int n, m, k, d, s;
  cin >> n >> m >> k >> d >> s;
  if(n > m) {
    cout << d*k << endl;
    return 0;
  }
  vector<int> a(m), b(m), c(m);
  vector<int> cmpC;
  for(int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    --c[i];
    //cmpC.push_back(c[i]);
  }
  /*
  for(int i = 0; i < m; ++i) {
    c[i] = lower_bound(cmpC.begin(), cmpC.end(), c[i])-cmpC.begin();
  }
  */
  struct State {
    int t, a, b, c;
    State(){}
    State(int t, int a, int b, int c):t(t), a(a), b(b), c(c){}
    bool operator < (const State& s) const {
      return a == s.a ? t > s.t : a > s.a;
    }
  };
  priority_queue<State> que;
  for(int i = 0; i < m; ++i) que.emplace(1, a[i], b[i], c[i]);
  que.emplace(2, s+d, 0, 0);
  int sz = n;//max(n, (int)cmpC.size());
  SegmentTree seg(sz);
  for(int i = 0; i < sz; ++i) seg.update(i, 0);
  while(!que.empty()) {
    State st = que.top(); que.pop();
    //cout<<st.t<<" "<<st.a<<" "<<st.b<<" "<<st.c<<endl;
    if(st.t == 1) {
      int tmp = seg.query(st.c, st.c+1);
      //cout<<tmp<<endl;
      seg.update(st.c, max(st.a, tmp)+st.b);
    } else {
      int tmp = seg.query(0, sz);
      //cout<<sz<<endl;
      //cout<<tmp<<endl;
      --k;
      if(k == 0) {
	cout << max(st.a, tmp)-s << endl;
	return 0;
      }
      que.emplace(2, max(st.a, tmp)+d, 0, 0);
    }
  }
  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 10364
final_rank: 6