#include <vector>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <cstring>
#include <bitset>
#include <iostream>
#include <queue>
#include <iomanip>
#include <complex>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin())
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define RED(i, n) FORD(i, n, 1)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define REPD(i, n) FORD(i, n-1,0)
#define printA(a, L, R) FOR(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
#define PRESENT(c, x) ((c).find(x) != (c).end())
#define CPRESENT(c, x) (find(ALL(c),x) != (c).end())
#define bit(x, i) (x&(1<<i))
#define lowbit(x) ((x)&((x)^((x)-1)))
const int fx[4][2] = {
{0, 1},
{1, 0},
{0, -1},
{-1, 0}
};
const int fxx[8][2] = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0},
{1, 1},
{1, -1},
{-1, 1},
{-1, -1}
};
const int sk[8][2] = {
{2, 1},
{2, -1},
{-2, -1},
{-2, 1},
{1, 2},
{-1, 2},
{1, -2},
{-1, -2}
};
const int maxa = 202112, maxn = 300024, maxp = 60;
const long long maxc = 40002, maxc2 = 25002, inf = 4000000000000000001ll;
const long long mod = 1000000007ll;
int n, m, k;
long long a[maxn], de[maxn], inc[maxn], ans;
struct ci {
vector<long long>p;
};
vector<ci>ma;
vector<long long>ro;
vector<vector<long long>>pref, suf;
bool cmp(const ci& A, const ci& B) {
return A.p[m - 1] > B.p[m - 1];
}
void solve() {
cin>>n>>m>>k;
REP(i, n) {
bool ros = false;
cin>>a[0];
RE(j, m - 1) {
cin>>a[j];
if (a[j] > a[j - 1])ros = true;
}
if (ros) {
ci pom;
pom.p.resize(m, 0);
pom.p[0] = a[0];
RE(j, m - 1) {
pom.p[j] = pom.p[j - 1] + a[j];
}
ma.push_back(pom);
} else {
REP(j, m)ro.push_back(a[j]);
}
}
sort(ro.begin(), ro.end());
RE(i, ro.size()) {
de[i] = de[i - 1] + ro[ro.size() - i];
}
sort(ma.begin(), ma.end(), cmp);
long long sum = 0;
suf.resize(ma.size());
REPD(i, ma.size()) {
vector<long long>pom;
REP(j, m) {
long long po = ma[i].p[j];
if (i < ma.size() - 1) {
po = max(po, suf[i + 1][j]);
}
pom.push_back(po);
}
suf[i] = pom;
}
REP(i, ma.size()) {
REP(j, m) {
const int akt = i * m + j + 1;
inc[akt] = sum + suf[i][j];
if (i > 0 && sum + pref[i - 1][j] + ma[i].p[m - 1] > inc[akt]) {
inc[akt] = sum + pref[i - 1][j] + ma[i].p[m - 1];
}
}
vector<long long>pom;
REP(j, m) {
long long po = ma[i].p[j] - ma[i].p[m - 1];
if (i > 0) {
po = max(po, pref[i - 1][j]);
}
pom.push_back(po);
}
pref.push_back(pom);
sum += ma[i].p[m - 1];
}
REP(i, k + 1)ans = max(ans, inc[i] + de[k - i]);
cout<<ans<<"\n";
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
}
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 | #include <vector> #include <cstdio> #include <stack> #include <algorithm> #include <queue> #include <map> #include <cmath> #include <unordered_set> #include <unordered_map> #include <set> #include <cstring> #include <bitset> #include <iostream> #include <queue> #include <iomanip> #include <complex> using namespace std; #define ALL(x) x.begin(), x.end() #define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin()) #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define RED(i, n) FORD(i, n, 1) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define REPD(i, n) FORD(i, n-1,0) #define printA(a, L, R) FOR(i,L,R) cout << a[i] << (i==R?'\n':' ') #define printV(a) printA(a,0,a.size()-1) #define PRESENT(c, x) ((c).find(x) != (c).end()) #define CPRESENT(c, x) (find(ALL(c),x) != (c).end()) #define bit(x, i) (x&(1<<i)) #define lowbit(x) ((x)&((x)^((x)-1))) const int fx[4][2] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} }; const int fxx[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1} }; const int sk[8][2] = { {2, 1}, {2, -1}, {-2, -1}, {-2, 1}, {1, 2}, {-1, 2}, {1, -2}, {-1, -2} }; const int maxa = 202112, maxn = 300024, maxp = 60; const long long maxc = 40002, maxc2 = 25002, inf = 4000000000000000001ll; const long long mod = 1000000007ll; int n, m, k; long long a[maxn], de[maxn], inc[maxn], ans; struct ci { vector<long long>p; }; vector<ci>ma; vector<long long>ro; vector<vector<long long>>pref, suf; bool cmp(const ci& A, const ci& B) { return A.p[m - 1] > B.p[m - 1]; } void solve() { cin>>n>>m>>k; REP(i, n) { bool ros = false; cin>>a[0]; RE(j, m - 1) { cin>>a[j]; if (a[j] > a[j - 1])ros = true; } if (ros) { ci pom; pom.p.resize(m, 0); pom.p[0] = a[0]; RE(j, m - 1) { pom.p[j] = pom.p[j - 1] + a[j]; } ma.push_back(pom); } else { REP(j, m)ro.push_back(a[j]); } } sort(ro.begin(), ro.end()); RE(i, ro.size()) { de[i] = de[i - 1] + ro[ro.size() - i]; } sort(ma.begin(), ma.end(), cmp); long long sum = 0; suf.resize(ma.size()); REPD(i, ma.size()) { vector<long long>pom; REP(j, m) { long long po = ma[i].p[j]; if (i < ma.size() - 1) { po = max(po, suf[i + 1][j]); } pom.push_back(po); } suf[i] = pom; } REP(i, ma.size()) { REP(j, m) { const int akt = i * m + j + 1; inc[akt] = sum + suf[i][j]; if (i > 0 && sum + pref[i - 1][j] + ma[i].p[m - 1] > inc[akt]) { inc[akt] = sum + pref[i - 1][j] + ma[i].p[m - 1]; } } vector<long long>pom; REP(j, m) { long long po = ma[i].p[j] - ma[i].p[m - 1]; if (i > 0) { po = max(po, pref[i - 1][j]); } pom.push_back(po); } pref.push_back(pom); sum += ma[i].p[m - 1]; } REP(i, k + 1)ans = max(ans, inc[i] + de[k - i]); cout<<ans<<"\n"; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); int tt = 1; // cin >> tt; while (tt--) { solve(); } return 0; } |
English