提交时间:2023-12-11 12:56:52

运行 ID: 115395

# include <bits/stdc++.h> using namespace std ; int a [114] , dp[114]; int sum ; int main ( ) { int n ; cin >> n ; for( int i = 1 ; i <= n ; i ++ ) { cin >> a[i] ; sum += a[i] ; } int sum1 = sum / 2 ; for( int i = a[0] ; i <= sum1 ; i ++ ) { dp [i] = a [0] ; } sort ( a + 1 , a + n + 1 ) ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = sum1 ; j >= a [i] ; j -- ) { if ( j - a [i] >= 0 ) { dp [j] = max ( dp [j] , dp [j - a [i]] + a [i] ) ; } } } cout << sum - 2 * dp [sum1] ; return 0 ; }