#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define int long long
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<bool> vb;
#define FOR(i,a) for(int i=0;i<(a);i++)
#define FORE(i,a,b) for(int i=(a);i<(b);i++)
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define pb push_back
#define sp <<" "<<
#define cont(x) for(auto el:x) cout<<el<<' ';cout<<endl;
#define contp(x) for(auto el:x) cout<<el.fi<<'-'<<el.se<<' ';cout<<endl;
#define DEBUG(x) cout<<#x sp x<<endl;
#define carp(x,y) ((x%MOD)*(y%MOD))%MOD
#define topla(x,y) ((x%MOD)+(y%MOD))%MOD
#define mid (l+r)/2
const int MAXN=2e3+5;
const int INF=1e18;
int n,k;
vii a;
int c;
struct Segt{
    //segment tree için
    vi t, lazy;
    //fracturing search için
    vi cikar; //hangileri çıkacak ya da çıkmayacak
    //1--> kesin çıkar
    //0--> free
    set<int> vec; //hangi elemanlar var
    int bas; //ilk "bas" elemanı kesin alacam
    //answerı bulmak için
    vi tut;
    int cost;
    int sz;
    void build(int nd=1,int l=1,int r=n){
        if(l==r){
            t[nd]=r;
            return;
        }
        build(nd*2,l,mid);
        build(nd*2+1,mid+1,r);
        t[nd]=min(t[nd*2],t[nd*2+1]);
    }
    void push(int nd,int l,int r){
        if(lazy[nd]==0) return;
        t[nd*2]+=lazy[nd];
        t[nd*2+1]+=lazy[nd];
        lazy[nd*2]=lazy[nd*2+1]=lazy[nd];
        lazy[nd]=0;
    }
    int query(int ql,int qr,int nd=1,int l=1,int r=n){
        if(l>r or ql>r or r<ql) return INF;
        push(nd,l,r);
        if(ql<=l and r<=qr) return t[nd];
    
        auto bir=query(ql,qr,nd*2,l,mid);
        auto iki=query(ql,qr,nd*2+1,mid+1,r);
        return min(bir,iki);
    }
    void update(int ql,int qr,int val,int nd=1,int l=1,int r=n){
        if(l>r or ql>r or r<ql) return;
        push(nd,l,r);
        if(ql<=l and r<=qr){
            t[nd]+=val;
            lazy[nd]+=val;
            return;
        }
        update(ql,qr,val,nd*2,l,mid);
        update(ql,qr,val,nd*2+1,mid+1,r);
        t[nd]=min(t[nd*2],t[nd*2+1]);
    }
    int query2(int pos,int nd=1,int l=1,int r=n){
        if(l>r or l>pos) return 0;
        push(nd,l,r);
        if(l==r){
            if(t[nd]!=0) return 0;
            else return l;
        }
        if(pos<=mid){
            return query2(pos,nd*2,l,mid);
        }
        else{
            if(t[nd*2+1]==0LL){
                int deg=query2(pos,nd*2+1,mid+1,r);
                if(deg>pos) return query2(pos,nd*2,l,mid);
                else return deg;
            }
            else return query2(pos,nd*2,l,mid);
        }
    }
    void calc(){
        vector<vii> ss(n+1);
        vb used(n+1,false);
        for(auto el:vec) used[el]=true;
        FORE(i,bas+1,n+1){
            if(cikar[i]==0 and !used[i]){
                //if free and not included
                ss[a[i].se].pb({a[i].fi,i});
            }
        }
        set<pii> cur;
        for(int i=n;i>0;i--){
            //i'den büyük min costlu eleman
            for(auto el:ss[i]) cur.insert(el);
            if(cur.empty()) tut[i]=-1;
            else tut[i]=cur.begin()->se;
        }
    }
    Segt(vi cikar,int bas) : cikar(cikar), bas(bas) {
        t=vi (4*n+100);
        lazy= vi(4*n+100);
        tut=vi(n+1);
        sz=0;
        cost=0;
    
        build();
        FORE(i,1,n+1){
            if(cikar[i]==0 and query(a[i].se,n)>=1){
                //alabilirim bu ürünü
                update(a[i].se,n,-1);
                sz++;
                cost+=a[i].fi;
                vec.insert(i);
            }
        }
        calc();
    }
};
bool operator<(const Segt&a , const Segt &b){
    if(a.sz==b.sz) return a.cost>b.cost;
    return a.sz<b.sz;
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k;
    a.resize(n+1);
    FORE(i,1,n+1) cin>>a[i].fi>>a[i].se;
    
    //weightlere göre sortla a'yı
    sort(all(a));
    priority_queue<Segt> pq;
    vi temp(n+1,0);
    pq.push(Segt(temp,0));
    auto cur=pq.top();
    cout<<cur.sz sp cur.cost<<endl;
}   
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |