제출 #1358201

#제출 시각아이디문제언어결과실행 시간메모리
1358201nathako9nSelf Study (JOI22_ho_t2)C++20
100 / 100
121 ms5128 KiB
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=300005;
ll n,m,A[N+2],B[N+3];

ll ch(ll mid){
    ll tot=0;
    ll limit = n * m; // Total available slots across all courses
    for(int i=1;i<=n;i++){
        ll want=mid;
        ll tclas=0,tself=0;
        
        // If attending class is better than self-study
        if(A[i]>B[i]){
            tclas=want/A[i];
            if(tclas*A[i]<want)++tclas;
            tclas=min(tclas,m); // Use up to m native slots for this course
            want-=tclas*A[i];
            tot+=tclas;
        }
        
        // Use any remaining slots (native or from other courses) for self-study
        if(want>0){
            tself=want/B[i];
            if(tself*B[i]<want)++tself;
            tot+=tself;
        }    
        
        // CHANGE 1: Prevent integer overflow of tot and exit early
        if(tot > limit) return limit + 1; 
    }
    return tot;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    if (!(cin >> n >> m)) return 0;
    for(int i=1;i<=n;i++) cin >> A[i];
    for(int i=1;i<=n;i++) cin >> B[i];
    
    // CHANGE 2: Range should go up to 2e18 because N*M slots 
    // and high B[i] can result in very high levels.
    ll l=1, r=2e18, ans=0; 
    while(l<=r){
        ll mid=l+(r-l)/2;
        if(ch(mid)<=n*m){
            ans=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    cout << ans << endl;

    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…