This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std ;
const int MAX = 2005 ;
long long tmp1[MAX] , tmp2[MAX] , id[MAX] , arr[MAX] , arr2[MAX];
long long dp[MAX][MAX] ;
int n , m ;
long long solve(int idx , int rem)
{
if(idx == n && rem != 0)
return -1e17 ;
if(rem == 0 || idx == n)
return 0 ;
long long &ret = dp[idx][rem] ;
if(ret != -1)
return ret ;
long long choice1 = solve(idx+1 , rem) ;
long long choice2 = solve(idx+1 , rem-1) + arr[idx] ;
if(rem != m)
choice2 -= id[arr2[idx]] ;
if(rem != 1)
choice2 += id[arr2[idx]] ;
if(rem == 1)
choice2 -= id[arr2[idx]] ;
if(rem == m)
choice2 += id[arr2[idx]] ;
ret = max(choice1 , choice2) ;
return ret ;
}
vector< pair<long long , int> >cmprs ;
void compress()
{
sort(cmprs.begin() , cmprs.end()) ;
for(int i = 0 ; i < n ; ++i)
{
arr[i] = tmp1[cmprs[i].second] ;
arr2[i] = i ;
id[i] = cmprs[i].first;
}
return ;
}
int main()
{
memset(dp , -1 , sizeof(dp)) ;
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>m ;
for(int i = 0 ; i < n ; ++i)
{
cin>>tmp1[i]>>tmp2[i] ;
cmprs.push_back({tmp2[i] , i}) ;
}
compress() ;
return cout<<solve(0 , m)<<"\n" , 0 ;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |