fork download
  1. // ~~ icebear ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define BIT(S, i) (((S) >> (i)) & 1)
  27. #define mp make_pair
  28. #define pb push_back
  29. #define fi first
  30. #define se second
  31. #define all(x) x.begin(), x.end()
  32. #define task "domine"
  33. /*END OF TEMPLATE. ICEBEAR AND THE CAT WILL WIN VOI26 */
  34.  
  35. const int MOD = 1e9 + 7;
  36. const int inf = 1e9 + 27092008;
  37. const ll INF = 1e18 + 27092008;
  38. const int N = 1000 + 5;
  39. int n, m, k, a[1005][10], sum[1005][MASK(4)];
  40. ll f[2][MASK(4)][2709];
  41.  
  42. void init(void) {
  43. cin >> n >> m >> k;
  44. FOR(i, 1, n) REP(j, m) cin >> a[i][j];
  45. }
  46.  
  47. void process(void) {
  48. FOR(i, 1, n) REP(mask, MASK(m)) {
  49. REP(j, m) if (BIT(mask, j)) sum[i][mask] += a[i][j] + a[i + 1][j];
  50. }
  51. ll ans = -INF;
  52. memset(f[1], -0x3f, sizeof f[1]);
  53. ll tmp = f[1][0][0];
  54. f[1][0][0] = 0;
  55. FOR(i, 1, n) {
  56. int cur = i & 1;
  57. int nxt = cur ^ 1;
  58. memset(f[nxt], -0x3f, sizeof f[nxt]);
  59. REP(mask, MASK(m)) FOR(t, 0, k) if (f[cur][mask][t] != tmp) {
  60. REP(j, m - 1) if (!BIT(mask, j) && !BIT(mask, j + 1))
  61. maximize(f[cur][mask | MASK(j) | MASK(j + 1)][t + 1], f[cur][mask][t] + a[i][j] + a[i][j + 1]);
  62.  
  63. REP(mask2, MASK(m)) if ((mask & mask2) == 0)
  64. maximize(f[nxt][mask2][t + __builtin_popcount(mask2)], f[cur][mask][t] + sum[i][mask2]);
  65. maximize(ans, f[cur][mask][k]);
  66. }
  67. }
  68.  
  69. cout << ans << '\n';
  70. }
  71.  
  72. int main() {
  73. ios_base::sync_with_stdio(0);
  74. cin.tie(0); cout.tie(0);
  75. if (fopen(task".inp", "r")) {
  76. freopen(task".inp", "r", stdin);
  77. freopen(task".out", "w", stdout);
  78. }
  79. int tc = 1;
  80. // cin >> tc;
  81. while(tc--) {
  82. init();
  83. process();
  84. }
  85. return 0;
  86. }
  87.  
  88.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
-1000000000027091968