#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())
template<class A, class B> auto &operator<<(ostream &o, pair<A, B> p) {
return o << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) {
o << '{'; int i = 0; for (auto e : x) o << (", ")+2*!i++ << e;
return o << '}';
}
#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n';
#else
#define debug(...) {}
#endif
constexpr LL INF = 1'000'000'000'000'000'000;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
vector<LL> revs, a(m);
vector<vector<pair<LL, int>>> opts(m);
vector<vector<LL>> sum(n, vector<LL>(m + 1));
int incs = 0;
REP(i, n) {
bool inc = true;
LL prev = -1;
REP(j, m) {
cin >> a[j];
sum[i][j + 1] = sum[i][j] + a[j];
if (a[j] < prev)
inc = false;
prev = a[j];
}
if (inc)
incs++;
LL total = 0;
REP(j, m) {
total += a[j];
if (inc)
opts[(j + 1) % m].emplace_back(total, i);
else
revs.emplace_back(a[j]);
}
}
REP(i, m)
sort(opts[i].begin(), opts[i].end());
sort(revs.begin(), revs.end());
vector<LL> pref_revs(k + 1);
FOR(i, 1, k) {
pref_revs[i] = pref_revs[i - 1];
if (!revs.empty()) {
pref_revs[i] += revs.back();
revs.pop_back();
}
}
int idx = 0;
LL ans = pref_revs[k];
LL ensured_score = 0;
vector<bool> removed(n);
vector<LL> best_swap(m, -INF);
LL first_full_not_taken = -INF;
FOR(normal_taken, 1, min(incs * m, k)) {
idx++;
idx %= m;
while (removed[opts[idx].back().second])
opts[idx].pop_back();
auto [score, i] = opts[idx].back();
if (idx == 0) {
removed[i] = true;
opts[0].pop_back();
ensured_score += score;
FOR(j, 1, m - 1)
best_swap[j] = max(best_swap[j], sum[i][j] - score);
score = 0;
if (!opts[0].empty())
first_full_not_taken = opts[0].back().first;
}
LL rev_sum = pref_revs[k - normal_taken];
LL current = rev_sum + ensured_score + score;
LL replaced = rev_sum + ensured_score + first_full_not_taken + best_swap[idx];
ans = max(ans, max(current, replaced));
}
cout << ans << '\n';
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 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto &operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for (auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'; #else #define debug(...) {} #endif constexpr LL INF = 1'000'000'000'000'000'000; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; vector<LL> revs, a(m); vector<vector<pair<LL, int>>> opts(m); vector<vector<LL>> sum(n, vector<LL>(m + 1)); int incs = 0; REP(i, n) { bool inc = true; LL prev = -1; REP(j, m) { cin >> a[j]; sum[i][j + 1] = sum[i][j] + a[j]; if (a[j] < prev) inc = false; prev = a[j]; } if (inc) incs++; LL total = 0; REP(j, m) { total += a[j]; if (inc) opts[(j + 1) % m].emplace_back(total, i); else revs.emplace_back(a[j]); } } REP(i, m) sort(opts[i].begin(), opts[i].end()); sort(revs.begin(), revs.end()); vector<LL> pref_revs(k + 1); FOR(i, 1, k) { pref_revs[i] = pref_revs[i - 1]; if (!revs.empty()) { pref_revs[i] += revs.back(); revs.pop_back(); } } int idx = 0; LL ans = pref_revs[k]; LL ensured_score = 0; vector<bool> removed(n); vector<LL> best_swap(m, -INF); LL first_full_not_taken = -INF; FOR(normal_taken, 1, min(incs * m, k)) { idx++; idx %= m; while (removed[opts[idx].back().second]) opts[idx].pop_back(); auto [score, i] = opts[idx].back(); if (idx == 0) { removed[i] = true; opts[0].pop_back(); ensured_score += score; FOR(j, 1, m - 1) best_swap[j] = max(best_swap[j], sum[i][j] - score); score = 0; if (!opts[0].empty()) first_full_not_taken = opts[0].back().first; } LL rev_sum = pref_revs[k - normal_taken]; LL current = rev_sum + ensured_score + score; LL replaced = rev_sum + ensured_score + first_full_not_taken + best_swap[idx]; ans = max(ans, max(current, replaced)); } cout << ans << '\n'; return 0; } |
English