提交时间:2024-05-27 13:11:09
运行 ID: 149420
# include <bits/stdc++.h> using namespace std ; struct bk { int hgt , wde ; } a [200] ; int f [200] [200] ; bool cmp ( bk a , bk b ) { return a . hgt > b . hgt ; } int main ( ) { int n , k ; cin >> n >> k ; for ( int i = 1 ; i <= n ; i ++ ) { cin >> a [i] . hgt >> a [i] . wde ; } sort ( a + 1 , a + n + 1 , cmp ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 2 ; j <= min ( i , n - k ) ; j ++ ) { f [i] [j] = 0x3f3f3f3f ; for ( int x = j - 1 ; x < i ; x ++ ) { f [i] [j] = min ( f [i] [j] , f [x] [j - 1] + abs ( a [x] . wde - a [i] . wde ) ) ; } } } int ans = f [n] [n - k] ; for ( int i = n - 1 ; i >= n - k ; i -- ) { ans = min ( ans , f [i] [n - k] ) ; } cout << ans << endl ; return 0 ; }