Problem:
Problem H: Sequence
Problem
項数$L$の等差数列が$N$個ある。$i$番目の等差数列の初項は$a_i$で公差は$d_i$である。1番目の数列から順番に1番目の数列, 2番目の数列, ..., $N$番目の数列と並べ、そうしてできた数列を$A$とする。その後、$A$から長さ$K$の任意の区間を選んで、新たに数列$B$を作るとき、数列$B$の和を最大化せよ。
Input
入力は以下の形式で与えられる。
$N$ $L$ $K$
$a_1$ $d_1$
$a_2$ $d_2$
...
$a_N$ $d_N$
1行目に3つの整数$N$, $L$, $K$が空白区切りで与えられる。
続く$N$行に、$i$個目の等差数列の情報$a_i$, $d_i$が空白区切りで与えられる。
Constraints
入力は以下の条件を満たす。
$1 \leq N \leq 10^5$
$1 \leq L \leq 10^5$
$1 \leq K \leq N\times L$
$0 \leq a_i \leq 10^5$
$1 \leq d_i \leq 10^3$
入力はすべて整数
Output
数列$B$の和の最大値を1行に出力する。
Sample Input 1
3 3 5
0 3
2 2
4 1
Sample Output 1
25
数列$A$は$0,3,6,2,4,6,4,5,6$となる。この数列の第5項から第9項までを数列$B$とすれば、数列$B$の和は25となり、これが最大である。
Sample Input 2
3 3 5
0 10
8 1
11 1
Sample Output 2
58

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll (i)=0; (i)<(n); (i)++)

ll sum_all(ll a, ll d, ll l){
    return a * l + l * (l-1) / 2 * d;
}
ll sum_left(ll a, ll d, ll l, ll n){
    return a * n + n * (n-1) / 2 * d;
}
ll N, L, K;
vector<ll> A, D;
vector<ll> S, cumS;
ll get_sum(ll n){
    ll q = n / L;
    ll m = n % L;
    return cumS[q] + sum_left(A[q], D[q], L, m);
}
ll get_s(ll n){
    ll q = n / L;
    ll m = n % L;
    return A[q] + (m-1) * D[q];
}

int main(){
    cin >> N >> L >> K;
    A.resize(N+1);
    D.resize(N+1);
    S.resize(N+1);
    cumS.resize(N+1);
    ll ans = 0;
    rep(i, N){
        ll a, d;
        cin >> a >> d;
        A[i] = a;
        D[i] = d;
        S[i] = sum_all(a, d, L);
        cumS[i+1] = S[i] + cumS[i];
    }

    // rep(i, N*L+1){
    //     cout << get_sum(i) << " ";
    // } cout << endl;
    
    vector<ll> Candidates;
    Candidates.reserve(2*N);
    rep(i, N){
        ll l, r, an;
        l = i * L;
        r = l + K;
        if (0 <= l && l < N*L && 0 < r && r <= N*L){
            Candidates.push_back(l);
            //an = get_sum(r) - get_sum(l);
            //if (ans < an) ans = an;
        }
        r = (i+1) * L;
        l = r - K;
        if (0 <= l && l < N*L && 0 < r && r <= N*L){
            Candidates.push_back(l);
            //an = get_sum(r) - get_sum(l);
            //if (ans < an) ans = an;
        }
    }
    sort(Candidates.begin(), Candidates.end());

    rep(i, Candidates.size()-1){
        ll cand1_l = Candidates[i];
        ll cand1_r = cand1_l + K;
        ll cand2_l = Candidates[i+1];
        ll cand2_r = cand2_l + K;
        
        ll diff1 = get_s(cand1_r) - get_s(cand1_l);
        ll an = get_sum(cand1_r) - get_sum(cand1_l);
        if (ans < an) ans = an;
        ll diff2 = get_s(cand2_r) - get_s(cand2_l);
        an = get_sum(cand2_r) - get_sum(cand2_l);
        if (ans < an) ans = an;

        ll c1=cand1_l, c2=cand2_l;
        if (c1==c2) continue;
        while(c1+1<c2){
            ll cl = c1+c2>>1;
            ll cr = cl + K;
            ll diff = get_s(cr) - get_s(cl);
            if ((diff > 0) == (diff1 > 0)){
                c1 = cl;
            } else {
                c2 = cl;
            }
        }
        ll cl = c1;
        ll cr = cl + K;
        if (cl-1 >= 0) {
            an = get_sum(cr-1) - get_sum(cl-1);
            if (ans < an) ans = an;
        }
        if (ans < an) ans = an;
        an = get_sum(cr) - get_sum(cl);
        if (ans < an) ans = an;
        an = get_sum(cr+1) - get_sum(cl+1);
        //cout << cl << " " << an << " " << endl;
        if (ans < an) ans = an;
        if (cr+2 <= N*L) {
            an = get_sum(cr+2) - get_sum(cl+2);
            if (ans < an) ans = an;
        }
    }


    cout << ans << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 7200
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll (i)=0; (i)<(n); (i)++)

ll sum_all(ll a, ll d, ll l){
    return a * l + l * (l-1) / 2 * d;
}
ll sum_left(ll a, ll d, ll l, ll n){
    return a * n + n * (n-1) / 2 * d;
}
ll N, L, K;
vector<ll> A, D;
vector<ll> S, cumS;
vector<ll> Left1, Right1;
ll get_sum(ll n){
    ll q = n / L;
    ll m = n % L;
    return cumS[q] + sum_left(A[q], D[q], L, m);
}

int main(){
    cin >> N >> L >> K;
    A.resize(N+1);
    D.resize(N+1);
    S.resize(N);
    cumS.resize(N+1);
    ll ans = 0;
    rep(i, N){
        ll a, d;
        cin >> a >> d;
        A[i] = a;
        D[i] = d;
        S[i] = sum_all(a, d, L);
        cumS[i+1] = S[i] + cumS[i];
    }
    rep(i, N){
        ll l, r, an;
        l = i * L;
        r = l + K;
        if (0 <= l && l < N*L && 0 < r && r <= N*L){
            an = get_sum(r) - get_sum(l);
            if (ans < an) ans = an;
        }
        l = i * L - 1;
        r = l + K;
        if (0 <= l && l < N*L && 0 < r && r <= N*L){
            an = get_sum(r) - get_sum(l);
            if (ans < an) ans = an;
        }
        r = (i+1) * L;
        l = r - K;
        if (0 <= l && l < N*L && 0 < r && r <= N*L){
            an = get_sum(r) - get_sum(l);
            if (ans < an) ans = an;
        }
        r = (i+1) * L + 1;
        l = r - K;
        if (0 <= l && l < N*L && 0 < r && r <= N*L){
            an = get_sum(r) - get_sum(l);
            if (ans < an) ans = an;
        }
    }
    cout << ans << endl;
}
accuracy: 0.2564102564102564
time_ms: 20
memory_kb: 4836
final_rank: 17

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 100005

ll N,L,K;
ll A[SIZE],D[SIZE];
ll SUM[SIZE];


ll calc(ll head_index,ll head_loc,ll tail_index){

	ll n_1 = (L-head_loc+1);
	ll first_1 = A[head_index]+(head_loc-1)*D[head_index];
	ll sum_head = (n_1*(2*first_1+(n_1-1)*D[head_index]))/2;

	ll n_2 = (K-(n_1))%L;
	if(n_2 == 0){
		n_2 = L;
	}
	ll first_2 = A[tail_index];
	ll sum_tail = (n_2*(2*first_2+(n_2-1)*D[tail_index]))/2;

	return sum_head+sum_tail;
}


int main(){

	scanf("%lld %lld %lld",&N,&L,&K);

	SUM[0] = 0;

	for(int i = 1; i <= N; i++){

		scanf("%lld %lld",&A[i],&D[i]);

		SUM[i] = (L*(2*A[i]+(L-1)*D[i]))/2;
		SUM[i] += SUM[i-1];
	}

	ll add_index;

	if(K <= L){

		add_index = 0;

	}else{ //K > L

		if((K-L)%L == 0){

			add_index = (K-L)/L;

		}else{

			add_index = (K-L)/L+1;
		}
	}

	//末尾の数列のindexが切り替わる位置を求める
	ll MID;
	ll left = 1,right = L,mid = (left+right)/2;
	ll tmp,tmp_add;

	while(left <= right){

		tmp = L-mid+1;

		if((K-tmp)%L == 0){

			tmp_add = (K-tmp)/L;

		}else{

			tmp_add = (K-tmp)/L+1;
		}

		if(tmp_add == add_index){

			MID = mid;
			left = mid+1;

		}else{

			right = mid-1;
		}
		mid = (left+right)/2;
	}

	ll ans = 0;
	ll tail_index,base_sum;
	ll calc_L,calc_R,mid1,mid2;

	for(ll i = 1; i <= N; i++){

		tail_index = min(i+add_index,N);
		if(tail_index >= i+2){

			base_sum = SUM[tail_index-1]-SUM[i];

		}else{

			base_sum = 0;
		}

		//1-MIDで検索
		if(tail_index == i){

			//最も右側の数列
			ll first = A[i];
			if(L >= K){

				first += (L-K)*D[i];
			}

			tmp = (min(L,K)*(2*(first)+(min(L,K)-1)*D[i]))/2;
			ans = max(ans,base_sum+tmp);

		}else{

			calc_L = 1, calc_R = MID;

			while(calc_L+2 < calc_R){

				mid1 = (2*calc_L+calc_R)/3;
				mid2 = (calc_L+2*calc_R)/3;

				if(calc(i,mid1,tail_index) > calc(i,mid2,tail_index)){

					calc_R = mid2;
				}else{

					calc_L = mid1;
				}
			}

			for(int k = calc_L; k <= calc_R; k++){

				ans = max(ans,base_sum+calc(i,k,tail_index));
			}
		}

		if(MID == L)continue;
		//MID+1-Lで検索

		calc_L = MID+1, calc_R = L;

		tail_index++;
		tail_index = min(tail_index,N);

		if(tail_index >= i+2){

			base_sum = SUM[tail_index-1]-SUM[i];

		}else{

			base_sum = 0;
		}

		while(calc_L+2 < calc_R){

			mid1 = (2*calc_L+calc_R)/3;
			mid2 = (calc_L+2*calc_R)/3;

			if(calc(i,mid1,tail_index) > calc(i,mid2,tail_index)){

				calc_R = mid2;
			}else{

				calc_L = mid1;
			}
		}

		for(int k = calc_L; k <= calc_R; k++){

			ans = max(ans,base_sum+calc(i,k,tail_index));
		}
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 5484
final_rank: 8

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 100005

ll N,L,K;
ll A[SIZE],D[SIZE];
ll SUM[SIZE];


ll calc(ll head_index,ll head_loc,ll tail_index){

	ll n_1 = (L-head_loc+1);
	ll first_1 = A[head_index]+(head_loc-1)*D[head_index];
	ll sum_head = (n_1*(2*first_1+(n_1-1)*D[head_index]))/2;

	/*printf("head_index:%lld head_loc:%lld tail_index:%lld\n",head_index,head_loc,tail_index);
	printf("sum_head:%lld\n",sum_head);*/

	ll n_2 = (K-(n_1))%L;
	if(n_2 == 0){
		n_2 = L;
	}
	ll first_2 = A[tail_index];
	ll sum_tail = (n_2*(2*first_2+(n_2-1)*D[tail_index]))/2;

	//printf("n_2:%lld sum_tail:%lld\n",n_2,sum_tail);

	return sum_head+sum_tail;
}


int main(){

	scanf("%lld %lld %lld",&N,&L,&K);

	SUM[0] = 0;

	for(int i = 1; i <= N; i++){

		scanf("%lld %lld",&A[i],&D[i]);

		SUM[i] = (L*(2*A[i]+(L-1)*D[i]))/2;
		SUM[i] += SUM[i-1];
	}

	ll add_index;

	if(K <= L){

		add_index = 0;

	}else{ //K > L

		if((K-L)%L == 0){

			add_index = (K-L)/L;

		}else{

			add_index = (K-L)/L+1;
		}
	}

	//末尾の数列のindexが切り替わる位置を求める
	ll MID;
	ll left = 1,right = L,mid = (left+right)/2;
	ll tmp,tmp_add;

	while(left <= right){

		tmp = L-mid+1;

		if((K-tmp)%L == 0){

			tmp_add = (K-tmp)/L;

		}else{

			tmp_add = (K-tmp)/L+1;
		}

		if(tmp_add == add_index){

			MID = mid;
			left = mid+1;

		}else{

			right = mid-1;
		}
		mid = (left+right)/2;
	}

	ll ans = 0;
	ll tail_index,base_sum;
	ll calc_L,calc_R,mid1,mid2;
	//printf("add_index_1:%lld MID:%lld\n",add_index,MID);

	for(ll i = 1; i <= N; i++){

		//printf("\ni:%lld\n",i);

		tail_index = min(i+add_index,N);
		if(tail_index >= i+2){

			base_sum = SUM[tail_index-1]-SUM[i];

		}else{

			base_sum = 0;
		}

		//printf("base_sum:%lld\n",base_sum);

		//1-MIDで検索
		if(tail_index == i){

			//最も右側の数列
			ll first = A[i];
			if(L >= K){

				first += (L-K)*D[i];
			}

			tmp = (min(L,K)*(2*(first)+(min(L,K)-1)*D[i]))/2;
		//	printf("tmp:%lld\n",tmp);
			ans = max(ans,base_sum+tmp);

		}else{

			calc_L = 1, calc_R = MID;

			while(calc_L+2 < calc_R){

				//printf("\nL:%lld R:%lld\n",calc_L,calc_R);

				mid1 = (2*calc_L+calc_R)/3;
				mid2 = (calc_L+2*calc_R)/3;

				if(calc(i,mid1,tail_index) > calc(i,mid2,tail_index)){

					calc_R = mid2;
				}else{

					calc_L = mid1;
				}

			}

			for(int k = calc_L; k <= calc_R; k++){

				ans = max(ans,base_sum+calc(i,k,tail_index));
			}
		}

		if(MID == L)continue;
		//MID+1-Lで検索


		calc_L = MID+1, calc_R = L;

		tail_index++;
		tail_index = min(tail_index,N);

		if(tail_index >= i+2){

			base_sum = SUM[tail_index-1]-SUM[i];

		}else{

			base_sum = 0;
		}

		/*printf("後半\n");
				printf("base_sum:%lld\n",base_sum);*/

		while(calc_L+2 < calc_R){

			//printf("\nL:%lld R:%lld\n",calc_L,calc_R);

			mid1 = (2*calc_L+calc_R)/3;
			mid2 = (calc_L+2*calc_R)/3;

			if(calc(i,mid1,tail_index) > calc(i,mid2,tail_index)){

				calc_R = mid2;
			}else{

				calc_L = mid1;
			}
		}

		for(int k = calc_L; k <= calc_R; k++){

			ans = max(ans,base_sum+calc(i,k,tail_index));
		}
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 5464
final_rank: 7

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 100005

ll N,L,K;
ll A[SIZE],D[SIZE];
ll SUM[SIZE];


ll calc(ll head_index,ll head_loc,ll tail_index){

	ll n_1 = (L-head_loc+1);
	ll first_1 = A[head_index]+(head_loc-1)*D[head_index];
	ll sum_head = (n_1*(2*first_1+(n_1-1)*D[head_index]))/2;

	/*printf("head_index:%lld head_loc:%lld tail_index:%lld\n",head_index,head_loc,tail_index);
	printf("sum_head:%lld\n",sum_head);*/

	ll n_2 = (K-(n_1))%L;
	if(n_2 == 0){
		n_2 = L;
	}
	ll first_2 = A[tail_index];
	ll sum_tail = (n_2*(2*first_2+(n_2-1)*D[tail_index]))/2;

	//printf("n_2:%lld sum_tail:%lld\n",n_2,sum_tail);

	return sum_head+sum_tail;
}


int main(){

	scanf("%lld %lld %lld",&N,&L,&K);

	SUM[0] = 0;

	for(int i = 1; i <= N; i++){

		scanf("%lld %lld",&A[i],&D[i]);

		SUM[i] = (L*(2*A[i]+(L-1)*D[i]))/2;
		SUM[i] += SUM[i-1];
	}

	ll add_index;

	if(K <= L){

		add_index = 0;

	}else{ //K > L

		if((K-L)%L == 0){

			add_index = (K-L)/L;

		}else{

			add_index = (K-L)/L+1;
		}
	}

	//末尾の数列のindexが切り替わる位置を求める
	ll MID;
	ll left = 1,right = L,mid = (left+right)/2;
	ll tmp,tmp_add;

	while(left <= right){

		tmp = L-mid+1;

		if((K-tmp)%L == 0){

			tmp_add = (K-tmp)/L;

		}else{

			tmp_add = (K-tmp)/L+1;
		}

		if(tmp_add == add_index){

			MID = mid;
			left = mid+1;

		}else{

			right = mid-1;
		}
		mid = (left+right)/2;
	}

	ll ans = 0;
	ll tail_index,base_sum;
	ll calc_L,calc_R,mid1,mid2;
	//printf("add_index_1:%lld MID:%lld\n",add_index,MID);

	for(ll i = 1; i <= N; i++){

	//	printf("\ni:%lld\n",i);

		tail_index = min(i+add_index,N);
		if(tail_index >= i+2){

			base_sum = SUM[tail_index-1]-SUM[i];

		}else{

			base_sum = 0;
		}

		//printf("base_sum:%lld\n",base_sum);

		//1-MIDで検索
		if(tail_index == i){

			//最も右側の数列
			tmp = (K*(2*(A[i]+(L-K)*D[i])+(K-1)*D[i]))/2;
		//	printf("tmp:%lld\n",tmp);
			ans = max(ans,base_sum+tmp);

		}else{

			calc_L = 1, calc_R = MID;

			while(calc_L+2 < calc_R){

				//printf("\nL:%lld R:%lld\n",calc_L,calc_R);

				mid1 = (2*calc_L+calc_R)/3;
				mid2 = (calc_L+2*calc_R)/3;

				if(calc(i,mid1,tail_index) > calc(i,mid2,tail_index)){

					calc_R = mid2;
				}else{

					calc_L = mid1;
				}

			}

			for(int k = calc_L; k <= calc_R; k++){

				ans = max(ans,base_sum+calc(i,k,tail_index));
			}
		}

		if(MID == L)continue;
		//MID+1-Lで検索


		calc_L = MID+1, calc_R = L;

		tail_index++;
		tail_index = min(tail_index,N);

		if(tail_index >= i+2){

			base_sum = SUM[tail_index-1]-SUM[i];

		}else{

			base_sum = 0;
		}

		/*printf("後半\n");
				printf("base_sum:%lld\n",base_sum);*/

		while(calc_L+2 < calc_R){

			//printf("\nL:%lld R:%lld\n",calc_L,calc_R);

			mid1 = (2*calc_L+calc_R)/3;
			mid2 = (calc_L+2*calc_R)/3;

			if(calc(i,mid1,tail_index) > calc(i,mid2,tail_index)){

				calc_R = mid2;
			}else{

				calc_L = mid1;
			}
		}

		for(int k = calc_L; k <= calc_R; k++){

			ans = max(ans,base_sum+calc(i,k,tail_index));
		}
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 0.23076923076923078
time_ms: 60
memory_kb: 4632
final_rank: 20

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 100005
#define NUM 20

ll N,L,K;
ll A[SIZE],D[SIZE];
ll SUM[SIZE];


ll calc(ll head_index,ll head_loc,ll tail_index){

	ll n_1 = (L-head_loc+1);
	ll first_1 = A[head_index]+(head_loc-1)*D[head_index];
	ll sum_head = (n_1*(2*first_1+(n_1-1)*D[head_index]))/2;

	/*printf("head_index:%lld head_loc:%lld tail_index:%lld\n",head_index,head_loc,tail_index);
	printf("sum_head:%lld\n",sum_head);*/

	ll n_2 = (K-(n_1))%L;
	if(n_2 == 0){
		n_2 = L;
	}
	ll first_2 = A[tail_index];
	ll sum_tail = (n_2*(2*first_2+(n_2-1)*D[tail_index]))/2;

	//printf("n_2:%lld sum_tail:%lld\n",n_2,sum_tail);

	return sum_head+sum_tail;
}


int main(){

	scanf("%lld %lld %lld",&N,&L,&K);

	SUM[0] = 0;

	for(int i = 1; i <= N; i++){

		scanf("%lld %lld",&A[i],&D[i]);

		SUM[i] = (L*(2*A[i]+(L-1)*D[i]))/2;
		SUM[i] += SUM[i-1];
	}

	ll add_index_1,add_index_2;
	if(K <= L){

		add_index_1 = 0;

	}else{ //K > L

		if((K-L)%L == 0){

			add_index_1 = (K-L)/L;

		}else{

			add_index_1 = (K-L)/L+1;
		}
	}
	add_index_2 = add_index_1+1;


	//末尾の数列のindexが切り替わる位置を求める
	ll MID;
	ll left = 1,right = N,mid = (left+right)/2;
	ll tmp,tmp_add;

	while(left <= right){

		tmp = L-mid+1;

		if((K-tmp)%L == 0){

			tmp_add = (K-tmp)/L;

		}else{

			tmp_add = (K-tmp)/L+1;
		}

		if(tmp_add == add_index_1){

			MID = mid;
			left = mid+1;

		}else{

			right = mid-1;
		}
		mid = (left+right)/2;
	}

	ll ans = 0;
	ll tail_index,base_sum;
	ll calc_L,calc_R,mid1,mid2;
	ll pre_L,pre_R;

	//printf("add_index_1:%lld MID:%lld\n",add_index_1,MID);

	for(int i = 1; i+add_index_1 <= N; i++){

		//printf("\ni:%d\n",i);

		tail_index = i+add_index_1;
		if(tail_index >= i+2){

			base_sum = SUM[tail_index-1]-SUM[i];

		}else{

			base_sum = 0;
		}

		//printf("base_sum:%lld\n",base_sum);

		//1-MIDで検索
		if(tail_index == i){

			//最も右側の数列
			tmp = (K*(2*(A[i]+(L-K)*D[i])+(K-1)*D[i]))/2;
			//printf("tmp:%lld\n",tmp);
			ans = max(ans,base_sum+tmp);

		}else{

			calc_L = 1, calc_R = MID;

			ans = max(ans,base_sum+max(calc(i,calc_L,tail_index),calc(i,calc_R,tail_index))); //両端

			do{

				pre_L = calc_L;
				pre_R = calc_R;

				//printf("\nL:%lld R:%lld\n",calc_L,calc_R);

				mid1 = (2*calc_L+calc_R)/3;
				mid2 = (calc_L+2*calc_R)/3;

				ll calc_1 = calc(i,mid1,tail_index);
				ll calc_2 = calc(i,mid2,tail_index);

				//printf("mid_1:%lld mid_2:%lld calc_1:%lld calc_2:%lld\n",mid1,mid2,calc_1,calc_2);

				ans = max(ans,base_sum+max(calc_1,calc_2));

				if(calc_1 >= calc_2){

					calc_R = mid2;
				}else{

					calc_L = mid1;
				}

				if(calc_R == pre_R && calc_L == pre_L){

					break;
				}

			}while(calc_L < calc_R);
		}

		if(MID == L)continue;
		if(i+add_index_2 > N)break;
		//MID+1-Lで検索


		calc_L = MID+1, calc_R = L;

		tail_index++;

		if(tail_index >= i+2){

					base_sum = SUM[tail_index-1]-SUM[i];

				}else{

					base_sum = 0;
				}

		/*printf("後半\n");
				printf("base_sum:%lld\n",base_sum);*/

		ans = max(ans,base_sum+max(calc(i,calc_L,tail_index),calc(i,calc_R,tail_index))); //両端

		do{

			pre_L = calc_L;
			pre_R = calc_R;

			//printf("\nL:%lld R:%lld\n",calc_L,calc_R);

			mid1 = (2*calc_L+calc_R)/3;
			mid2 = (calc_L+2*calc_R)/3;

			ll calc_1 = calc(i,mid1,tail_index);
			ll calc_2 = calc(i,mid2,tail_index);

			//printf("mid_1:%lld mid_2:%lld calc_1:%lld calc_2:%lld\n",mid1,mid2,calc_1,calc_2);

			ans = max(ans,base_sum+max(calc_1,calc_2));

			if(calc_1 >= calc_2){

				calc_R = mid2;
			}else{

				calc_L = mid1;
			}

			if(calc_R == pre_R && calc_L == pre_L){

				break;
			}

		}while(calc_L < calc_R);
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 0.2564102564102564
time_ms: 50
memory_kb: 4584
final_rank: 18

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef double ld;
typedef complex<ld> Point;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
typedef vector<int> vec;
typedef vector<string> svec;


void solve() {
	ll n, l; ll k; cin >> n >> l >> k;
	ll d = k / l;
	vector<ll> a(n),b(n);
	vector<ll> s(n);
	rep(i, n) {
		cin >> a[i] >> b[i];
		s[i] = l * a[i] + l * (l - 1) / 2 * b[i];
	}
	ll sum = 0;
	rep(i, d)sum += s[i];
	{
		ll cur = a[d];
		rep(i, (k%l)) {
			sum += cur;
			cur += b[d];
		}
	}
	ll ans = sum;
	int le = 0, ri = d;
	int cl = 0, cr = k%l;
	while (ri < n) {
		//cout << le << " " << cl << " " << ri << " " << cr << " " << sum << endl;
		ll ad = a[ri] + b[ri] * cr - (a[le] + b[le] * cl);
		ll rad = b[ri] - b[le];
		ll len = min(l - cl, l - cr);
		if (rad < 0) {
			ll u = ad / (-rad);
			u++;
			if (u < len&&u>=0) {
				ll csum = sum + ad * u + u * (u - 1) / 2 * rad;
				ans = max(ans, csum);
			}
		}
		sum += ad * len + len * (len - 1) / 2 * rad;
		ans = max(ans, sum);
		cl += len; cr += len;
		if (cl == l) {
			le++; cl = 0;
		}
		if (cr == l) {
			ri++; cr = 0;
		}
	}
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(5);
	//while (cin >> n>>h>>m>>s,n)solve();
	solve();
	//stop
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5064
final_rank: 1

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;
using ld = long double;

vector<pair<long long int, long long int>>ps;
long long int g(long long int x, long long int place) {
	return ps[x].first + ps[x].second*(place);
}

int main() {
	long long int N, L, K; cin >> N >> L >> K;
	vector<long long int>sums(N + 1);
	for (int i = 0; i < N; ++i) {
		int a, d; cin >> a >> d;
		ps.emplace_back(a, d);

		sums[i + 1] = sums[i] + (a + (a + d*(L - 1)))*L / 2;
	}

	long long int answer = 0;
	if (N == 1) {
		answer = (g(0, L - K) + g(0, L - 1))*K / 2;

	}
	else if (N*L == K) {
		answer = sums[N];
	}
	else {

		const long long int t = K / L;

		for (long long int len = t - 1; len <= t; ++len) {
			for (int l = 1; l + len < N; ++l) {
				int r = l + len;

				long long int rest = K - L*len;

				if (rest <= L) {

					long long int now_ans = sums[r] - sums[l] +
						(g(l - 1, L - rest) + g(l - 1, L - 1))*
						rest / 2;

					long long int r_minus_l_start = g(r, 0) - g(l - 1, L - rest);
					long long int r_minus_l_sa = ps[r].second - ps[l - 1].second;

					long long int max_cnt;
					if (r_minus_l_sa > 0) {
						max_cnt = rest;
					}
					else if (r_minus_l_sa == 0) {
						if (r_minus_l_start > 0) {
							max_cnt = rest;
						}
						else {
							max_cnt = 0;
						}
					}
					else {
						if (r_minus_l_start > 0) {
							long long int k = r_minus_l_start / (-r_minus_l_sa) + 1;
							max_cnt = min(rest, k);
						}
						else {
							max_cnt = 0;
						}
					}

					long long int max_ans = now_ans + r_minus_l_start*max_cnt +
						r_minus_l_sa*(max_cnt - 1)*max_cnt / 2;

					answer = max(answer, now_ans);
					answer = max(answer, max_ans);
					answer = max(answer,
						now_ans + r_minus_l_start*rest +
						r_minus_l_sa*(rest - 1)*rest / 2);

				}
				else {

					long long int now_ans = sums[r] - sums[l - 1]
						+ (ps[r].first + (ps[r].first + ps[r].second*(rest - L - 1)))*(rest - L) / 2;

					long long int r_minus_l_start = g(r, rest - L) - g(l - 1, 0);
					long long int r_minus_l_sa = ps[r].second - ps[l - 1].second;

					long long int max_cnt;
					if (r_minus_l_sa > 0) {
						max_cnt = 2 * L - rest;
					}
					else if (r_minus_l_sa == 0) {
						if (r_minus_l_start > 0) {
							max_cnt = 2 * L - rest;
						}
						else {
							max_cnt = 0;
						}
					}
					else {
						if (r_minus_l_start > 0) {
							long long int k = r_minus_l_start / (-r_minus_l_sa) + 1;
							max_cnt = min(2 * L - rest, k);
						}
						else {
							max_cnt = 0;
						}
					} 
					long long int max_ans = now_ans + r_minus_l_start*max_cnt +
						(r_minus_l_sa*(max_cnt - 1)*max_cnt) / 2ll;

					long long int kk=(g(l-1,max_cnt)+g(l-1,L-1))*(L-max_cnt)/2+
						sums[r] - sums[l]+
						(g(r,0)+g(r,rest-L-1+max_cnt))*(rest-L+max_cnt)/2;
					
					answer = max(answer, now_ans);
					answer = max(answer, max_ans);
					answer = max(answer,
						now_ans + r_minus_l_start*(2 * L - rest) +
						r_minus_l_sa*((2 * L - rest) - 1)*(2 * L - rest) / 2);

				}
			}
		}
	}


	cout << answer << endl;
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5712
final_rank: 4

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;
using ld = long double;

vector<pair<long long int, long long int>>ps;
long long int g(long long int x, long long int place) {
	return ps[x].first + ps[x].second*(place);
}

int main() {
	long long int N, L, K; cin >> N >> L >> K;
	vector<long long int>sums(N + 1);
	for (int i = 0; i < N; ++i) {
		int a, d; cin >> a >> d;
		ps.emplace_back(a, d);

		sums[i + 1] = sums[i] + (a + (a + d*(L - 1)))*L / 2;
	}

	long long int answer = 0;
	if (N == 1) {
		answer = (g(0, L - K) + g(0, L - 1))*K / 2;

	}
	else if (N*L == K) {
		answer = sums[N];
	}
	else {

		const long long int t = K / L;

		for (long long int len = t - 1; len <= t; ++len) {
			for (int l = 1; l + len < N; ++l) {
				int r = l + len;

				long long int rest = K - L*len;

				if (rest <= L) {

					long long int now_ans = sums[r] - sums[l] +
						(g(l - 1, L - rest) + g(l - 1, L - 1))*
						rest / 2;

					long long int r_minus_l_start = g(r, 0) - g(l - 1, L - rest);
					long long int r_minus_l_sa = ps[r].second - ps[l - 1].second;

					long long int max_cnt;
					if (r_minus_l_sa > 0) {
						max_cnt = rest;
					}
					else if (r_minus_l_sa == 0) {
						if (r_minus_l_start > 0) {
							max_cnt = rest;
						}
						else {
							max_cnt = 0;
						}
					}
					else {
						if (r_minus_l_start > 0) {
							long long int k = r_minus_l_start / (-r_minus_l_sa) + 1;
							max_cnt = min(rest, k);
						}
						else {
							max_cnt = 0;
						}
					}

					long long int max_ans = now_ans + r_minus_l_start*max_cnt +
						r_minus_l_sa*(max_cnt - 1)*max_cnt / 2;

					answer = max(answer, now_ans);
					answer = max(answer, max_ans);
					answer = max(answer,
						now_ans + r_minus_l_start*rest +
						r_minus_l_sa*(rest - 1)*rest / 2);

				}
				else {

					long long int now_ans = sums[r] - sums[l - 1]
						+ (ps[r].first + (ps[r].first + ps[r].second*(rest - L - 1)))*(rest - L) / 2;

					long long int r_minus_l_start = g(r, rest - L) - g(l - 1, 0);
					long long int r_minus_l_sa = ps[r].second - ps[l - 1].second;

					long long int max_cnt;
					if (r_minus_l_sa > 0) {
						max_cnt = 2 * L - rest;
					}
					else if (r_minus_l_sa == 0) {
						if (r_minus_l_start > 0) {
							max_cnt = 2 * L - rest;
						}
						else {
							max_cnt = 0;
						}
					}
					else {
						if (r_minus_l_start > 0) {
							long long int k = r_minus_l_start / (-r_minus_l_sa) + 1;
							max_cnt = min(2 * L - rest, k);
						}
						else {
							max_cnt = 0;
						}
					} 
					long long int a=now_ans;
					long long int b=r_minus_l_start*max_cnt;
					long long int c=r_minus_l_sa*(max_cnt-1ll)*max_cnt/2ll;
					long long int abc=a+b+c;
					long long int max_ans = now_ans + r_minus_l_start*max_cnt +
						(r_minus_l_sa*(max_cnt - 1)*max_cnt) / 2ll;

					long long int kk=(g(l-1,max_cnt)+g(l-1,L-1))*(L-max_cnt)/2+
						sums[r] - sums[l]+
						(g(r,0)+g(r,rest-L-1+max_cnt))*(rest-L+max_cnt)/2;
					{
						long long int fx=0;
					swap(fx,max_cnt);
					long long int akk = (g(l - 1, max_cnt) + g(l - 1, L - 1))*(L - max_cnt) / 2 +
						sums[r] - sums[l] +
						(g(r, 0) + g(r, rest - L - 1 + max_cnt))*(rest - L + max_cnt) / 2;
					swap(fx,max_cnt);
					}
					max_cnt++;
					auto max2 = now_ans + r_minus_l_start*max_cnt +
						r_minus_l_sa*(max_cnt - 1)*max_cnt / 2;
					max_cnt -= 2; 
					auto max3 = now_ans + r_minus_l_start*max_cnt +
						r_minus_l_sa*(max_cnt - 1)*max_cnt / 2;
					max_cnt++;
					answer = max(answer, now_ans);
					answer = max(answer, max_ans);
					//answer=max(answer,max2);
					//answer=max(answer,max3);
					answer = max(answer,
						now_ans + r_minus_l_start*(2 * L - rest) +
						r_minus_l_sa*((2 * L - rest) - 1)*(2 * L - rest) / 2);

					if (answer == 2707366438153060
						) {
						//cout<<answer<<endl;
					}
				}
			}
		}
	}


	cout << answer << endl;
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5656
final_rank: 3

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;
using ld =long double;

vector<pair<long long int, long long int>>ps;
long long int g(int x, int place) {
	return ps[x].first+ps[x].second*(place);
}

int main() {
	long long int N,L,K;cin>>N>>L>>K;
	vector<long long int>sums(N+1);
	for (int i = 0; i < N; ++i) {
		int a,d;cin>>a>>d;
		ps.emplace_back(a,d);

		sums[i+1]=sums[i]+(a+(a+d*(L-1)))*L/2;
	}

	long long int answer = 0;
	if (N == 1) {
		answer=(g(0,L-K)+g(0,L-1))*K/2;

	}
	else if (N*L == K) {
		answer=sums[N];
	}
	else {

		const long long int t = K / L;

		for (int len = t - 1; len <= t; ++len) {
			for (int l = 1; l + len < N; ++l) {
				int r = l + len;

				long long int rest = K - L*len;

				if (rest <= L) {

					long long int now_ans = sums[r] - sums[l] +
						(g(l - 1, L - rest) + g(l - 1, L - 1))*
						rest / 2;

					long long int r_minus_l_start = g(r, 0) - g(l - 1, L - rest);
					long long int r_minus_l_sa = ps[r].second - ps[l - 1].second;

					long long int max_cnt;
					if (r_minus_l_sa > 0) {
						max_cnt = rest;
					}
					else if (r_minus_l_sa == 0) {
						if (r_minus_l_start > 0) {
							max_cnt = rest;
						}
						else {
							max_cnt = 0;
						}
					}
					else {
						if (r_minus_l_start > 0) {
							long long int k = r_minus_l_start / (-r_minus_l_sa) + 1;
							max_cnt = min(rest, k);
						}
						else {
							max_cnt = 0;
						}
					}

					long long int max_ans = now_ans + r_minus_l_start*max_cnt +
						r_minus_l_sa*(max_cnt - 1)*max_cnt / 2;

					answer=max(answer,now_ans);
					answer = max(answer, max_ans);
					answer = max(answer, 
						now_ans+r_minus_l_start*rest+
					r_minus_l_sa*(rest-1)*rest/2);

				}
				else {

					long long int now_ans = sums[r] - sums[l - 1]
						+ (ps[r].first + (ps[r].first + ps[r].second*(rest - L - 1))*(rest - L) / 2);

					long long int r_minus_l_start = g(r, rest - L) - g(l - 1, 0);
					long long int r_minus_l_sa = ps[r].second - ps[l - 1].second;

					long long int max_cnt;
					if (r_minus_l_sa > 0) {
						max_cnt = 2 * L - rest;
					}
					else if (r_minus_l_sa == 0) {
						if (r_minus_l_start > 0) {
							max_cnt = 2 * L - rest;
						}
						else {
							max_cnt = 0;
						}
					}
					else {
						if (r_minus_l_start > 0) {
							long long int k = r_minus_l_start / (-r_minus_l_sa) + 1;
							max_cnt = min(2 * L - rest, k);
						}
						else {
							max_cnt = 0;
						}
					}

					long long int max_ans = now_ans + r_minus_l_start*max_cnt +
						r_minus_l_sa*(max_cnt - 1)*max_cnt / 2;
					max_cnt++;
					auto max2= now_ans + r_minus_l_start*max_cnt +
						r_minus_l_sa*(max_cnt - 1)*max_cnt / 2;
					max_cnt-=2;
					auto max3=now_ans + r_minus_l_start*max_cnt +
						r_minus_l_sa*(max_cnt - 1)*max_cnt / 2;
					max_cnt++;
					answer = max(answer, now_ans);
					answer = max(answer, max_ans);
					//answer=max(answer,max2);
					//answer=max(answer,max3);
					answer = max(answer,
						now_ans + r_minus_l_start*(2*L-rest) +
						r_minus_l_sa*((2 * L - rest) - 1)*(2 * L - rest) / 2);

					if (answer == 2707366438153060
						) {
						//cout<<answer<<endl;
					}
				}
			}
		}
	}

	
	cout<<answer<<endl;
	return 0;
}
accuracy: 0.2564102564102564
time_ms: 20
memory_kb: 4344
final_rank: 16

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;
using ld =long double;

vector<pair<long long int, long long int>>ps;
long long int g(int x, int place) {
	return ps[x].first+ps[x].second*(place);
}

int main() {
	long long int N,L,K;cin>>N>>L>>K;
	vector<long long int>sums(N+1);
	for (int i = 0; i < N; ++i) {
		int a,d;cin>>a>>d;
		ps.emplace_back(a,d);

		sums[i+1]=sums[i]+(a+(a+d*(L-1)))*L/2;
	}

	long long int answer = 0;
	if (N == 1) {
		answer=(g(0,L-K)+g(0,L-1))*K/2;

	}
	else if (N*L == K) {
		answer=sums[N];
	}
	else {

		const long long int t = K / L;

		for (int len = t - 1; len <= t; ++len) {
			for (int l = 1; l + len < N; ++l) {
				int r = l + len;

				long long int rest = K - L*len;

				if (rest <= L) {

					long long int now_ans = sums[r] - sums[l] +
						(g(l - 1, L - rest) + g(l - 1, L - 1))*
						rest / 2;

					long long int r_minus_l_start = g(r, 0) - g(l - 1, L - rest);
					long long int r_minus_l_sa = ps[r].second - ps[l - 1].second;

					long long int max_cnt;
					if (r_minus_l_sa > 0) {
						max_cnt = rest;
					}
					else if (r_minus_l_sa == 0) {
						if (r_minus_l_start > 0) {
							max_cnt = rest;
						}
						else {
							max_cnt = 0;
						}
					}
					else {
						if (r_minus_l_start > 0) {
							long long int k = r_minus_l_start / (-r_minus_l_sa) + 1;
							max_cnt = min(rest, k);
						}
						else {
							max_cnt = 0;
						}
					}

					long long int max_ans = now_ans + r_minus_l_start*max_cnt +
						r_minus_l_sa*(max_cnt - 1)*max_cnt / 2;

					answer=max(answer,now_ans);
					answer = max(answer, max_ans);
					answer = max(answer, 
						now_ans+r_minus_l_start*rest+
					r_minus_l_sa*(rest-1)*rest/2);

				}
				else {

					long long int now_ans = sums[r] - sums[l - 1]
						+ (ps[r].first + (ps[r].first + ps[r].second*(rest - L - 1))*(rest - L) / 2);

					long long int r_minus_l_start = g(r, rest - L) - g(l - 1, 0);
					long long int r_minus_l_sa = ps[r].second - ps[l - 1].second;

					long long int max_cnt;
					if (r_minus_l_sa > 0) {
						max_cnt = 2 * L - rest;
					}
					else if (r_minus_l_sa == 0) {
						if (r_minus_l_start > 0) {
							max_cnt = 2 * L - rest;
						}
						else {
							max_cnt = 0;
						}
					}
					else {
						if (r_minus_l_start > 0) {
							long long int k = r_minus_l_start / (-r_minus_l_sa) + 1;
							max_cnt = min(2 * L - rest, k);
						}
						else {
							max_cnt = 0;
						}
					}

					long long int max_ans = now_ans + r_minus_l_start*max_cnt +
						r_minus_l_sa*(max_cnt - 1)*max_cnt / 2;

					answer = max(answer, now_ans);
					answer = max(answer, max_ans);
					answer = max(answer,
						now_ans + r_minus_l_start*(2*L-rest) +
						r_minus_l_sa*((2 * L - rest) - 1)*(2 * L - rest) / 2);
				}
			}
		}
	}

	
	cout<<answer<<endl;
	return 0;
}
accuracy: 0.2564102564102564
time_ms: 20
memory_kb: 4280
final_rank: 14

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;
using ld =long double;

vector<pair<long long int, long long int>>ps;
long long int g(int x, int place) {
	return ps[x].first+ps[x].second*(place);
}

int main() {
	int N,L,K;cin>>N>>L>>K;
	vector<long long int>sums(N+1);
	for (int i = 0; i < N; ++i) {
		int a,d;cin>>a>>d;
		ps.emplace_back(a,d);

		sums[i+1]=sums[i]+(a+(a+d*(L-1)))*L/2;
	}

	long long int answer = 0;
	if (N == 1) {
		answer=(g(0,L-K)+g(0,L-1))*K/2;

	}
	else if (N*L == K) {
		answer=sums[N];
	}
	else {

		const int t = K / L;

		for (int len = t - 1; len <= t; ++len) {
			for (int l = 1; l + len < N; ++l) {
				int r = l + len;

				int rest = K - L*len;

				if (rest <= L) {

					long long int now_ans = sums[r] - sums[l] +
						(g(l - 1, L - rest) + g(l - 1, L - 1))*
						rest / 2;

					long long int r_minus_l_start = g(r, 0) - g(l - 1, L - rest);
					long long int r_minus_l_sa = ps[r].second - ps[l - 1].second;

					int max_cnt;
					if (r_minus_l_sa > 0) {
						max_cnt = rest;
					}
					else if (r_minus_l_sa == 0) {
						if (r_minus_l_start > 0) {
							max_cnt = rest;
						}
						else {
							max_cnt = 0;
						}
					}
					else {
						if (r_minus_l_start > 0) {
							int k = r_minus_l_start / (-r_minus_l_sa) + 1;
							max_cnt = min(rest, k);
						}
						else {
							max_cnt = 0;
						}
					}

					long long int max_ans = now_ans + r_minus_l_start*max_cnt +
						r_minus_l_sa*(max_cnt - 1)*max_cnt / 2;

					answer=max(answer,now_ans);
					answer = max(answer, max_ans);
					answer = max(answer, 
						now_ans+r_minus_l_start*rest+
					r_minus_l_sa*(rest-1)*rest/2);

				}
				else {

					long long int now_ans = sums[r] - sums[l - 1]
						+ (ps[r].first + (ps[r].first + ps[r].second*(rest - L - 1))*(rest - L) / 2);

					long long int r_minus_l_start = g(r, rest - L) - g(l - 1, 0);
					long long int r_minus_l_sa = ps[r].second - ps[l - 1].second;

					int max_cnt;
					if (r_minus_l_sa > 0) {
						max_cnt = 2 * L - rest;
					}
					else if (r_minus_l_sa == 0) {
						if (r_minus_l_start > 0) {
							max_cnt = 2 * L - rest;
						}
						else {
							max_cnt = 0;
						}
					}
					else {
						if (r_minus_l_start > 0) {
							int k = r_minus_l_start / (-r_minus_l_sa) + 1;
							max_cnt = min(2 * L - rest, k);
						}
						else {
							max_cnt = 0;
						}
					}

					long long int max_ans = now_ans + r_minus_l_start*max_cnt +
						r_minus_l_sa*(max_cnt - 1)*max_cnt / 2;

					answer = max(answer, now_ans);
					answer = max(answer, max_ans);
					answer = max(answer,
						now_ans + r_minus_l_start*(2*L-rest) +
						r_minus_l_sa*((2 * L - rest) - 1)*(2 * L - rest) / 2);
				}
			}
		}
	}

	
	cout<<answer<<endl;
	return 0;
}
accuracy: 0.23076923076923078
time_ms: 20
memory_kb: 4316
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define MAX_N 100005
typedef long long ll;

ll calc(ll a,ll d,ll n){
  if(n==0)return 0;
  return ( a+a+d*(n-1) )*n/2;
}

ll N,X,L;
ll a[MAX_N],d[MAX_N];

ll solve2();

ll solve(){

  ll res=0, val=0;

  
  int j=0;
  for(int i=0;i<N;i++){
    while( j < i+L/X ){
      val+=(a[j]+a[j]+(X-1)*d[j])*X/2;
      j++;
      if(j>=N)break;
    }
    
    res=max(res,val);
    if(j>=N)break;
    
    ll aj =a[j]+(L%X)*d[j];
    double A = -0.5*d[i] + 0.5*d[j];
    double B = -(a[i]-0.5*d[i]) + (aj-0.5*d[j]);
    
    double left=0, right =X-L%X , ll , rr, lv,rv;
    
    for(int p=0;p<100;p++){
      ll=(left+left+right)/3.0;
      rr=(right+right+left)/3.0;
      lv=A*ll*ll+B*ll;
      rv=A*rr*rr+B*rr;
      if(lv < rv)left=ll;
      else right=rr;
    }


    res=max(res, val + calc(a[j],d[j],L%X) );
    res=max(res, val + calc(a[j],d[j],X) - calc(a[i],d[i],X-L%X) );
    
    long long mm = left;
    /*
    cout<<"i="<<i<<" j="<<j<<endl;
    cout<<"val="<<val<<endl;
    cout<<"mm="<<mm<<endl;
    cout<<val + calc(a[j],d[j],L%X)<<endl;
    cout<<endl;
    */
    
    for( long long  dm = mm-1; dm<=mm+1; dm++ ){
      if(dm<0 || dm>(X-L%X) )continue;
      res=max(res, val + calc(a[j],d[j],L%X+dm) - calc(a[i],d[i],dm) );
    }
  
    if(i>0 && L%X>0){
      
      long long fa = a[i-1]+(X-1)*d[i-1];
      long long fd = d[i-1]*-1;
      long long sa = a[j]+(L%X-1)*d[j];
      long long sd = d[j]*-1;

      A = ( fd*fd*0.5 ) - ( sd*sd*0.5 );
      B = ( fa-fd*0.5 ) - ( sa-sd*0.5 );
      
      left=0,right=L%X;
      for(int p=0;p<100;p++){
        ll=(left+left+right)/3.0;
        rr=(right+right+left)/3.0;
        lv=A*ll*ll+B*ll;
        rv=A*rr*rr+B*rr;
        if(lv < rv)left=ll;
        else right=rr;
      }

      mm = left;
      for( long long  dm = mm-1; dm<=mm+1; dm++ ){
        if(dm<0 || dm>(L%X) )continue;
        res=max(res, val + calc(a[j],d[j],L%X) + calc(fa,fd,dm) - calc(sa,sd,dm) );
      }
      
      
    }
    val-=(a[i]+a[i]+(X-1)*d[i])*X/2;
  }
  return res;
}

int main(){
  scanf("%lld %lld %lld",&N,&X,&L);
  for(int i=0;i<N;i++){
    scanf("%lld %lld",&a[i],&d[i]);
  }
  ll ans=solve();

  for(int i=0;i<N;i++){
    a[i]= a[i]+d[i]*(X-1);
    d[i]*=-1;
  }
  printf("%lld\n",ans);
  return 0;
}

ll solve2(){
  ll res=0;
  for(int i=0;i<N;i++){
    ll A = a[i] + (X-1)*d[i];
    ll B = a[i] + (X-L)*d[i];
    ll sum = (A+B)*L/2;
    res=max(res,sum);
  }
  return res;
}
accuracy: 1
time_ms: 260
memory_kb: 4756
final_rank: 10

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

using namespace std;

template< class T >
struct CumulativeSum
{
  vector< T > data;

  CumulativeSum(int sz) : data(sz, 0) {};

  void add(int k, T x)
  {
    data[k] += x;
  }

  void build()
  {
    for(int i = 1; i < data.size(); i++) {
      data[i] += data[i - 1];
    }
  }

  T query(int k)
  {
    if(k < 0) return (0);
    return (data[min(k, (int) data.size() - 1)]);
  }
};

typedef long long int64;

int main()
{
  int64 N, L, K, A[100000], D[100000];

  cin >> N >> L >> K;
  for(int i = 0; i < N; i++) {
    cin >> A[i] >> D[i];
  }

  auto getSum = [&](int64 a, int64 l, int64 d)
  {
    return ((2 * a + (l - 1) * d) * l / 2);
  };

  CumulativeSum< int64 > sum(N);
  for(int i = 0; i < N; i++) {
    sum.add(i, getSum(A[i], L, D[i]));
  }
  sum.build();

  int64 ret = numeric_limits< int64 >::min();

  auto check = [&](int64 st)
  {
    int64 ed = st + K - 1;
    int64 range = sum.query(ed / L - 1) - sum.query(st / L);
    int64 st_index = st / L, ed_index = ed / L;
    st %= L, ed %= L;
    range += getSum(A[st_index] + D[st_index] * st, L - st, D[st_index]);
    range += getSum(A[ed_index], ed + 1, D[ed_index]);
    return (range);
  };

  for(int64 i = 0; i < N; i++) {
    int64 low = i * L, high = min(N * L - K + 1, i * L + L);
    if(low >= high) break;
    while(high - low > 1) {
      int64 mid = (low + high) >> 1LL;
      if(check(mid) - check(mid - 1) > 0) low = mid;
      else high = mid;
    }
    ret = max(ret, check(low));
  }
  cout << ret << endl;
}
accuracy: 1
time_ms: 110
memory_kb: 5136
final_rank: 5

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

ll calc1(ll a,ll d,ll n) {return (a*2+(n-1)*d)*n/2;}
ll calc2(ll a,ll d,ll n) {return calc1(a,d,n+1)-calc1(a,d,n);}
ll n,m,t,a[111111],b[111111],c[111111],d[111111],e[111111];

ll calc(ll k,ll mid) {
  ll sum=calc1(calc2(a[k],b[k],mid),b[k],m-mid);
  if(c[k]+t+mid>n*m) return -1;
  ll l=k+e[mid];
  sum+=d[l-1]-d[k+1];
  sum+=calc1(a[l-1],b[l-1],t-m*(l-k-1)+mid);
  return sum;
}

int main() {
  cin >> n >> m >> t;
  for(int i=0; i<n; i++) cin >> a[i] >> b[i];
  c[0]=d[0]=0;
  for(int i=0; i<n; i++) {
    c[i+1]+=c[i]+m;
    d[i+1]+=d[i]+calc1(a[i],b[i],m);
  }
  for(int i=0; i<=m; i++) e[i]=lower_bound(c,c+n,t+i)-c;
  ll ans=0;
  for(int i=0; i<n; i++) {
    ll l=max(0LL,m-t),r=m;
    while(l+2<r) {
      ll m1=(l*2+r)/3,m2=(l+r*2)/3;
      ll d1=calc(i,m1),d2=calc(i,m2);
      if(d1<d2) l=m1;
      else r=m2;
    }
    for(int j=l; j<=r; j++) ans=max(ans,calc(i,j));
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 6892
final_rank: 9

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}


const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;

LL N, L, K;
PLL xs[100010];
LL acc[100010];
LL sum(LL a, LL d, LL n){
  return a * n + d * (n-1) * n / 2;
}
LL sum(LL l, LL r){
  int il = l / L;
  int ir = r / L;

  if(il == ir)
	return sum(xs[il].FF + l%L * xs[il].SS, xs[il].SS, r - l);

  LL res = acc[ir] - acc[il+1];
  res += sum(xs[il].FF + l%L * xs[il].SS, xs[il].SS, L - l%L);
  res += sum(xs[ir].FF, xs[ir].SS, r%L);
	
  return res;
}

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

  cin >> N >> L >> K;
  REP(i,N)
	cin >> xs[i].FF >> xs[i].SS;
  REP(i,N)
	acc[i+1] = acc[i] + sum(xs[i].FF, xs[i].SS, L);

  LL ans = -1;
  LL NL = N * L;
  REP(i,N+1){
	for(int d=-1;d<=1;++d){
	  LL l = i*L + d, r = l + K;
	  if(0 <= l && l <= NL && 0 <= r && r <= NL)
		maxi(ans, sum(l, r));
	  r = i*L + d;
	  l = r - K;
	  if(0 <= l && l <= NL && 0 <= r && r <= NL)
		maxi(ans, sum(l, r));
	}
  }
  cout << ans << endl;

  return 0;
}
accuracy: 0.2564102564102564
time_ms: 10
memory_kb: 4452
final_rank: 15

code_snippet: // need
#include <iostream>
#include <algorithm>

// data structure
#include <bitset>
//#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <array>
//#include <tuple>
//#include <unordered_map>
#include <unordered_set>
#include <complex>
//#include <deque>
#include<valarray>

// stream
//#include <istream>
//#include <sstream>
//#include <ostream>
//#include <fstream>

// etc
#include <cmath>
#include <cassert>
#include <functional>
#include <iomanip>
//#include <typeinfo>
#include <chrono>
#include <random>
#include <numeric>

// input
#define INIT std::ios::sync_with_stdio(false);std::cin.tie(0);
#define VAR(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; }
template<typename First, typename...Rest>void MACRO_VAR_Scan(First& first, Rest&...rest) { std::cin >> first; MACRO_VAR_Scan(rest...); }
#define VEC_ROW(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_VEC_ROW_Init(n, __VA_ARGS__); for(int i=0; i<n; ++i){MACRO_VEC_ROW_Scan(i, __VA_ARGS__);}
template<typename T> void MACRO_VEC_ROW_Init(int n, T& t) { t.resize(n); }
template<typename First, typename...Rest>void MACRO_VEC_ROW_Init(int n, First& first, Rest&...rest) { first.resize(n); MACRO_VEC_ROW_Init(n, rest...); }
template<typename T> void MACRO_VEC_ROW_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename...Rest>void MACRO_VEC_ROW_Scan(int p, First& first, Rest&...rest) { std::cin >> first[p]; MACRO_VEC_ROW_Scan(p, rest...); }
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define MAT(type, c, m, n) std::vector<std::vector<type>> c(m, std::vector<type>(n));for(auto& r:c)for(auto& i:r)std::cin>>i;

// output
#define OUT(d) std::cout<<d;
#define FOUT(n, d) std::cout<<std::fixed<<std::setprecision(n)<<d;
#define SOUT(n, c, d) std::cout<<std::setw(n)<<std::setfill(c)<<d;
#define SP std::cout<<" ";
#define TAB std::cout<<"\t";
#define BR std::cout<<"\n";
#define SPBR(i, n) std::cout<<(i + 1 == n ? '\n' : ' ');
#define ENDL std::cout<<std::endl;
#define FLUSH std::cout<<std::flush;
#define SHOW(d) {std::cerr << #d << "\t:" << d << "\n";}
#define SHOWVECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOWVECTOR2(v) {std::cerr << #v << "\t:\n";for(const auto& xxx : v){for(const auto& yyy : xxx){std::cerr << yyy << " ";}std::cerr << "\n";}}
#define SHOWQUEUE(a) {auto tmp(a);std::cerr << #a << "\t:";while(!tmp.empty()){std::cerr << tmp.front() << " ";tmp.pop();}std::cerr << "\n";}

// utility
#define ALL(a) (a).begin(),(a).end()
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RFOR(i, a, b) for(int i=(b)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define FORLL(i, a, b) for(ll i=ll(a);i<ll(b);++i)
#define RFORLL(i, a, b) for(ll i=ll(b)-1;i>=ll(a);--i)
#define REPLL(i, n) for(ll i=0;i<ll(n);++i)
#define RREPLL(i, n) for(ll i=ll(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
template<typename T> inline T CHMAX(T& a, const T b) { return a = (a < b) ? b : a; }
template<typename T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; }
#define EXCEPTION(msg) throw std::string("Exception : " msg " [ in ") + __func__ + " : " + std::to_string(__LINE__) + " lines ]"
#define TRY(cond, msg) try {if (cond) EXCEPTION(msg);}catch (std::string s) {std::cerr << s << std::endl;}
void CHECKTIME(std::function<void()> f) { auto start = std::chrono::system_clock::now(); f(); auto end = std::chrono::system_clock::now(); auto res = std::chrono::duration_cast<std::chrono::nanoseconds>((end - start)).count(); std::cerr << "[Time:" << res << "ns  (" << res / (1.0e9) << "s)]\n"; }

// test
template<class T> std::vector<std::vector<T>> VV(int n) {
	return std::vector<std::vector<T>>(n);
}
template<class T> std::vector<std::vector<T>> VV(int n, int m, T init = T()) {
	return std::vector<std::vector<T>>(n, std::vector<T>(m, init));
}
template<typename S, typename T>
std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) {
	os << "(" << p.first << ", " << p.second << ")"; return os;
}

// type/const

#define int ll
using ll = long long;
using ull = unsigned long long;
using PAIR = std::pair<int, int>;
using PAIRLL = std::pair<ll, ll>;
constexpr int INFINT = 1 << 30;                          // 1.07x10^ 9
constexpr int INFINT_LIM = (1LL << 31) - 1;              // 2.15x10^ 9
constexpr ll INFLL = 1LL << 60;                          // 1.15x10^18
constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62);  // 9.22x10^18
constexpr double EPS = 1e-7;
constexpr int MOD = 1000000007;
constexpr double PI = 3.141592653589793238462643383279;

//SegmentTree(?????°??¨)
template <class T>
class SegmentTree {
private:
	T init_val;
	std::vector<T> ary;
	std::function<T(T, T)> func;
public:
	// ?????¨???(??????????????§??????n??¨??????)
	// SegmentTree<int> st(n,  INFLL, [](int l, int r){return std::min(l, r);}); ????°????
	// SegmentTree<int> st(n, -INFLL, [](int l, int r){return std::max(l, r);}); ?????§???
	// SegmentTree<int> st(n,    0, [](int l, int r){return l+r;});            ????¨?
	SegmentTree(int n, T val, std::function<T(T, T)> f) : init_val(val), func(f) {
		ary.resize(static_cast<int>(std::pow(2, std::ceil(std::log(n) / std::log(2)) + 1) - 1), val);
	}
	void init(int n, T val, std::function<T(T, T)> f) {
		ary.resize(static_cast<int>(std::pow(2, std::ceil(1.0*std::log(n) / std::log(2)) + 1) - 1), val);
		init_val = val;
		func = f;
	}
	void update(int i, T val) {
		i += ary.size() / 2;
		ary[i] = val;
		while (i > 0) {
			i = (i - 1) / 2;
			ary[i] = func(ary[2 * i + 1], ary[2 * i + 2]);
		}
	}
	// ??????[a, b)???func????????????????????? (query_(a, b, 0, 0, n)??¨????????????)
	T query_(int a, int b, int i, int l, int r) {
		if (r <= a || b <= l) return init_val;
		if (a <= l && r <= b) return ary[i];
		else {
			T vl = query_(a, b, 2 * i + 1, l, (l + r) / 2);
			T vr = query_(a, b, 2 * i + 2, (l + r) / 2, r);
			return func(vl, vr);
		}
	}
	// ??????(b)????°??????§?????????????????????
	T query(int a, int b) {
		return query_(a, b, 0, 0, ary.size() / 2 + 1);
	}

	T operator[](int i) {
		return query(i, i + 1);
	}
};

int calcSum(int a, int d, int n) {
	return n * (2 * a + (n - 1) * d) / 2;
}

int calc(int a, int d, int i, int j) {
	return calcSum(a, d, j) - calcSum(a, d, i);
}

//#define DEBUG

signed main() {
	INIT;
#ifdef DEBUG
	while (true) {
		std::random_device rnd;
		std::mt19937 mt(rnd());
		std::uniform_int_distribution<> rand(/* min */1, /* max */10);
		std::uniform_int_distribution<> randA(/* min */0, /* max */100000);
		std::uniform_int_distribution<> randD(/* min */1, /* max */1000);
		int n = rand(mt);
		int l = rand(mt);
		int k = rand(mt) * rand(mt);
		if (k > n*l) continue;
		if (n*l*k > 20000000) continue;
		n += 4;
		std::vector<int> a(n, 0), d(n, 0);
		FOR(i, 2, n - 2) {
			a[i] = randA(mt);
			d[i] = randD(mt);
		}
#else
	int cntcntcnt = 0;
	while (cntcntcnt++ == 0) {
		VAR(int, n, l, k);
		n += 4;
		std::vector<int> a(n, 0), d(n, 0);
		FOR(i, 2, n - 2) {
			std::cin >> a[i] >> d[i];
		}
#endif

		int ka = k / l;
		int ha = k % l;
		if (ka > 0) {
			--ka;
			ha += l;
		}

		SegmentTree<int> st(n, 0, [](int l, int r) {return l + r; });
		REP(i, n) {
			int t = calc(a[i], d[i], 0, l);
			st.update(i, t);
		}

		int ans = 0;
		REP(L, n) {
			int R = L + ka + 1;
			if (R >= n) break;
			int sum = st.query(L + 1, R);

			int LL = std::max(ha - l, 0LL), RR = l + 1;
			int a1 = a[L], d1 = d[L], a2 = a[R], d2 = d[R];
			auto f = [&](int i) {
				int t;
				if (i >= ha) {
					t = calc(a2, d2, i - ha, i);
				}
				else {
					t = calc(a1, d1, l - ha + i, l) + calc(a2, d2, std::max(0LL, i - ha), i);
				}
				return t;
			};
			int cnt = 0;
			while (cnt++ < 200) {
				int m1 = (LL + LL + RR) / 3;
				int m2 = (LL + RR + RR) / 3;
				if (f(m1) < f(m2)) LL = m1;
				else RR = m2;
			}
			int di = 50;
			LL -= di;
			CHMAX(LL, std::max(ha - l, 0LL));
			RR += di;
			CHMIN(RR, l + 1);
			FOR(i, LL, RR) {
				CHMAX(ans, sum + f(i));
			}
		}
		///////////////////////////////
		// pre 
		ka = k / l;
		ha = k % l;

		REP(L, n) {
			int R = L + ka + 1;
			if (R >= n) break;
			int sum = st.query(L + 1, R);

			int LL = 0, RR = ha + 1;
			int ma = 0;
			auto f = [&](int a1, int d1, int a2, int d2, int i) {
				int t = (ha - i)*(2 * a1 + (2 * l - ha + i - 1)*d1) / 2 + i*(2 * a2 + (i - 1)*d2) / 2;
				return t;
			};
			int cnt = 0;
			while (cnt++ < 50) {
				int m1 = (LL + LL + RR) / 3;
				int m2 = (LL + RR + RR) / 3;
				if (f(a[L], d[L], a[R], d[R], m1) < f(a[L], d[L], a[R], d[R], m2)) LL = m1;
				else RR = m2;
			}
			int di = 50;
			LL -= di;
			CHMAX(LL, 0LL);
			RR += di;
			CHMIN(RR, ha + 1);
			FOR(i, LL, RR) {
				CHMAX(ma, f(a[L], d[L], a[R], d[R], i));
			}
			CHMAX(ans, sum + ma);
		}

		///////////////////////////////
		OUT(ans)BR;
#ifdef DEBUG
		std::vector<int> b(n*l, 0);
		REP(i, n) {
			b[i*l] = a[i];
			FOR(j, 1, l) {
				b[i*l + j] = b[i*l + j - 1] + d[i];
			}
		}
		int ans2 = 0;
		REP(i, n*l) {
			if (i + k > n*l) break;
			int sum = 0;
			FOR(j, i, i + k) sum += b[j];
			CHMAX(ans2, sum);
		}
		if (ans != ans2) {
			OUT("===========")BR;
			OUT(n - 4)SP OUT(l)SP OUT(k)BR;
			FOR(i, 2, n - 2) {
				OUT(a[i])SP OUT(d[i])BR;
			}
			OUT("===========")BR;
			SHOWVECTOR(b);
			OUT(ans)SP OUT(ans2)BR;
			return 0;
		}
		FLUSH;
#endif
	}
	return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 6728
final_rank: 13

code_snippet: #include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<array>
#include<map>
#include<iomanip>
//<< setprecision(5)
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define dme cout<<-1<<endl;return 0
const int mod=1000000007;
const int big=1e9+10;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-7;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){return a/gcd(a,b)*b;}
int main(void){
	llint n,L,K,i;cin>>n>>L>>K;
	llint ans=0;
	vector<pair<llint,llint>>retu(n+2);
	vector<llint>wa(n+2);
	retu[0]=mp(0L,0L);retu[n+1]=mp(0L,0L);
	for(i=1;i<=n;i++){
		llint a,d;cin>>a>>d;retu[i]=mp(a,d);
		wa[i]=wa[i-1]+a*L+d*L*(L-1)/2;
	}
	wa[n+1]=wa[n];
	//????????????????????????,??????????°?????????????
	llint kan=K/L;llint ama=K%L;
	for(i=0;i+kan+1<=n+1;i++){//i????°?????????????
		llint ou=wa[i+kan]-wa[i];//?????¨???????????????????????????
		maxeq(ans,ou+retu[i+kan+1].fir*ama+retu[i+kan+1].sec*ama*(ama-1)/2);
		maxeq(ans,ou+(retu[i].fir+retu[i].sec*L)*ama-retu[i].sec*ama*(ama+1)/2);
		llint dx=retu[i].sec-retu[i+kan+1].sec;//dx?????£????????¶
		llint sy=retu[i].fir+retu[i].sec*L-retu[i+kan+1].fir-retu[i+kan+1].sec*ama;//??£????????¶
		//cout<<"de"<<ans<<" "<<dx<<" "<<sy<<endl;
		if(dx<=0||sy<=0){continue;}
		llint mae=sy/dx;
		if(mae<=0||ama<=mae){continue;}
		llint syo=retu[i].fir+retu[i].sec*(L-mae);//??????
		ou+=syo*mae+retu[i].sec*mae*(mae-1)/2;
		llint ato=ama-mae;
		ou+=retu[i+kan+1].fir*ato+retu[i+kan+1].sec*ato*(ato-1)/2;
		maxeq(ans,ou);
	}
	kan--;ama+=L;//?????????????°????????????? kan?????????????????????
	for(i=0;i+kan+1<=n+1;i++){//i????°?????????????
		//cout<<"de"<<ans<<endl;
		llint ou=wa[i+kan]-wa[i];//?????¨???????????????????????????
		llint dx=retu[i].sec-retu[i+kan+1].sec;//dx?????£????????¶
		llint sy=retu[i].fir+retu[i].sec*L-retu[i+kan+1].fir-retu[i+kan+1].sec*ama;//??£????????¶
		if(dx<=0||sy<=0){continue;}
		llint mae=sy/dx;
		if(mae<=ama-L||L<=mae){continue;}
		llint syo=retu[i].fir+retu[i].sec*(L-mae);//??????
		ou+=syo*mae+retu[i].sec*mae*(mae-1)/2;
		llint ato=ama-mae;
		ou+=retu[i+kan+1].fir*ato+retu[i+kan+1].sec*ato*(ato-1)/2;
		maxeq(ans,ou);
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 5204
final_rank: 2

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef ll Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
//#define mt make_tuple
//typedef tuple<double,int,double> tp;
//typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const double EPS=1e-7;
Def inf=sizeof(Def)==sizeof(long long)?2e18:1e9;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
ll n,l,k;
vp in(n);
ll calc(ll q,ll T){
	if(q<0||q>=n)return 0;
	return (ll)(in[q].first*2+(T-1)*in[q].second)*T/2;
}
ll f(ll a,ll t,ll e){
//	cout<<a<<" "<<t<<endl;
	ll T=k-e*l-t;
	ll co=e*l;
	ll sum=0;
	ll q=a+e;
	if(t<0||t>l)return 0;
	if(T<0||T>l)return 0;
	sum+=calc(q,T);
//	cout<<a<<" "<<t<<" "<<sum<<endl;
	sum+=calc(a-1,l)-calc(a-1,l-t);
//	cout<<a<<" "<<t<<" "<<sum<<endl;
	return sum;
}
int main(){
	cin>>n>>l>>k;
	in=vp(n);
	rep(i,n)cin>>in[i].first>>in[i].second;
	vi su(n+1);
	rep(i,n){
		su[i+1]=(in[i].first*2+(l-1)*in[i].second)*l/2;
	}
	rep(i,n)su[i+1]+=su[i];
	ll out=0;
	ll e=k/l;
	rep(q,n-e+1){
//		cout<<su[q+(k-1)/l]-su[q]<<endl;
		ll sum=su[q+e]-su[q];
		ll L=0,r=k-e*l+1;
//		cout<<L<<" "<<r<<endl;
		while(r-L>10){
			ll h1=L+(r-L)/3;
			ll h2=L+(r-L)/3*2;
			ll t1=f(q,h1,e);
			ll t2=f(q,h2,e);
			if(t1>t2)r=h2;
			else L=h1;
		}
		loop(i,max(0ll,L-100),min(k-e*l+1,L+100)){
			out=max(out,sum+f(q,i,e));
		}
	}
	if(k-(k/l-1)*l<2*l){
		e=k/l-1;
		rep(q,n-e+1){
	//		cout<<su[q+(k-1)/l]-su[q]<<endl;
			ll sum=su[q+e]-su[q];
			ll L=0,r=k-e*l+1;
	//		cout<<L<<" "<<r<<endl;
			while(r-L>10){
				ll h1=L+(r-L)/3;
				ll h2=L+(r-L)/3*2;
				ll t1=f(q,h1,e);
				ll t2=f(q,h2,e);
				if(t1>t2)r=h2;
				else L=h1;
			}
			loop(i,max(0ll,L-100),min(k-e*l+1,L+100)){
				out=max(out,sum+f(q,i,e));
			}
		}
	}
	cout<<out<<endl;
}
accuracy: 1
time_ms: 220
memory_kb: 5900
final_rank: 12

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
template<typename Func>
int findMax(int L, int R, Func f) { //[L, R)
    int lo = L, hi = R;
    while (lo + 1 != hi) {
        int mi = (lo + hi) / 2;
        if (f(mi) - f(mi - 1) > 0) lo = mi;
        else hi = mi;
    }
    return lo;
}
int getrand(int l, int r) { // [l, r]
    static uint32_t y = time(NULL);
    y ^= (y << 13); y ^= (y >> 17);
    y ^= (y << 5);
    return y % (r - l + 1) + l;
}
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§  
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i     
?????????????????????????????? ???  |???|     
????????? /?????? /??£??£??£??£/??????|  
??? ???_(__??????/???    ???/ .| .|????????????  
??? ????????????/????????????/??????u??????  
---------------------------------------------------------------------------------------------------*/



typedef long long ll;
ll N, L, A[101010], D[101010];
ll K;
ll sm[101010];
ll smr[101010];
//--------------------------------------------------------------------------------------------------
ll get(int idx, ll X, ll Y) { // ??°???idx???[X,Y]
    if (X > Y) return 0;
    if (L < Y) Y = L;
    ll a = A[idx] + (X - 1) * D[idx];
    ll b = A[idx] + (Y - 1) * D[idx];
    return (Y - X + 1) * (a + b) / 2;
}
ll getback(int idx, ll X, ll Y) { // ??°???idx???????????????[X,Y]
    if (X > Y) return 0;
    if (L < Y) Y = L;

    ll AA = A[idx] + (L - 1) * D[idx];
    ll DD = -D[idx];

    ll a = AA + (X - 1) * DD;
    ll b = AA + (Y - 1) * DD;
    return (Y - X + 1) * (a + b) / 2;
}
//--------------------------------------------------------------------------------------------------
ll XX, YY, res;
ll f(int lef) {
    ll le = getback(XX - 1, 1, lef);
    ll ri = get(YY + 1, 1, res - lef);
    if (L < res - lef) return -(res - lef);
    if (res - lef < 1) return -(res - lef);
    return le + ri;
}
//--------------------------------------------------------------------------------------------------
ll naive() {
    vector<ll> v;
    rep(i, 0, N) rep(j, 0, L) v.push_back(A[i] + D[i] * j);
    //fore(i, v) printf("%lld\n", i);

    ll ans = 0;
    rep(i, 0, N * L) {
        int X = i;
        int Y = i + K - 1;
        if (N * L <= Y) break;

        ll sm = 0;
        rep(j, X, Y + 1) sm += v[j];
        ans = max(ans, sm);
    }

    return ans;
}
//--------------------------------------------------------------------------------------------------
void debug() {
    N = getrand(1, 5), L = getrand(1, 5), K = getrand(1, N * L);
    rep(i, 0, N) {
        A[i] = getrand(0, 10);
        D[i] = getrand(1, 10);
    }
}
//--------------------------------------------------------------------------------------------------
ll opt() {
    rep(i, 0, N) sm[i] = get(i, 1, L);
    smr[0] = sm[0];
    rep(i, 1, N) smr[i] = smr[i - 1] + sm[i];

    if (K == N * L) return smr[N - 1];

    if (K <= L) {
        ll ans = 0;
        rep(i, 0, N) ans = max(ans, getback(i, 1, K));
        rep(i, 0, N - 1) {
            XX = i + 1;
            YY = i;
            res = K;
            int lef = findMax(0, L, f);
            ans = max(ans, f(lef));
        }
        return ans;
    }

    ll cnt = K / L;
    ll ans = 0;
    rep(i, 0, N - cnt + 1) {
        int X = i;
        int Y = i + cnt - 1;

        ll XYsm = smr[Y];
        if (X) XYsm -= smr[X - 1];

        if (X == 0) ans = max(ans, XYsm + get(Y + 1, 1, K - cnt * L));
        else if (Y == N - 1) {
            ans = max(ans, XYsm + getback(X - 1, 1, K - cnt * L));
        }
        else {
            XX = X;
            YY = Y;
            res = K - cnt * L;
            int lef = findMax(0, res + 1, f);
            ans = max(ans, XYsm + f(lef));
        }
    }

    cnt--;
    rep(i, 0, N - cnt + 1) {
        int X = i;
        int Y = i + cnt - 1;

        ll XYsm = smr[Y];
        if (X) XYsm -= smr[X - 1];

        if (X == 0) {
            if(K - cnt * L <= L) ans = max(ans, XYsm + get(Y + 1, 1, K - cnt * L));
        }
        else if (Y == N - 1) {
            if(K - cnt * L <= L) ans = max(ans, XYsm + getback(X - 1, 1, K - cnt * L));
        }
        else {
            XX = X;
            YY = Y;
            res = K - cnt * L;
            int lef = findMax(0, L, f);
            ans = max(ans, XYsm + f(lef));
        }
    }

    return ans;
}
//--------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> L >> K;
    rep(i, 0, N) cin >> A[i] >> D[i];

    //ll naive_ans = naive();
    //printf("%lld\n", naive_ans);

    ll ans = opt();
    printf("%lld\n", ans);
    return;

    rep(_, 0, 110) {
        debug();

        ll naive_ans = naive();
        //printf("%lld\n", naive_ans);

        ll ans = opt();

        //printf("%lld\n", ans);

        if (naive_ans != ans) {
            printf("!!\n");
            printf("%lld %lld %lld\n", N, L, K);
            rep(i, 0, N) printf("%lld %lld\n", A[i], D[i]);
            return;
        }
    }
}
accuracy: 1
time_ms: 30
memory_kb: 6284
final_rank: 6