COFO::1443C The Delivery Dilemma

Problem A

Point

Design

Big O(time)

Big O(memory)

Code

// https://beenpow.github.io/
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define r_rep(i,a,b) for(int i=(a);i>(b);i--)
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
typedef long long ll;
using namespace std;
vector<pair<ll,ll> > v;
vector<ll> sum;

int n;
void input(){
    v.clear();
    cin >> n;
    v.resize(n);
    rep(i, 0, n) cin >> v[i].first;
    rep(i, 0, n) cin >> v[i].second;
    sort(v.begin(), v.end());
    
    sum.clear();
    sum.resize(n);
    sum[n-1] = v[n-1].second;
    r_rep(i, n-2, -1){
        sum[i] = v[i].second + sum[i+1];
    }
}

void solve(){
    input();
    ll mn = sum[0];
    rep(i, 0, n){
        ll r = max(v[i].first, i == n-1?0:sum[i+1]);
        if(mn > r)
            mn = r;
    }
    cout << mn << '\n';
}

int main(){
    int tc; cin >> tc;
    while(tc--)
        solve();
    return 0;
}