fork download
  1. #include <bits/stdc++.h>
  2.  
  3. #define FOR(i, a, b) for (int i = a; i <= b; ++i)
  4. #define FORD(i, a, b) for (int i = a; i >= b; --i)
  5. #define ll long long
  6.  
  7. using namespace std;
  8.  
  9. const int N = 305;
  10. const ll inf = 9e18;
  11. int a[N][N], n, m;
  12. ll dp[N][N], cost[N][N];
  13.  
  14. void nhap() {
  15. cin >> n >> m;
  16. FOR(i, 1, n) FOR(j, 1, m) cin >> a[i][j];
  17. }
  18.  
  19. void giai() {
  20. FOR(i, 1, n) {
  21. sort(a[i] + 1, a[i] + 1 + m);
  22. ll sum = 0;
  23. FOR(k, 1, m) {
  24. sum += a[i][k];
  25. cost[i][k] = sum;
  26. }
  27. }
  28.  
  29. memset(dp, 0x3f, sizeof dp);
  30. dp[0][0] = 0;
  31.  
  32. FOR(i, 1, n) FOR(j, i - 1, n) {
  33. FOR(k, 0, m)
  34. dp[i][j + k] = min(dp[i][j + k], dp[i - 1][j] + cost[i][k] + k * k);
  35. }
  36.  
  37. cout << dp[n][n];
  38. }
  39.  
  40. int main() {
  41. ios_base::sync_with_stdio(0);
  42. cin.tie(0); cout.tie(0);
  43.  
  44. #define name "pie"
  45.  
  46. if (fopen(name".inp", "r")) {
  47. freopen(name".inp", "r", stdin);
  48. freopen(name".out", "w", stdout);
  49. }
  50.  
  51. nhap();
  52. giai();
  53. }
  54.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty