이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define N 505
#define M 100005
using namespace std;
int dp[N][M];
int arr[N];
void solve(){
for(int i=0;i<N;i++){
for(int j = 0;j<M;j++){
dp[i][j] = -1e9;
}
}
dp[0][0] = 0;
int n;
cin >> n;
for(int i=1;i<=n;i++){
cin >> arr[i];
}
sort(arr+1,arr+n+1);
int sum = 0;
int ans = -1e9;
for(int i=1;i<=n;i++){
for(int j=0;j<M;j++){
dp[i][j] = dp[i-1][j];
}
for(int j=0;j<M-arr[i];j++){
dp[i][j] = max(dp[i][j],min({dp[i-1][j],dp[i-1][arr[i]],dp[i-1][j+arr[i]]})+arr[i]);
}
for(int j=0;j<M-arr[i];j++){
dp[i][j+arr[i]] = max(dp[i][j+arr[i]],dp[i-1][j]);
if(dp[i][j] >= 0){
//cout << i << " " << j << " "<< dp[i][j] << endl;
}
}
sum+=arr[i];
}
for(int j=0;j<M;j++){
ans = max(ans,dp[n][j]);
}
cout << sum-ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# | 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... |
# | 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... |