#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
void solve() {
int n,m,k;
cin >> n >> m >> k;
vector<ll> dec;
// pairs (sum, vector)
vector<pair<ll, vector<ll>>> inc;
for(int i = 1; i <= n; i++) {
vector<ll> V;
ll sum = 0;
for(int j = 1; j <= m; j++) {
ll x;
cin >> x;
V.pb(x);
sum += x;
}
// descreasing, we can take biggest ones always
if(V[0] >= V.back()) {
dec.insert(dec.end(), all(V));
} else {
ll p = 0;
for (auto v: V) p += v;
inc.pb({p, move(V)});
}
}
sort(all(dec));
sort(all(inc));
reverse(all(inc));
/// aaaaanother not nice implementation :(
/// Anyway, on the left side we add 'start' elements right away.
/// On the right we need X = (k - 'start') / m full stacks and then it decreases.
/// In general we take X biggest and best remaining part from the right.
/// Or some subset of X - 1 from the X biggest and remaining from one of them.
/// Bro I hate this shit.
/// Let's handle this shit separately so we don't need to care about exact bounds.
vector<ll> dp_left(n*m+1, -1e18);
vector<ll> dp_right(n*m+1, -1e18);
dp_left[0] = 0;
for(int i = 1; !dec.empty(); i++) {
dp_left[i] = dp_left[i-1] + dec.back();
dec.pop_back();
}
// Now right
vector<multiset<ll>> best_parts_1(m+1);
vector<multiset<ll>> best_parts_2(m+1);
auto add = [m](vector<multiset<ll>>&to, vector<ll> what, ll sum = 0) {
ll p = 0;
for(int i = 1; i <= m; i++) {
p += what[i-1];
to[i].insert(sum != 0 ? p - sum : p);
}
};
auto rem = [m](vector<multiset<ll>>&to, vector<ll> what) {
ll p = 0;
for(int i = 1; i <= m; i++) {
p += what[i-1];
to[i].erase(to[i].find(p));
}
};
for (int i = 0; i < inc.size(); i++) {
add(best_parts_2, inc[i].nd);
}
ll bigs = 0;
dp_right[0] = 0;
int full = 0;
for(int i = 1; i <= min(k, (int)inc.size() * m); i++) {
// At this moment there is i selected from the left.
int not_full = i % m;
if (not_full == 0) {
// Remove currently biggest stack from right, add to left.
rem(best_parts_2, inc[full].nd);
add(best_parts_1, inc[full].nd, inc[full].st);
bigs += inc[full].st;
dp_right[i] = bigs;
full++;
continue;
}
// Take X biggest stacks.
ll option_a = -1e18;
// Take X-1 biggest stacks.
ll option_b = -1e18;
option_a = bigs + *best_parts_2[not_full].rbegin();
if (full > 0) {
option_b = bigs + *best_parts_1[not_full].rbegin()+ inc[full].st;
}
dp_right[i] = max(option_a, option_b);
}
ll res = -1e18;
for(int i = 0; i <= k; i++) {
res = max(res, dp_left[i] + dp_right[k-i]);
}
// deb(dp_left, dp_right);
cout << res << "\n";
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cout << fixed << setprecision(10);
solve();
}
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 | #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 void solve() { int n,m,k; cin >> n >> m >> k; vector<ll> dec; // pairs (sum, vector) vector<pair<ll, vector<ll>>> inc; for(int i = 1; i <= n; i++) { vector<ll> V; ll sum = 0; for(int j = 1; j <= m; j++) { ll x; cin >> x; V.pb(x); sum += x; } // descreasing, we can take biggest ones always if(V[0] >= V.back()) { dec.insert(dec.end(), all(V)); } else { ll p = 0; for (auto v: V) p += v; inc.pb({p, move(V)}); } } sort(all(dec)); sort(all(inc)); reverse(all(inc)); /// aaaaanother not nice implementation :( /// Anyway, on the left side we add 'start' elements right away. /// On the right we need X = (k - 'start') / m full stacks and then it decreases. /// In general we take X biggest and best remaining part from the right. /// Or some subset of X - 1 from the X biggest and remaining from one of them. /// Bro I hate this shit. /// Let's handle this shit separately so we don't need to care about exact bounds. vector<ll> dp_left(n*m+1, -1e18); vector<ll> dp_right(n*m+1, -1e18); dp_left[0] = 0; for(int i = 1; !dec.empty(); i++) { dp_left[i] = dp_left[i-1] + dec.back(); dec.pop_back(); } // Now right vector<multiset<ll>> best_parts_1(m+1); vector<multiset<ll>> best_parts_2(m+1); auto add = [m](vector<multiset<ll>>&to, vector<ll> what, ll sum = 0) { ll p = 0; for(int i = 1; i <= m; i++) { p += what[i-1]; to[i].insert(sum != 0 ? p - sum : p); } }; auto rem = [m](vector<multiset<ll>>&to, vector<ll> what) { ll p = 0; for(int i = 1; i <= m; i++) { p += what[i-1]; to[i].erase(to[i].find(p)); } }; for (int i = 0; i < inc.size(); i++) { add(best_parts_2, inc[i].nd); } ll bigs = 0; dp_right[0] = 0; int full = 0; for(int i = 1; i <= min(k, (int)inc.size() * m); i++) { // At this moment there is i selected from the left. int not_full = i % m; if (not_full == 0) { // Remove currently biggest stack from right, add to left. rem(best_parts_2, inc[full].nd); add(best_parts_1, inc[full].nd, inc[full].st); bigs += inc[full].st; dp_right[i] = bigs; full++; continue; } // Take X biggest stacks. ll option_a = -1e18; // Take X-1 biggest stacks. ll option_b = -1e18; option_a = bigs + *best_parts_2[not_full].rbegin(); if (full > 0) { option_b = bigs + *best_parts_1[not_full].rbegin()+ inc[full].st; } dp_right[i] = max(option_a, option_b); } ll res = -1e18; for(int i = 0; i <= k; i++) { res = max(res, dp_left[i] + dp_right[k-i]); } // deb(dp_left, dp_right); cout << res << "\n"; } int32_t main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(10); solve(); } |
English