#include <iostream>
#include <vector>
#include <algorithm>
#define DEBUG 0
int n, m, k;
std::vector<std::vector<long long int>> A;
std::vector<long long int> order;
std::vector<long long int> left_sum;
std::vector<std::pair<long long int, int>> rsums;
std::vector<bool> used;
long long int best_last(int remaining)
{
/* select only `remaining`, so only one stack will be used anyway */
long long int best = 0, s = 0;
for (int i = 0; i < n; ++i) {
if (A[i][0] >= A[i][m-1])
continue;
if (used[i])
continue;
s = 0;
for (int j = 0; j < remaining; ++j)
s += A[i][j];
if (best < s)
best = s;
}
return best;
}
long long int omnomnom()
{
long long int score = 0;
used.resize(n);
order.reserve(n*m);
left_sum.reserve(n*m);
for (int i = 0; i < n; ++i) {
used[i] = false;
if (A[i][0] < A[i][m-1])
continue;
for (int j = 0; j < m; ++j) {
order.push_back(A[i][j]);
}
}
#if DEBUG
for (auto it : order) {
std::cout << it << " ";
}
std::cout << std::endl;
#endif
std::sort(order.begin(), order.end());
std::reverse(order.begin(), order.end());
long long int s = 0;
for (auto it : order) {
left_sum.push_back(s);
s += it;
#if DEBUG
std::cout << s << " ";
#endif
}
left_sum.push_back(s);
#if DEBUG
std::cout << std::endl;
#endif
rsums.reserve(n);
for (int i = 0; i < n; ++i) {
if (A[i][0] >= A[i][m-1])
continue;
s = 0;
for (int j = 0; j < m; ++j)
s += A[i][j];
rsums.push_back(std::make_pair(-s, i));
}
std::sort(rsums.begin(), rsums.end());
int rused = 0;
int lused = 0;
int i = 0;
while (i < k) {
if (k - i < m) {
long long int best = best_last(k - i);
#if DEBUG
std::cout << "using last " << k - i << std::endl;
std::cout << "--- lused " << lused << std::endl;
std::cout << "--- left " << left_sum[lused + k - i] - left_sum[lused] << std::endl;
std::cout << "--- best rev " << best << std::endl;
#endif
if (best < left_sum[lused + k - i] - left_sum[lused]) {
score += order[lused++];
i++;
continue;
} else {
score += best;
break;
}
}
if (-rsums[rused].first >= left_sum[lused + m] - left_sum[lused]) {
#if DEBUG
std::cout << "using rev[" << rsums[rused].second << "] " << rsums[rused].first << std::endl;
#endif
used[rsums[rused].second] = true;
score += -rsums[rused++].first;
i += m;
} else {
#if DEBUG
std::cout << "using " << order[lused] << std::endl;
#endif
score += order[lused++];
i++;
}
}
return score;
}
int main()
{
std::cin >> n >> m >> k;
A.resize(n);
for (int i = 0; i < n; ++i) {
A[i].resize(m);
for (int j = 0; j < m; ++j)
std::cin >> A[i][j];
}
std::cout << omnomnom() << std::endl;
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 | #include <iostream> #include <vector> #include <algorithm> #define DEBUG 0 int n, m, k; std::vector<std::vector<long long int>> A; std::vector<long long int> order; std::vector<long long int> left_sum; std::vector<std::pair<long long int, int>> rsums; std::vector<bool> used; long long int best_last(int remaining) { /* select only `remaining`, so only one stack will be used anyway */ long long int best = 0, s = 0; for (int i = 0; i < n; ++i) { if (A[i][0] >= A[i][m-1]) continue; if (used[i]) continue; s = 0; for (int j = 0; j < remaining; ++j) s += A[i][j]; if (best < s) best = s; } return best; } long long int omnomnom() { long long int score = 0; used.resize(n); order.reserve(n*m); left_sum.reserve(n*m); for (int i = 0; i < n; ++i) { used[i] = false; if (A[i][0] < A[i][m-1]) continue; for (int j = 0; j < m; ++j) { order.push_back(A[i][j]); } } #if DEBUG for (auto it : order) { std::cout << it << " "; } std::cout << std::endl; #endif std::sort(order.begin(), order.end()); std::reverse(order.begin(), order.end()); long long int s = 0; for (auto it : order) { left_sum.push_back(s); s += it; #if DEBUG std::cout << s << " "; #endif } left_sum.push_back(s); #if DEBUG std::cout << std::endl; #endif rsums.reserve(n); for (int i = 0; i < n; ++i) { if (A[i][0] >= A[i][m-1]) continue; s = 0; for (int j = 0; j < m; ++j) s += A[i][j]; rsums.push_back(std::make_pair(-s, i)); } std::sort(rsums.begin(), rsums.end()); int rused = 0; int lused = 0; int i = 0; while (i < k) { if (k - i < m) { long long int best = best_last(k - i); #if DEBUG std::cout << "using last " << k - i << std::endl; std::cout << "--- lused " << lused << std::endl; std::cout << "--- left " << left_sum[lused + k - i] - left_sum[lused] << std::endl; std::cout << "--- best rev " << best << std::endl; #endif if (best < left_sum[lused + k - i] - left_sum[lused]) { score += order[lused++]; i++; continue; } else { score += best; break; } } if (-rsums[rused].first >= left_sum[lused + m] - left_sum[lused]) { #if DEBUG std::cout << "using rev[" << rsums[rused].second << "] " << rsums[rused].first << std::endl; #endif used[rsums[rused].second] = true; score += -rsums[rused++].first; i += m; } else { #if DEBUG std::cout << "using " << order[lused] << std::endl; #endif score += order[lused++]; i++; } } return score; } int main() { std::cin >> n >> m >> k; A.resize(n); for (int i = 0; i < n; ++i) { A[i].resize(m); for (int j = 0; j < m; ++j) std::cin >> A[i][j]; } std::cout << omnomnom() << std::endl; return 0; } |
English