Submission #1312164

#TimeUsernameProblemLanguageResultExecution timeMemory
1312164cubedGym Badges (NOI22_gymbadges)C++20
100 / 100
131 ms12488 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define endl '\n'
#define f first
#define s second
#define pb(x) push_back(x)
#define int long long
 
const int MOD = 1e9+7;
const int inf =1e9;
const ll INF = 1e18;
const ll INV2 = 500000004;

class DSU {
    vector<int> parent, size;
public:
    DSU(int n) {
        parent.resize(n+1);
        size.resize(n+1);
        for (int i=0; i<=n; i++) {
            parent[i]=i;
            size[i]=1;
        }
    }

    int find (int node) {
        if (node==parent[node]) return node;
        return parent[node]=find(parent[node]);
    }

    void unite (int u, int v) {
        int pv=find(v);
        int pu=find(u);

        if (pv==pu) return;
        if (size[pu]<size[pv]) swap(pu, pv);

        parent[pv]=pu;
        size[pu]+=size[pv];
    }

    int getsize (int node) {
        return size[find(node)];
    }
};

ll getsqrt(ll x) {
    ll val = sqrtl(x) + 2;
    while (val * val > x) val--;
    return val;
}

bool safe (int i, int j, int n, int m) {
    if (i<0 || j<0 || i>=n || j>=m) return false;
    return true;
}

ll gcd(ll a, ll b) {
    while (b != 0) {
        ll temp=b;
        b=a%b;
        a=temp;
    }
    return a;
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b)*b;
}

vector<int> dx={-1, 0, 1, 0};
vector<int> dy={0, 1, 0, -1};

// SOLUTION STARTS FROM HERE //

struct Gym {
    int x, l;
};

bool comp (const Gym &a, const Gym &b) {
    return (a.l+a.x) < (b.l+b.x);
}

void solve() {
    int n;
    cin>>n;

    vector<Gym> a(n);
    for (int i=0; i<n; i++) cin>>a[i].x;
    for (int i=0; i<n; i++) cin>>a[i].l;

    sort(a.begin(), a.end(), comp);

    priority_queue<int> pq;
    int curr=0;

    for (int i=0; i<n; i++) {
        if (curr <= a[i].l) {
            curr+=a[i].x;
            pq.push(a[i].x);
        } else if (!pq.empty() && pq.top() > a[i].x) {
            if (curr - pq.top() <=a[i].l) {
                curr -= pq.top();
                pq.pop();
                pq.push(a[i].x);
                curr+=a[i].x;
            }
        }
    }

    cout<<pq.size()<<endl;
}

bool multi=false;

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


    //freopen("convention2.in", "r", stdin);
	//freopen("convention2.out", "w", stdout);

    int t=1;
    if (multi) cin>>t;
 
    while (t--) solve();
 
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...