Problem:
Problem F:
RabbitLunch
うさぎは昼食ににんじんとキウイを1 個ずつ食べる. うさぎはとても個性的なので, 食べるにんじんの種類もキウイの種類も同じであるような, 異なる2 匹のうさぎが存在してはならない.
にんじんは $M$ 種類ある. $i$ 種類目のにんじんは $m_i$ 個ある. キウイは $N$ 種類ある. $i$ 種類目のキウイは $n_i$ 個ある. 最大何匹のうさぎが昼食をとれるか求めよ.
$m_i$ と $n_i$ は次の漸化式を用いて生成せよ.
$m_0 = m0$
$m_{i+1} = (m_i * 58 + md )$ mod $(N + 1)$
$n_0 = n0$
$n_{i+1} = (n_i * 58 + nd )$ mod $(M + 1)$
Constraints
$M$ will be between 1 and 2,500,000, inclusive.
$N$ will be between 1 and 2,500,000, inclusive.
$m0$ and $md$ will be between 0 and $N$, inclusive.
$n0$ and $nd$ will be between 0 and $M$, inclusive.
Input
入力は以下の形式で与えられる:
$M$ $N$ $m0$ $md$ $n0$ $nd$
Output
昼食をとれるうさぎの匹数の最大値を表す整数を 1 行に出力せよ.
Sample Input 1
2 3 1 3 1 0
Sample Output 1
2
Sample Input 2
5 8 1 2 3 4
Sample Output 2
19

Submitted solutions:

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

#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef pair<int,int> pii;

const int maxn=2500005;
const int mlog=23;
int n,m,md,nd,lim;
int a[maxn],b[maxn],prv[maxn*2],nxt[maxn*2];
ll dat[maxn*2];
inline void del(int x){
	nxt[prv[x]]=nxt[x];
	prv[nxt[x]]=prv[x];
}

namespace Fenwick{
	ll t[maxn*2];
	void add(int v,int val){
		if(!val)return;
		for(;v<=lim;v+=(v&-v))t[v]+=val;
	}
	int qry(int &k){
		int res=0;
		for(int i=mlog-1;i>=0;i--){
			int cur=res+(1<<i);
			if(cur<=lim&&t[cur]<k){
				k-=t[cur];
				res=cur;
			}
		}
		return res+1;
	}
}

int main(){
	lim=5000000;
	scanf("%d%d%d%d%d%d",&m,&n,&a[0],&md,&b[0],&nd);
	REP(i,m-1)a[i]=(a[i-1]*58+md)%(n+1);
	REP(i,n-1)b[i]=(b[i-1]*58+nd)%(m+1);
	sort(a,a+m);
	rep(i,n)if(b[i])Fenwick::add(lim-b[i]+1,1),dat[lim-b[i]+1]++;
	REP(i,lim){
		prv[i]=i-1;
		nxt[i]=i+1;
	}
	ll ans=0;
	for(int i=m-1;i>=0;i--)if(a[i]){
		int tmp=a[i],cur=Fenwick::qry(tmp);
		ans+=a[i]-tmp;
		if(cur<=lim){
			ans+=tmp;
			int dlt=dat[cur]-tmp;
			dat[prv[cur]]+=dlt;
			Fenwick::add(prv[cur],dlt);
			dat[cur]-=dlt;
			Fenwick::add(cur,-dlt);
		}
		else cur--;
		if(nxt[cur]==5000001){
			Fenwick::add(cur,-dat[cur]);
			lim=prv[cur]; 
			dat[cur]=0;
			del(cur);
		}
		else{
			Fenwick::add(cur,dat[nxt[cur]]);
			dat[cur]+=dat[nxt[cur]];
			dat[nxt[cur]]=0;
			if(nxt[nxt[cur]]==5000001)lim=cur; 
			del(nxt[cur]);
		}
	}
	printf("%lld\n",ans);
	return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 105488
final_rank: 15

code_snippet: // AOJ #2374 RabbitLunch
// 2025.2.20

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

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int M, N;
    cin >> M >> N;
    
    ll m0, md;
    cin >> m0 >> md;
    vector<ll> a(M);
    a[0] = m0;
    for (int i = 1; i < M; i++) a[i] = (a[i - 1] * 58 + md) % (N + 1);
    
    ll n0, nd;
    cin >> n0 >> nd;
    vector<ll> b(N);
    b[0] = n0;
    for (int i = 1; i < N; i++) b[i] = (b[i - 1] * 58 + nd) % (M + 1);
    
    sort(b.begin(), b.end());
    reverse(b.begin(), b.end());
    
    vector<ll> c(N + 1, 0);
    for (int i = 0; i < M; i++) c[0]++, c[a[i]]--;
    for (int i = 1; i < N; i++) c[i] += c[i - 1];
    
    ll cs = 0, bs = 0;
    for (int i = 0; i < N; i++){
        cs += c[i], bs += b[i];
        bs = min(bs, cs);
    }
    cout << bs << "\n";
    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 61764
final_rank: 7

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

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    ll N, M, N0, ND, M0, MD;
    cin >> N >> M >> N0 >> ND >> M0 >> MD;
    vector<ll> AC(M+1,0), BC(N+1,0);
    rep(i,0,N) {
        AC[N0]++;
        N0 *= 58;
        N0 += ND;
        N0 %= (M+1);
    }
    rep(i,0,M) {
        BC[M0]++;
        M0 *= 58;
        M0 += MD;
        M0 %= (N+1);
    }
    vector<ll> A, B;
    rep(i,0,M+1) rep(j,0,AC[i]) A.push_back(i);
    rep(i,0,N+1) rep(j,0,BC[i]) B.push_back(i);
    vector<pair<ll,ll>> X(N+1);
    ll Cur = 0;
    int it = 0;
    rep(i,0,N+1) {
        while(it < M && B[it] <= i) Cur += B[it], it++;
        X[i] = {Cur,M-it};
    }
    Cur = 0;
    ll ANS = INF;
    rep(i,0,N+1) {
        chmin(ANS, Cur + X[N-i].first + X[N-i].second * (N-i));
        if (i < N) Cur += A[i];
    }
    cout << ANS << endl;
}
accuracy: 1
time_ms: 140
memory_kb: 123756
final_rank: 14

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

using ll = long long;

inline ll nx(ll pre, ll d, ll mod) {
  return (pre * 58 + d) % mod;
}

int main() {
  ll M, N, m0, md, n0, nd; cin >> M >> N >> m0 >> md >> n0 >> nd;
  vector<ll> A(M), B(N);
  {
    ll m = m0, n = n0;
    for (int i = 0; i < M; ++i) A[i] = m, m = nx(m, md, N+1);
    for (int i = 0; i < N; ++i) B[i] = n, n = nx(n, nd, M+1);
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  sort(B.begin(), B.end());
  
  vector<ll> SB(M+1, 0); // SB[m] := sum_i min(m, B[i])
  ll lb = 0, sumb = 0;
  for (int m = 0; m <= M; ++m) {
    while (lb < N && B[lb] < m) {
      sumb += B[lb];
      lb++;
    }
    SB[m] = sumb + (N - lb) * m;
    // cout << m << " " << SB[m] << endl;
  }
  
  ll suma = 0;
  for (int m = 0; m < M; ++m) {
    ll mx = SB[m+1];
    ll diff = mx - suma;
    A[m] = min(A[m], diff);
    suma += A[m];
  }
  cout << suma << endl;
  
  return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 61896
final_rank: 8

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

int main(){
    
    ll M,N,p,q,r,s;
    cin>>M>>N>>p>>q>>r>>s;
    vll A(N+1,0),B(N+1,0),BB(N);
    rep(i,M){
        A[p]++;
        p=58*p+q;
        p%=(N+1);
    }
    rep(i,N){
        BB[i]=r;
        r=58*r+s;
        r%=(M+1);
    }
    sort(BB.begin(),BB.end());
    reverse(BB.begin(),BB.end());
    BB.push_back(0);
    for(int i=0;i<=N;i++){
        B[i+1]+=BB[i]-BB[i+1];
    }
    ll an=0,pos=N,adis=0;
    for(ll n=N;n>=0;n--)rep(j,A[n]){
        while(pos>=0&&B[pos]==0)pos--;
        if(pos==-1){
            an+=min(n,adis);
            adis-=min(n,adis);
            continue;
        }
        if(pos>n){
            an+=n;
            B[pos]--;
            adis+=pos-n;
        }
        else{
            B[pos]--;
            if(pos+adis>=n){
                an+=n;
                adis=pos+adis-n;
            }
            else{
                an+=pos+adis;
                adis=0;
            }
        }
    }
    cout<<an<<endl;
}
accuracy: 1
time_ms: 230
memory_kb: 83036
final_rank: 12

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

int main(){
    
    ll M,N,p,q,r,s;
    cin>>M>>N>>p>>q>>r>>s;
    vll A(N+1,0),B(N+1,0),BB(N);
    rep(i,M){
        A[p]++;
        // cout<<p<<" \n"[i==M-1];
        p=58*p+q;
        p%=(N+1);

    }
    rep(i,N){
        BB[i]=r;
        // cout<<r<<" \n"[i==N-1];
        r=58*r+s;
        r%=(M+1);
    }
    sort(BB.begin(),BB.end());
    reverse(BB.begin(),BB.end());
    BB.push_back(0);
    for(int i=0;i<=N;i++){
        B[i+1]+=BB[i]-BB[i+1];
    }
    // rep(i,N+1)cout<<A[i]<<" \n"[i==N];
    // rep(i,N+1)cout<<B[i]<<" \n"[i==N];
    ll an=0;
    ll pos=N;
    ll adis=0;
    for(ll n=N;n>=0;n--){
        rep(j,A[n]){
            while(pos>=0&&B[pos]==0)pos--;
            if(pos==-1){
                an+=min(n,adis);
                adis-=min(n,adis);
                continue;
            }
            // cout<<n<<" "<<pos<<endl;
            if(pos>n){
                an+=n;
                B[pos]--;
                adis+=pos-n;
            }
            else{
                B[pos]--;
                if(pos+adis>=n){
                    an+=n;
                    adis=pos+adis-n;
                }
                else{
                    an+=pos+adis;
                    adis=0;
                }
            }
        }
    }
    cout<<an<<endl;

}
accuracy: 1
time_ms: 220
memory_kb: 83100
final_rank: 11

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

int main(){
    
    ll M,N,p,q,r,s;
    cin>>M>>N>>p>>q>>r>>s;
    vll A(N+1,0),B(N+1,0),BB(N);
    rep(i,M){
        A[p]++;
        // cout<<p<<" \n"[i==M-1];
        p=58*p+q;
        p%=(N+1);

    }
    rep(i,N){
        BB[i]=r;
        // cout<<r<<" \n"[i==N-1];
        r=58*r+s;
        r%=(M+1);
    }
    sort(BB.begin(),BB.end());
    reverse(BB.begin(),BB.end());
    BB.push_back(0);
    for(int i=0;i<=N;i++){
        B[i+1]+=BB[i]-BB[i+1];
    }
    // rep(i,N+1)cout<<A[i]<<" \n"[i==N];
    // rep(i,N+1)cout<<B[i]<<" \n"[i==N];
    ll an=0;
    ll pos=N;
    ll adis=0;
    for(ll n=N;n>=0;n--){
        rep(j,A[n]){
            while(pos>=0&&B[pos]==0)pos--;
            if(pos==-1){
                an+=min(n,adis);
                adis-=min(n,adis);
                continue;
            }
            // cout<<n<<" "<<pos<<endl;
            if(pos>n){
                an+=n;
                B[pos]--;
                adis+=pos-n;
            }
            else{
                B[pos]--;
                if(pos+adis>=n){
                    an+=n;
                    adis=pos+adis-n;
                }
                else{
                    an+=pos+adis;
                    adis=0;
                }
            }
        }
        if(pos==-1)break;
    }
    cout<<an<<endl;

}
accuracy: 0.42857142857142855
time_ms: 140
memory_kb: 68704
final_rank: 17

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

int M, N, m0, md, n0, nd, a[2500032], b[2500032];

int main() {
	cin >> M >> N >> m0 >> md >> n0 >> nd;
	for (int i = 0; i < M; i++) {
		a[m0] += 1;
		m0 = (m0 * 58 + md) % (N + 1);
	}
	for (int i = 0; i < N; i++) {
		b[n0] += 1;
		n0 = (n0 * 58 + nd) % (M + 1);
	}
	int rem = M, pos = M; long long s1 = 0, s2 = 0, loss = 0;
	for (int i = 0; i < N; i++) {
		rem -= a[i];
		s1 += rem;
		while (b[pos] == 0) {
			pos -= 1;
		}
		b[pos] -= 1;
		s2 += pos;
		loss = max(loss, s2 - s1);
	}
	cout << s2 - loss << endl;
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 22944
final_rank: 1

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

int M, N, m0, md, n0, nd; long long a[2500032], b[2500032];

int main() {
	cin >> M >> N >> m0 >> md >> n0 >> nd;
	for (int i = 0; i < M; i++) {
		a[m0] += 1;
		m0 = (m0 * 58 + md) % (N + 1);
	}
	for (int i = 0; i < N; i++) {
		b[n0] += 1;
		n0 = (n0 * 58 + nd) % (M + 1);
	}
	int rem = M, pos = M; long long s1 = 0, s2 = 0, loss = 0;
	for (int i = 0; i < N; i++) {
		rem -= a[i];
		s1 += rem;
		while (b[pos] == 0) {
			pos -= 1;
		}
		b[pos] -= 1;
		s2 += pos;
		loss = max(loss, s2 - s1);
	}
	cout << s2 - loss << endl;
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 42476
final_rank: 2

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

long long solve(int N, int M, const vector<int>& P, const vector<int>& Q) {
	vector<int> cnt(N + 1);
	for (int i = 0; i < M; i++) {
		if (P[i] >= 1) {
			cnt[P[i] - 1] += 1;
		}
	}
	for (int i = N - 1; i >= 0; i--) {
		cnt[i] += cnt[i + 1];
	}
	vector<int> SQ = Q;
	sort(SQ.begin(), SQ.end(), greater<int>());
	long long sumq = 0;
	for (int i = 0; i < N; i++) {
		sumq += Q[i];
	}
	long long cur = 0, border = 0, loss = 0;
	for (int i = 0; i < N; i++) {
		cur += SQ[i];
		border += cnt[i];
		loss = max(loss, cur - border);
	}
	return sumq - loss;
}

int main() {
	int M, N, m0, md, n0, nd;
	cin >> M >> N >> m0 >> md >> n0 >> nd;
	vector<int> P(M), Q(N);
	P[0] = m0;
	for (int i = 0; i < M - 1; i++) {
		P[i + 1] = (P[i] * 58LL + md) % (N + 1);
	}
	Q[0] = n0;
	for (int i = 0; i < N - 1; i++) {
		Q[i + 1] = (Q[i] * 58LL + nd) % (M + 1);
	}
	long long answer = solve(N, M, P, Q);
	cout << answer << endl;
	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 42172
final_rank: 3

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

using namespace std;
using ll = long long;

const int N = 2.5e6 + 5;
int n, m, a[N], b[N];
ll sa[N];

void generate() {
    int da, db;
    scanf("%d%d%d%d", &a[1], &da, &b[1], &db);
    for (int i = 2; i <= n; i++) a[i] = (a[i - 1] * 58 + da) % (m + 1);
    for (int i = 2; i <= m; i++) b[i] = (b[i - 1] * 58 + db) % (n + 1);
}

int main() {
    scanf("%d%d", &n, &m);
    generate();

    sort(a + 1, a + n + 1, greater<>());
    for (int i = 1; i <= n; i++) sa[i] = sa[i - 1] + a[i];
    sort(b + 1, b + m + 1);
    ll ans = sa[n], sum = 0;

    for (int i = 1, j = 0; i <= n; i++) {
        while (j < m && b[j + 1] <= i) j++, sum += b[j];
        ans = min(ans, min(sum + ll(m - j) * i, sa[i]) + sa[n] - sa[i]);
    }

    printf("%lld\n", ans);
    return 0;
}
accuracy: 1
time_ms: 310
memory_kb: 42588
final_rank: 4

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

using namespace std;
using ll = long long;

const int N = 2.5e6 + 5;
int n, m, a[N], b[N];
ll sa[N], sb[N];

void generate() {
    int da, db;
    scanf("%d%d%d%d", &a[1], &da, &b[1], &db);
    for (int i = 2; i <= n; i++) a[i] = (a[i - 1] * 58 + da) % (m + 1);
    for (int i = 2; i <= m; i++) b[i] = (b[i - 1] * 58 + db) % (n + 1);
}

int main() {
    scanf("%d%d", &n, &m);
    generate();

    sort(a + 1, a + n + 1, greater<>());
    for (int i = 1; i <= n; i++) sa[i] = sa[i - 1] + a[i];
    sort(b + 1, b + m + 1);
    for (int i = 1; i <= m; i++) sb[i] = sb[i - 1] + b[i];
    ll ans = sa[n];

    for (int i = 1, j = 0; i <= n; i++) {
        while (j < m && b[j + 1] <= i) j++;
        ans = min(ans, min(sb[j] + ll(m - j) * i, sa[i]) + sa[n] - sa[i]);
    }

    printf("%lld\n", ans);
    return 0;
}
accuracy: 1
time_ms: 310
memory_kb: 62152
final_rank: 10

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

using namespace std;
using ll = long long;

const int N = 2.5e6 + 5;
int n, m, a[N], b[N];
ll sa[N], sb[N];

void generate() {
    int da, db;
    scanf("%d%d%d%d", &a[1], &da, &b[1], &db);
    for (int i = 2; i <= n; i++) a[i] = (a[i - 1] * 58 + da) % (m + 1);
    for (int i = 2; i <= m; i++) b[i] = (b[i - 1] * 58 + db) % (n + 1);
}

int main() {
    scanf("%d%d", &n, &m);
    generate();

    sort(a + 1, a + n + 1, greater<>());
    for (int i = 1; i <= n; i++) sa[i] = sa[i - 1] + a[i];
    sort(b + 1, b + n + 1);
    for (int i = 1; i <= n; i++) sb[i] = sb[i - 1] + b[i];
    ll ans = sa[n];

    for (int i = 1, j = 0; i <= n; i++) {
        while (j < m && b[j + 1] <= i) j++;
        ans = min(ans, min(sb[j] + ll(m - j) * i, sa[i]) + sa[n] - sa[i]);
    }

    printf("%lld\n", ans);
    return 0;
}
accuracy: 0.2857142857142857
time_ms: 80
memory_kb: 31964
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,s,t) for (int i = (int)(s); i<(int)(t); ++i)
#define all(x) (x).begin(), (x).end()

template <class T>
void print(vector<T>& data) {
    rep(i,0,data.size()) cout << data[i] << " ";
    cout << endl;
}

struct FT {
    int n;
    vector<ll> data;

    FT(int n) : n(n), data(n+1) {}

    ll sum(int i) {
        ll ret = 0;
        for (; i > 0; i -= i&-i) ret += data[i];
        return ret;
    }

    void add(int i, ll x) {
        for (++i; i <= n; i += i&-i) data[i] += x;
    }

    // int lower_bound(ll x) {
    //     int k = 1;
    //     while (k * 2 <= n) k <<= 1;
    //     int i = 0;
    //     ll v = 0;
    //     for (; k > 0; k >>= 1) {
    //         if (i+k <= n) continue;
    //         ll nv = v + data[i+k];
    //         if (nv > x) {
    //             v = nv;
    //             i += k;
    //         }
    //     }
    //     return i+1;
    // }

    // int upper_bound(ll x) {
    //     int k = 1;
    //     while (k * 2 <= n) k <<= 1;
    //     int i = 0;
    //     ll v = 0;
    //     for (; k > 0; k >>= 1) {
    //         if (i+k <= n) continue;
    //         ll nv = v + data[i+k];
    //         if (nv >= x) {
    //             v = nv;
    //             i += k;
    //         }
    //     }
    //     return i+1;
    // }
};

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

    int M, N, m0, md, n0, nd;
    cin >> M >> N >> m0 >> md >> n0 >> nd;

    vector<int> m(M), n(N);
    m[0] = m0;
    n[0] = n0;
    rep(i,0,M-1) {
        m[i+1] = (m[i]*58+md) % (N+1);
    }
    rep(i,0,N-1) {
        n[i+1] = (n[i]*58+nd) % (M+1);
    }

    sort(all(m));
    reverse(all(m));
    sort(all(n));
    reverse(all(n));
    ll ans = 0;

    // print(m);
    // print(n);

    FT ft(N);
    ft.add(0, n[0]);
    rep(i,0,N-1) {
        ft.add(i+1, n[i+1]-n[i]);
    }

    rep(i,0,M) {
        while (N > 0 && ft.sum(N) == 0) {
            --N;
        }
        if (N == 0) break;

        // cout << i << endl;
        // rep(i,1,N+1) cout << ft.sum(i) << " ";
        // cout << endl;
        // cout << N << endl;

        m[i] = min(m[i], N);
        ans += m[i];

        if (ft.sum(m[i]) != ft.sum(m[i]+1)) {
            ft.add(0, -1);
            ft.add(m[i], 1);
        } else {
            ll x = ft.sum(m[i]);

            int lb = 0, ub = N+1;
            while (ub - lb > 1) {
                int m = (lb + ub) / 2;
                if (ft.sum(m) > x) lb = m;
                else ub = m;
            }
            int l = lb;

            lb = l, ub = N+1;
            while (ub - lb > 1) {
                int m = (lb + ub) / 2;
                if (ft.sum(m) >= x) lb = m;
                else ub = m;
            }
            int r = lb;

            ft.add(0, -1);
            ft.add(l, 1);
            ft.add(r-(m[i]-l), -1);
            ft.add(r, 1);
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 2060
memory_kb: 42160
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,s,t) for (int i = (int)(s); i<(int)(t); ++i)
#define all(x) (x).begin(), (x).end()

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

    int M, N, m0, md, n0, nd;
    cin >> M >> N >> m0 >> md >> n0 >> nd;

    vector<int> m(M), n(N);
    m[0] = m0;
    n[0] = n0;
    rep(i,0,M-1) {
        m[i+1] = (m[i]*58+md) % (N+1);
    }
    rep(i,0,N-1) {
        n[i+1] = (n[i]*58+nd) % (M+1);
    }
    sort(all(m));
    reverse(all(m));
    sort(all(n));
    reverse(all(n));
    ll ans = 0;
    int base = 0;
    int j = 0;

    rep(i,0,M) {
        while (N > 0 && n[N-1] == base) {
            --N;
        }

        m[i] = min(m[i], N);

        if (m[i] < N-j) {
            ans += m[i];
            j += m[i];
            continue;
        }

        m[i] -= N-j;
        ans += N-j;
        j = 0;
        ++base;

        while (N > 0 && n[N-1] == base) {
            --N;
        }

        m[i] = min(m[i], N);

        ans += m[i];
        j += m[i];
        if (j == N) {
            ++base;
            j = 0;
        }
    }
    cout << ans << endl;

}
accuracy: 0.2857142857142857
time_ms: 110
memory_kb: 10996
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define F first
#define S second
#define ll long long
#define N 2500010
using namespace std;
ll n,m,a[N],b[N],ad,bd,sum=0;
int main(){
	ll i,j;
	cin>>n>>m>>a[0]>>ad>>b[0]>>bd;
	for(i=1;i<=n;i++)
	{
		a[i]=(a[i-1]*58+ad)%(m+1);
	}
	for(i=1;i<=m;i++)
	{
		b[i]=(b[i-1]*58+bd)%(n+1);
	}
	sort(a,a+n);
	sort(b,b+m);
	ll ans=LINF;
	for(i=0;i<n;i++)
	{
		sum+=a[i];
	}
	for(i=n-1,j=0;i>=-1;i--)
	{
		while(j<m&&b[j]<=n-i-1)
		{
			sum+=b[j];
			j++;
		}
		ans=min(ans,sum+(n-i-1)*(m-j));
		if(i>=0)
		{
			sum-=a[i];
		}
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 42488
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2500006;
int n,m,a[MAXN],b[MAXN],md,nd;
ll sum[MAXN];
int main() {
	scanf("%d%d%d%d%d%d",&n,&m,&a[1],&md,&b[1],&nd);
	for(int i=2; i<=n; ++i)a[i]=(a[i-1]*58+md)%(m+1);
	for(int i=2; i<=m; ++i)b[i]=(b[i-1]*58+nd)%(n+1);
	sort(a+1,a+n+1),sort(b+1,b+m+1);
	for(int i=1; i<=n; ++i) sum[i]=sum[i-1]+a[i];
	ll ans=9e18,tmp=0;
	for(int i=n,j=0; i>=0; --i) {
		while(j<m&&b[j+1]<=n-i)tmp+=b[++j];
		ans=min(ans,sum[i]+tmp+1ll*(n-i)*(m-j));
	}
	printf("%lld\n",ans);
	return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 42332
final_rank: 5

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

int n, m;
ll a[2500250], b[2500250];
ll prea[2500250], preb[2500250];

int main(){
	ios::sync_with_stdio(false);
	
	cin >> n >> m;
	ll da, db;
	cin >> a[0] >> da >> b[0] >> db;
	rep(i, n-1) a[i+1] = (a[i] * 58ll + da) % (m+1);
	rep(i, m-1) b[i+1] = (b[i] * 58ll + db) % (n+1);
	
	sort(a, a+n);
	sort(b, b+m);
//	rep(i, n)
//		cout << a[i] << " ";
//	cout << endl;
//	rep(i, m)
//		cout << b[i] << " ";
//	cout << endl;
	rep(i, n)
		prea[i+1] = prea[i] + a[i];
	rep(i, m)
		preb[i+1] = preb[i] + b[i];
	
	auto calc = [&](int i, int j){ return prea[i] + preb[j] + 1ll * (n-i) * (m-j); };
	ll ans = 0x3f3f3f3f3f3f3f3fll;
	int j = 0;
	for(int i = n; i >= 0; --i){
		while(j < m && calc(i, j+1) < calc(i, j))
			++j;
		ans = min(ans, calc(i, j));
	}
	cout << ans << "\n";
	
	return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 81568
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
int n,m,c,d;
int a[2500005],b[2500005];
long long s1[2500005],s2[2500005];
int main() {
	cin>>n>>m>>a[1]>>c>>b[1]>>d;
	for(int i=2; i<=n; i++) {
		a[i]=(a[i-1]*58+c)%(m+1);
	}
	for(int i=2; i<=m; i++) {
		b[i]=(b[i-1]*58+d)%(n+1);
	}
	sort(a+1,a+1+n);
	sort(b+1,b+1+m);
	for(int i=1; i<=n; i++) {
		s1[i]=s1[i-1]+a[i];
	}
	for(int i=1; i<=m; i++) {
		s2[i]=s2[i-1]+b[i];
	}
	long long ans=0x3f3f3f3f3f3f3f3f;
	for(int i=n,j=0; i>=0; i--) {
		while(j<m&&b[j+1]<n-i)j++;
		ans=min(ans,1ll*(n-i)*(m-j)+s1[i]+s2[j]);
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 310
memory_kb: 61940
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
int n,m,c,d;
int a[2500005],b[2500005];
int s1[2500005],s2[2500005];
int main() {
	cin>>n>>m>>a[1]>>c>>b[1]>>d;
	for(int i=2; i<=n; i++) {
		a[i]=(a[i-1]*58+c)%(m+1);
	}
	for(int i=2; i<=m; i++) {
		b[i]=(b[i-1]*58+d)%(n+1);
	}
	sort(a+1,a+1+n);
	sort(b+1,b+1+m);
	for(int i=1; i<=n; i++) {
		s1[i]=s1[i-1]+a[i];
	}
	for(int i=1; i<=m; i++) {
		s2[i]=s2[i-1]+b[i];
	}
	long long ans=0x3f3f3f3f3f3f3f3f;
	for(int i=n,j=0; i>=0; i--) {
		while(j<m&&b[j+1]<n-i)j++;
		ans=min(ans,1ll*(n-i)*(m-j)+s1[i]+s2[j]);
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 0.2857142857142857
time_ms: 90
memory_kb: 25648
final_rank: 19