#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;
using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) {
*(int *)0 = 0;
});
# define DTP(x, y) \
auto operator<<(auto &o, auto a)->decltype(y, o) { \
o << "("; \
x; \
return o << ")"; \
}
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) {
((cerr << x << ", "), ...) << '\n';
}
# define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
# define deb(...) 0
#endif
vector<ll> calc_non_inc_profit(vector<vector<ll> > non_increasing) {
int n = sz(non_increasing);
if (n == 0) {
return {0};
}
int m = sz(non_increasing[0]);
priority_queue<pair<ll, int> > q;
rep(i, n) {
non_increasing[i].push_back(0);
reverse(all(non_increasing[i]));
q.push({non_increasing[i].back(), i});
}
vector<ll> profit(1);
ll sum = 0;
rep(i, n * m) {
auto [val, idx] = q.top();
q.pop();
sum += val;
profit.push_back(sum);
non_increasing[idx].pop_back();
q.push({non_increasing[idx].back(), idx});
}
return profit;
}
vector<ll> calc_non_dec_profit(vector<vector<ll> > non_decreasing) {
int n = sz(non_decreasing);
if (n == 0) {
return {0};
}
int m = sz(non_decreasing[0]);
rep(i, n) {
ll sum = accumulate(all(non_decreasing[i]), 0LL);
non_decreasing[i].insert(non_decreasing[i].begin(), sum);
}
sort(all(non_decreasing), [](auto &a, auto &b) {
return a[0] > b[0];
});
non_decreasing.insert(non_decreasing.begin(), vector<ll>(m + 1));
vector<ll> total_sum(n + 1);
const ll inf = 1e18;
vector<vector<ll> > min_partial_sum(n + 1, vector<ll>(m + 1, -inf));
vector<vector<ll> > max_partial_sum(n + 2, vector<ll>(m + 1));
fwd(i, 1, n + 1) {
total_sum[i] = total_sum[i - 1] + non_decreasing[i][0];
ll partial_sum = 0;
for (int j = m; j >= 1; j--) {
partial_sum += non_decreasing[i][j];
min_partial_sum[i][j] =
max(-partial_sum, min_partial_sum[i - 1][j]);
}
}
for (int i = n; i >= 1; i--) {
ll partial_sum = 0;
fwd(j, 1, m + 1) {
partial_sum += non_decreasing[i][j];
max_partial_sum[i][j] = max(partial_sum, max_partial_sum[i + 1][j]);
}
}
vector<ll> profit(1);
auto calc_profit = [&](int take) {
int take_whole = take / m;
int take_partial = take % m;
if (take_partial == 0) {
return total_sum[take_whole];
}
ll case_1 = total_sum[take_whole + 1] +
min_partial_sum[take_whole + 1][take_partial + 1];
ll case_2 = total_sum[take_whole] +
max_partial_sum[take_whole + 1][take_partial];
return max(case_1, case_2);
};
fwd(i, 1, n * m + 1) {
profit.push_back(calc_profit(i));
}
return profit;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
int n, m, k;
cin >> n >> m >> k;
vector<vector<ll> > non_increasing, non_decreasing;
rep(i, n) {
vector<ll> nalesniki(m);
rep(j, m) cin >> nalesniki[j];
bool non_inc = true;
fwd(j, 1, m) {
if (nalesniki[j] > nalesniki[j - 1]) {
non_inc = false;
break;
}
}
if (non_inc) {
non_increasing.pb(nalesniki);
} else {
non_decreasing.pb(nalesniki);
}
}
vector<ll> non_inc_profit = calc_non_inc_profit(non_increasing);
vector<ll> non_dec_profit = calc_non_dec_profit(non_decreasing);
ll ans = 0;
rep(i, k + 1) {
int take_non_inc = i;
int take_non_dec = k - i;
if (take_non_inc < sz(non_inc_profit) &&
take_non_dec < sz(non_dec_profit)) {
ans = max(
ans,
non_inc_profit[take_non_inc] + non_dec_profit[take_non_dec]);
}
}
cout << ans << '\n';
#ifdef LOCF
cout.flush();
cerr << "- - - - - - - - -\n";
(void)!system(
"grep VmPeak /proc/$PPID/status | sed s/....kB/\' MB\'/1 >&2"); // 4x.kB
// ....kB
#endif
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(size(X)) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); # define DTP(x, y) \ auto operator<<(auto &o, auto a)->decltype(y, o) { \ o << "("; \ x; \ return o << ")"; \ } DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } # define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else # define deb(...) 0 #endif vector<ll> calc_non_inc_profit(vector<vector<ll> > non_increasing) { int n = sz(non_increasing); if (n == 0) { return {0}; } int m = sz(non_increasing[0]); priority_queue<pair<ll, int> > q; rep(i, n) { non_increasing[i].push_back(0); reverse(all(non_increasing[i])); q.push({non_increasing[i].back(), i}); } vector<ll> profit(1); ll sum = 0; rep(i, n * m) { auto [val, idx] = q.top(); q.pop(); sum += val; profit.push_back(sum); non_increasing[idx].pop_back(); q.push({non_increasing[idx].back(), idx}); } return profit; } vector<ll> calc_non_dec_profit(vector<vector<ll> > non_decreasing) { int n = sz(non_decreasing); if (n == 0) { return {0}; } int m = sz(non_decreasing[0]); rep(i, n) { ll sum = accumulate(all(non_decreasing[i]), 0LL); non_decreasing[i].insert(non_decreasing[i].begin(), sum); } sort(all(non_decreasing), [](auto &a, auto &b) { return a[0] > b[0]; }); non_decreasing.insert(non_decreasing.begin(), vector<ll>(m + 1)); vector<ll> total_sum(n + 1); const ll inf = 1e18; vector<vector<ll> > min_partial_sum(n + 1, vector<ll>(m + 1, -inf)); vector<vector<ll> > max_partial_sum(n + 2, vector<ll>(m + 1)); fwd(i, 1, n + 1) { total_sum[i] = total_sum[i - 1] + non_decreasing[i][0]; ll partial_sum = 0; for (int j = m; j >= 1; j--) { partial_sum += non_decreasing[i][j]; min_partial_sum[i][j] = max(-partial_sum, min_partial_sum[i - 1][j]); } } for (int i = n; i >= 1; i--) { ll partial_sum = 0; fwd(j, 1, m + 1) { partial_sum += non_decreasing[i][j]; max_partial_sum[i][j] = max(partial_sum, max_partial_sum[i + 1][j]); } } vector<ll> profit(1); auto calc_profit = [&](int take) { int take_whole = take / m; int take_partial = take % m; if (take_partial == 0) { return total_sum[take_whole]; } ll case_1 = total_sum[take_whole + 1] + min_partial_sum[take_whole + 1][take_partial + 1]; ll case_2 = total_sum[take_whole] + max_partial_sum[take_whole + 1][take_partial]; return max(case_1, case_2); }; fwd(i, 1, n * m + 1) { profit.push_back(calc_profit(i)); } return profit; } int32_t main() { cin.tie(0)->sync_with_stdio(0); int n, m, k; cin >> n >> m >> k; vector<vector<ll> > non_increasing, non_decreasing; rep(i, n) { vector<ll> nalesniki(m); rep(j, m) cin >> nalesniki[j]; bool non_inc = true; fwd(j, 1, m) { if (nalesniki[j] > nalesniki[j - 1]) { non_inc = false; break; } } if (non_inc) { non_increasing.pb(nalesniki); } else { non_decreasing.pb(nalesniki); } } vector<ll> non_inc_profit = calc_non_inc_profit(non_increasing); vector<ll> non_dec_profit = calc_non_dec_profit(non_decreasing); ll ans = 0; rep(i, k + 1) { int take_non_inc = i; int take_non_dec = k - i; if (take_non_inc < sz(non_inc_profit) && take_non_dec < sz(non_dec_profit)) { ans = max( ans, non_inc_profit[take_non_inc] + non_dec_profit[take_non_dec]); } } cout << ans << '\n'; #ifdef LOCF cout.flush(); cerr << "- - - - - - - - -\n"; (void)!system( "grep VmPeak /proc/$PPID/status | sed s/....kB/\' MB\'/1 >&2"); // 4x.kB // ....kB #endif return 0; } |
English