题意:


思路:





#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 305;
int f[N][N];
int n,m,sum[N][N],a[N][N];
int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            scanf("%d",&a[i][j]);
        }
        sort(a[i] + 1,a[i] + 1 + m);
    }
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            sum[i][j] = sum[i][j-1] + a[i][j];
        }
    }
    memset(f,0x3f,sizeof(f));
    f[0][0] = 0;
    for(int i = 1;i <= n;i++){
        for(int j = i;j <= min(n,i*m);j++){
            for(int k = 0;k <= min(m,j);k++){
                f[i][j] = min(f[i][j],f[i-1][j-k] + sum[i][k] + k*k);
            }
        }
    }
    printf("%d\n",f[n][n]);
    return 0;
}