#include <bits/stdc++.h>
using namespace std;
bool isAscending(const vector<long long int> &stack) {
for (unsigned int i = 1; i < stack.size(); i++) {
if (stack[i] > stack[i - 1]) {
return true;
}
}
return false;
}
struct Stacks {
const int n;
const int m;
const int k;
const map<long long, vector<vector<long long>>> stackMap;
const vector<long long> descendingSingleValues;
const vector<long long> mapKeysVector;
const int F;
long long resultValue = 0;
int freeSpace = 0;
Stacks(int n, int m, int k,
const map<long long, vector<vector<long long>>> &stackMap,
const vector<long long> &descendingValues) :
n(n), m(m), k(k), stackMap(stackMap), descendingSingleValues(
descendingValues), mapKeysVector([&]() {
vector<long long> keys;
keys.reserve(stackMap.size());
for (auto it = stackMap.rbegin(); it != stackMap.rend(); ++it) {
keys.push_back(it->first);
}
return keys;
}()), F(descendingSingleValues.size()) {
}
void process() {
if (stackMap.size() > 0) {
processWithMap();
} else {
resultValue = 0;
for (int i = 0; i < k; i++) {
resultValue += descendingSingleValues[i];
}
}
}
void processWithMap() {
resultValue = 0;
freeSpace = k;
int singleIndex = 0;
unsigned int stacksIndex = 0;
while (freeSpace > 0) {
const long long int stackKeyValue =
stacksIndex >= mapKeysVector.size() ?
0 : mapKeysVector[stacksIndex];
if (freeSpace == m) {
fillTail(stacksIndex, singleIndex);
break;
} else if (singleIndex < F
&& descendingSingleValues[singleIndex] * m
> stackKeyValue) {
resultValue += descendingSingleValues[singleIndex];
freeSpace--;
singleIndex++;
} else {
int stacksCount = stackMap.at(stackKeyValue).size();
const int multiplier = min(freeSpace / m, stacksCount);
if (multiplier > 0) {
stacksCount -= multiplier;
resultValue += multiplier * stackKeyValue;
freeSpace -= multiplier * m;
}
if (freeSpace >= m) {
stacksIndex++;
} else {
if (stacksCount == 0) {
fillTail(stacksIndex + 1, singleIndex);
} else {
fillTail(stacksIndex, singleIndex);
}
break;
}
}
}
}
void fillTail(int stacksIndex, int singleIndex) {
vector<long long> maxStackSubSum(m + 1, 0);
const int K = mapKeysVector.size();
long long int partialSum = 0;
for (int i = stacksIndex; i < K; i++) {
const long long int stackKeyValue = mapKeysVector[stacksIndex];
const vector<vector<long long>> &stacks = stackMap.at(
stackKeyValue);
for (const vector<long long> &stackVector : stacks) {
partialSum = 0;
for (int j = 1; j <= m; j++) {
partialSum += stackVector[j - 1];
if (partialSum > maxStackSubSum[j]) {
maxStackSubSum[j] = partialSum;
}
}
}
}
const int maxSingleLength = min(F - singleIndex, freeSpace);
vector<long long> maxSingleSubSum(maxSingleLength + 1, 0);
partialSum = 0;
for (int j = 0; j < maxSingleLength; j++) {
partialSum += descendingSingleValues[singleIndex + j];
maxSingleSubSum[j + 1] = partialSum;
}
long long int maxTail = 0;
for (int i = 0; i <= maxSingleLength; i++) {
const long long int current = maxSingleSubSum[i]
+ maxStackSubSum[freeSpace - i];
if (current > maxTail) {
maxTail = current;
}
}
resultValue += maxTail;
freeSpace = 0;
}
};
struct Node {
long long last;
long long secondLast;
int index;
bool operator<(const Node &other) const {
if (last != other.last)
return last > other.last; // min-heap behavior
return secondLast > other.secondLast;
}
};
void removeLowest(vector<vector<long long>> &stacks, int kk) {
priority_queue<Node> pq;
for (int i = 0; i < (int) stacks.size(); ++i) {
if (!stacks[i].empty()) {
long long last = stacks[i].back();
long long secondLast =
(stacks[i].size() >= 2) ?
stacks[i][stacks[i].size() - 2] : (long long) 1e18; // treat missing as very large
pq.push( { last, secondLast, i });
}
}
while (kk-- > 0 && !pq.empty()) {
Node top = pq.top();
pq.pop();
int i = top.index;
stacks[i].pop_back();
if (!stacks[i].empty()) {
long long last = stacks[i].back();
long long secondLast =
(stacks[i].size() >= 2) ?
stacks[i][stacks[i].size() - 2] : (long long) 1e18;
pq.push( { last, secondLast, i });
}
}
}
long long sumStacks(const vector<vector<long long>> &stacks) {
long long total = 0;
for (const auto &vec : stacks) {
for (long long x : vec) {
total += x;
}
}
return total;
}
int main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
int n, m, k;
map<long long int, vector<vector<long long int>>> stackMap;
vector<long long int> descendingValues;
long long int a;
cin >> n;
cin >> m;
cin >> k;
vector<vector<long long int>> stacks(n);
long long int shortMax = 0;
bool c = k <= m;
for (int i = 0; i < n; i++) {
vector<long long int> stack(m);
long long int stackValue = 0;
for (int j = 1; j <= m; j++) {
cin >> a;
stack[j - 1] = a;
stackValue += a;
if (c && j == k && stackValue > shortMax) {
shortMax = stackValue;
}
}
stacks[i] = stack;
if (isAscending(stack)) {
stackMap[stackValue].push_back(stack);
} else {
descendingValues.insert(descendingValues.end(),
make_move_iterator(stack.begin()),
make_move_iterator(stack.end()));
}
}
sort(descendingValues.begin(), descendingValues.end(), greater<int>());
Stacks s(n, m, k, stackMap, descendingValues);
s.process();
removeLowest(stacks, m * n - k);
long long int sum = sumStacks(stacks);
long long int r = max(sum, max(shortMax, s.resultValue));
cout << r << endl;
}
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | #include <bits/stdc++.h> using namespace std; bool isAscending(const vector<long long int> &stack) { for (unsigned int i = 1; i < stack.size(); i++) { if (stack[i] > stack[i - 1]) { return true; } } return false; } struct Stacks { const int n; const int m; const int k; const map<long long, vector<vector<long long>>> stackMap; const vector<long long> descendingSingleValues; const vector<long long> mapKeysVector; const int F; long long resultValue = 0; int freeSpace = 0; Stacks(int n, int m, int k, const map<long long, vector<vector<long long>>> &stackMap, const vector<long long> &descendingValues) : n(n), m(m), k(k), stackMap(stackMap), descendingSingleValues( descendingValues), mapKeysVector([&]() { vector<long long> keys; keys.reserve(stackMap.size()); for (auto it = stackMap.rbegin(); it != stackMap.rend(); ++it) { keys.push_back(it->first); } return keys; }()), F(descendingSingleValues.size()) { } void process() { if (stackMap.size() > 0) { processWithMap(); } else { resultValue = 0; for (int i = 0; i < k; i++) { resultValue += descendingSingleValues[i]; } } } void processWithMap() { resultValue = 0; freeSpace = k; int singleIndex = 0; unsigned int stacksIndex = 0; while (freeSpace > 0) { const long long int stackKeyValue = stacksIndex >= mapKeysVector.size() ? 0 : mapKeysVector[stacksIndex]; if (freeSpace == m) { fillTail(stacksIndex, singleIndex); break; } else if (singleIndex < F && descendingSingleValues[singleIndex] * m > stackKeyValue) { resultValue += descendingSingleValues[singleIndex]; freeSpace--; singleIndex++; } else { int stacksCount = stackMap.at(stackKeyValue).size(); const int multiplier = min(freeSpace / m, stacksCount); if (multiplier > 0) { stacksCount -= multiplier; resultValue += multiplier * stackKeyValue; freeSpace -= multiplier * m; } if (freeSpace >= m) { stacksIndex++; } else { if (stacksCount == 0) { fillTail(stacksIndex + 1, singleIndex); } else { fillTail(stacksIndex, singleIndex); } break; } } } } void fillTail(int stacksIndex, int singleIndex) { vector<long long> maxStackSubSum(m + 1, 0); const int K = mapKeysVector.size(); long long int partialSum = 0; for (int i = stacksIndex; i < K; i++) { const long long int stackKeyValue = mapKeysVector[stacksIndex]; const vector<vector<long long>> &stacks = stackMap.at( stackKeyValue); for (const vector<long long> &stackVector : stacks) { partialSum = 0; for (int j = 1; j <= m; j++) { partialSum += stackVector[j - 1]; if (partialSum > maxStackSubSum[j]) { maxStackSubSum[j] = partialSum; } } } } const int maxSingleLength = min(F - singleIndex, freeSpace); vector<long long> maxSingleSubSum(maxSingleLength + 1, 0); partialSum = 0; for (int j = 0; j < maxSingleLength; j++) { partialSum += descendingSingleValues[singleIndex + j]; maxSingleSubSum[j + 1] = partialSum; } long long int maxTail = 0; for (int i = 0; i <= maxSingleLength; i++) { const long long int current = maxSingleSubSum[i] + maxStackSubSum[freeSpace - i]; if (current > maxTail) { maxTail = current; } } resultValue += maxTail; freeSpace = 0; } }; struct Node { long long last; long long secondLast; int index; bool operator<(const Node &other) const { if (last != other.last) return last > other.last; // min-heap behavior return secondLast > other.secondLast; } }; void removeLowest(vector<vector<long long>> &stacks, int kk) { priority_queue<Node> pq; for (int i = 0; i < (int) stacks.size(); ++i) { if (!stacks[i].empty()) { long long last = stacks[i].back(); long long secondLast = (stacks[i].size() >= 2) ? stacks[i][stacks[i].size() - 2] : (long long) 1e18; // treat missing as very large pq.push( { last, secondLast, i }); } } while (kk-- > 0 && !pq.empty()) { Node top = pq.top(); pq.pop(); int i = top.index; stacks[i].pop_back(); if (!stacks[i].empty()) { long long last = stacks[i].back(); long long secondLast = (stacks[i].size() >= 2) ? stacks[i][stacks[i].size() - 2] : (long long) 1e18; pq.push( { last, secondLast, i }); } } } long long sumStacks(const vector<vector<long long>> &stacks) { long long total = 0; for (const auto &vec : stacks) { for (long long x : vec) { total += x; } } return total; } int main() { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int n, m, k; map<long long int, vector<vector<long long int>>> stackMap; vector<long long int> descendingValues; long long int a; cin >> n; cin >> m; cin >> k; vector<vector<long long int>> stacks(n); long long int shortMax = 0; bool c = k <= m; for (int i = 0; i < n; i++) { vector<long long int> stack(m); long long int stackValue = 0; for (int j = 1; j <= m; j++) { cin >> a; stack[j - 1] = a; stackValue += a; if (c && j == k && stackValue > shortMax) { shortMax = stackValue; } } stacks[i] = stack; if (isAscending(stack)) { stackMap[stackValue].push_back(stack); } else { descendingValues.insert(descendingValues.end(), make_move_iterator(stack.begin()), make_move_iterator(stack.end())); } } sort(descendingValues.begin(), descendingValues.end(), greater<int>()); Stacks s(n, m, k, stackMap, descendingValues); s.process(); removeLowest(stacks, m * n - k); long long int sum = sumStacks(stacks); long long int r = max(sum, max(shortMax, s.resultValue)); cout << r << endl; } |
English