Submission #673267

# Submission time Handle Problem Language Result Execution time Memory
673267 2022-12-20T04:12:08 Z Cutebol Schools (IZhO13_school) C++17
100 / 100
159 ms 20464 KB
#include <bits/stdc++.h>
 
using namespace std;
 
void fopn(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
#define Scaramouche ios_base::sync_with_stdio(0) ; cin.tie(0) ; cout.tie(0);
#define int long long
#define itn int
#define endl "\n"
#define ff first
#define ss second
   
const int N = 3e5 + 5 ;
const int mod = 1e9 + 7 ;
const int inf = 1e12 ;

int n , k , m , sum ;
int	pref[N] , suf[N] ;
int a[N] , b[N] ;
multiset <int> st ;

void solve(){
	
	cin >> n >> m >> k ;
	for ( int i = 0 ; i < n ; i ++ ) cin >> a[i] >> b[i] ;
	vector < pair <int , pair <int , int> > > vec ;
	for ( int i = 0 ; i < n ; i ++ )
		vec.push_back({a[i]-b[i],{a[i],b[i]}}) ;
	sort ( vec.rbegin() , vec.rend() ) ;
	for ( int i = 0 ; i < m ; i ++ ){
		st.insert(vec[i].ss.ff) ;
		sum += vec[i].ss.ff ;
	}
	pref[m-1] = sum ;
	if (m)
	for ( int i = m ; i < n ; i ++ ){
		if ( vec[i].ss.ff > *st.begin() ){
			sum -= *st.begin() ;
			st.erase(st.begin()) ;
			st.insert(vec[i].ss.ff) ;
			sum += vec[i].ss.ff ;
		}
		pref[i] = sum ;
	}
	st.clear() ;
	sum = 0 ;
	for ( int i = n - 1 ; i >= n - k ; i -- ){
		st.insert(vec[i].ss.ss) ;
		sum += vec[i].ss.ss ;
	}
	suf[n-k] = sum ;
	if (k)
	for ( int i = n - k - 1 ; i >= 0 ; i -- ){
		if ( vec[i].ss.ss > *st.begin() ){
			sum -= *st.begin() ;
			st.erase(st.begin()) ;
			st.insert(vec[i].ss.ss) ;
			sum += vec[i].ss.ss ;
		}
		suf[i] = sum ;
	}
	int ans = 0 ;
	for ( int i = m-1 ; i < n ; i ++ ) ans = max ( ans , pref[i]+suf[i+1] ) ;
	cout << ans << endl ;
//	for ( int i = 0 ; i < n ; i ++ ) cerr << vec[i].ff << ' ' << vec[i].ss.ff << ' ' << vec[i].ss.ss << endl ;
//	for ( int i = 0 ; i < n ; i ++ ) cerr << pref[i] << ' ' << suf[i] << endl ;
}
 
signed main(){
//  fopn("blocks") ;
    Scaramouche ;
    int t = 1 ;
//    	cin >> t ;
    while ( t -- ) solve() ; 
}

Compilation message

school.cpp: In function 'void fopn(std::string)':
school.cpp:5:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | void fopn(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
school.cpp:5:72: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | void fopn(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 2 ms 660 KB Output is correct
8 Correct 2 ms 788 KB Output is correct
9 Correct 2 ms 660 KB Output is correct
10 Correct 2 ms 660 KB Output is correct
11 Correct 2 ms 660 KB Output is correct
12 Correct 2 ms 660 KB Output is correct
13 Correct 17 ms 3520 KB Output is correct
14 Correct 29 ms 5380 KB Output is correct
15 Correct 47 ms 9228 KB Output is correct
16 Correct 105 ms 16124 KB Output is correct
17 Correct 122 ms 15788 KB Output is correct
18 Correct 124 ms 16520 KB Output is correct
19 Correct 138 ms 17776 KB Output is correct
20 Correct 159 ms 20464 KB Output is correct