#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <stack>
#include <string>
#include <cstring>
#include <cassert>
#include <limits.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(VAR(i,a);i<=(b);++i)
#define FORD(i,a,b) for(VAR(i,a);i>=(b);--i)
#define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a)
#define VAR(a,b) __typeof(b) a=(b)
#define SIZE(a) ((int)((a).size()))
#define ALL(x) (x).begin(),(x).end()
#define CLR(x,a) memset(x,a,sizeof(x))
#define ZERO(x) memset(x,0,sizeof(x))
#define S(t,i) scanf("%" ## t, &i)
#define SI(i) scanf("%d", &i)
const int MAXN=300000+11;
int n, m, k;
LL nalesniki_odwrocone[MAXN];
int liczba_nalesniki_odwrocone = 0;
LL nalesniki[2*MAXN];
LL prefiksy[2*MAXN];
LL sumy_stosow[MAXN];
LL prefiksy_sumy_stosow[MAXN];
vector<int> stosy_nieodwrocone;
LL odwrocone_prefiksy_sum[MAXN];
LL *get_stos(int i) {
return nalesniki+i*(m+1);
}
LL *get_prefiks_stosu(int i) {
return prefiksy+i*(m+1);
}
LL max_result;
int compare_ints(const void* a, const void* b)
{
LL arg1 = *(const LL*)a;
LL arg2 = *(const LL*)b;
if (arg1 < arg2) return 1;
if (arg1 > arg2) return -1;
return 0;
}
LL oblicz_sume_pelnych(int oprocz_stosu, int ile_pelnych_stosow) {
if (ile_pelnych_stosow == 0) return 0;
// assert(ile_pelnych_stosow>0);
if (ile_pelnych_stosow <= oprocz_stosu) {
return prefiksy_sumy_stosow[ile_pelnych_stosow];
} else {
return prefiksy_sumy_stosow[ile_pelnych_stosow + 1]
- sumy_stosow[stosy_nieodwrocone[oprocz_stosu]];
}
}
LL oblicz(LL ile_pelnych_stosow, int ile_z_niepelengo, int oprocz_stosu, LL* prefiksy_stosu) {
LL rem = k - ile_pelnych_stosow * m - ile_z_niepelengo;
if (rem < 0) {
// cout << k << " " << ile_pelnych_stosow << " " << ile_z_niepelengo<< "\n";
return 0;
}
// assert(rem >= 0);
// assert(ile_z_niepelengo > 0);
// assert(ile_z_niepelengo < m);
// assert(ile_pelnych_stosow >= 0);
// assert(rem <= k);
return oblicz_sume_pelnych(oprocz_stosu, ile_pelnych_stosow)
+ prefiksy_stosu[ile_z_niepelengo]
+ odwrocone_prefiksy_sum[rem];
};
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m >> k;
REP(ni, n) {
LL *stos = get_stos(ni);
cin >> stos[0];
FOR(mi, 1, m-1) {
cin >> stos[mi];
}
if (stos[0] >= stos[m-1]) {
FOR(mi, 0, m-1) {
nalesniki_odwrocone[liczba_nalesniki_odwrocone] = stos[mi];
liczba_nalesniki_odwrocone++;
}
} else {
stosy_nieodwrocone.push_back(ni);
}
}
qsort(nalesniki_odwrocone, liczba_nalesniki_odwrocone, sizeof(nalesniki_odwrocone[0]), compare_ints);
// REP(ni, liczba_nalesniki_odwrocone) {
// //cout << nalesniki_odwrocone[ni] << " ";
// if (ni > 0) {
// assert(nalesniki_odwrocone[ni] <= nalesniki_odwrocone[ni-1]);
// }
// }
// cout << "\n\n";
odwrocone_prefiksy_sum[0]=0;
FOR(ki, 1, k) {
odwrocone_prefiksy_sum[ki] = odwrocone_prefiksy_sum[ki-1] + nalesniki_odwrocone[ki-1];
}
FORE(ni, stosy_nieodwrocone) {
LL *prefiksy_stosu = get_prefiks_stosu(*ni);
LL *stos = get_stos(*ni);
FOR(mi, 1, m) {
prefiksy_stosu[mi] = prefiksy_stosu[mi-1] + stos[mi-1];
}
sumy_stosow[*ni] = prefiksy_stosu[m];
}
std::sort(stosy_nieodwrocone.begin(), stosy_nieodwrocone.end(),
[](int a, int b)
{
return sumy_stosow[a] > sumy_stosow[b];
});
// FORE(ni, stosy_nieodwrocone) {
// if (ni != stosy_nieodwrocone.begin()) {
// assert(sumy_stosow[*ni] <= sumy_stosow[*(ni-1)]);
// }
// //cout << sumy_stosow[*ni] << " ";
// }
// cout << "\n\n";
LL liczba_nieodwroconych_stosow = stosy_nieodwrocone.size();
FOR(i, 1, liczba_nieodwroconych_stosow) {
prefiksy_sumy_stosow[i] = prefiksy_sumy_stosow[i-1] + sumy_stosow[stosy_nieodwrocone[i-1]];
}
FOR(f, 0, liczba_nieodwroconych_stosow) {
LL rem = k - f * m;
if (rem >= 0 && rem <= liczba_nalesniki_odwrocone) {
max_result = max(max_result, prefiksy_sumy_stosow[f] + odwrocone_prefiksy_sum[rem]);
}
}
FOR(oprocz_stosu, 0, liczba_nieodwroconych_stosow-1) {
LL *prefiksy_stosu = get_prefiks_stosu(stosy_nieodwrocone[oprocz_stosu]);
FOR(ile_z_niepelengo, 1, m-1) {
LL req = k - ile_z_niepelengo - liczba_nalesniki_odwrocone;
LL L = (req > 0) ? (req + m - 1) / m : 0;
LL r0 = liczba_nieodwroconych_stosow - 1;
LL r1 = (k - ile_z_niepelengo) / m;
// if (k < ile_z_niepelengo) {
// cout << r1 << "\n";
// }
LL R = min(r0, r1);
if (L <= R) {
while (R - L > 2) {
LL m1 = L + (R - L) / 3;
LL m2 = R - (R - L) / 3;
if (oblicz(m1, ile_z_niepelengo, oprocz_stosu, prefiksy_stosu)
< oblicz(m2, ile_z_niepelengo, oprocz_stosu, prefiksy_stosu)) {
L = m1;
} else {
R = m2;
}
}
for (LL ile_pelnych_stosow = L; ile_pelnych_stosow <= R; ++ile_pelnych_stosow) {
if (k - ile_pelnych_stosow * m - ile_z_niepelengo >= 0) {
max_result = max(max_result,
oblicz(ile_pelnych_stosow, ile_z_niepelengo, oprocz_stosu, prefiksy_stosu));
}
}
}
}
}
cout << max_result << "\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 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 | #include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <list> #include <vector> #include <map> #include <unordered_map> #include <unordered_set> #include <set> #include <bitset> #include <stack> #include <string> #include <cstring> #include <cassert> #include <limits.h> using namespace std; typedef long long LL; typedef long double LD; typedef vector<int> VI; #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(VAR(i,a);i<=(b);++i) #define FORD(i,a,b) for(VAR(i,a);i>=(b);--i) #define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a) #define VAR(a,b) __typeof(b) a=(b) #define SIZE(a) ((int)((a).size())) #define ALL(x) (x).begin(),(x).end() #define CLR(x,a) memset(x,a,sizeof(x)) #define ZERO(x) memset(x,0,sizeof(x)) #define S(t,i) scanf("%" ## t, &i) #define SI(i) scanf("%d", &i) const int MAXN=300000+11; int n, m, k; LL nalesniki_odwrocone[MAXN]; int liczba_nalesniki_odwrocone = 0; LL nalesniki[2*MAXN]; LL prefiksy[2*MAXN]; LL sumy_stosow[MAXN]; LL prefiksy_sumy_stosow[MAXN]; vector<int> stosy_nieodwrocone; LL odwrocone_prefiksy_sum[MAXN]; LL *get_stos(int i) { return nalesniki+i*(m+1); } LL *get_prefiks_stosu(int i) { return prefiksy+i*(m+1); } LL max_result; int compare_ints(const void* a, const void* b) { LL arg1 = *(const LL*)a; LL arg2 = *(const LL*)b; if (arg1 < arg2) return 1; if (arg1 > arg2) return -1; return 0; } LL oblicz_sume_pelnych(int oprocz_stosu, int ile_pelnych_stosow) { if (ile_pelnych_stosow == 0) return 0; // assert(ile_pelnych_stosow>0); if (ile_pelnych_stosow <= oprocz_stosu) { return prefiksy_sumy_stosow[ile_pelnych_stosow]; } else { return prefiksy_sumy_stosow[ile_pelnych_stosow + 1] - sumy_stosow[stosy_nieodwrocone[oprocz_stosu]]; } } LL oblicz(LL ile_pelnych_stosow, int ile_z_niepelengo, int oprocz_stosu, LL* prefiksy_stosu) { LL rem = k - ile_pelnych_stosow * m - ile_z_niepelengo; if (rem < 0) { // cout << k << " " << ile_pelnych_stosow << " " << ile_z_niepelengo<< "\n"; return 0; } // assert(rem >= 0); // assert(ile_z_niepelengo > 0); // assert(ile_z_niepelengo < m); // assert(ile_pelnych_stosow >= 0); // assert(rem <= k); return oblicz_sume_pelnych(oprocz_stosu, ile_pelnych_stosow) + prefiksy_stosu[ile_z_niepelengo] + odwrocone_prefiksy_sum[rem]; }; int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> k; REP(ni, n) { LL *stos = get_stos(ni); cin >> stos[0]; FOR(mi, 1, m-1) { cin >> stos[mi]; } if (stos[0] >= stos[m-1]) { FOR(mi, 0, m-1) { nalesniki_odwrocone[liczba_nalesniki_odwrocone] = stos[mi]; liczba_nalesniki_odwrocone++; } } else { stosy_nieodwrocone.push_back(ni); } } qsort(nalesniki_odwrocone, liczba_nalesniki_odwrocone, sizeof(nalesniki_odwrocone[0]), compare_ints); // REP(ni, liczba_nalesniki_odwrocone) { // //cout << nalesniki_odwrocone[ni] << " "; // if (ni > 0) { // assert(nalesniki_odwrocone[ni] <= nalesniki_odwrocone[ni-1]); // } // } // cout << "\n\n"; odwrocone_prefiksy_sum[0]=0; FOR(ki, 1, k) { odwrocone_prefiksy_sum[ki] = odwrocone_prefiksy_sum[ki-1] + nalesniki_odwrocone[ki-1]; } FORE(ni, stosy_nieodwrocone) { LL *prefiksy_stosu = get_prefiks_stosu(*ni); LL *stos = get_stos(*ni); FOR(mi, 1, m) { prefiksy_stosu[mi] = prefiksy_stosu[mi-1] + stos[mi-1]; } sumy_stosow[*ni] = prefiksy_stosu[m]; } std::sort(stosy_nieodwrocone.begin(), stosy_nieodwrocone.end(), [](int a, int b) { return sumy_stosow[a] > sumy_stosow[b]; }); // FORE(ni, stosy_nieodwrocone) { // if (ni != stosy_nieodwrocone.begin()) { // assert(sumy_stosow[*ni] <= sumy_stosow[*(ni-1)]); // } // //cout << sumy_stosow[*ni] << " "; // } // cout << "\n\n"; LL liczba_nieodwroconych_stosow = stosy_nieodwrocone.size(); FOR(i, 1, liczba_nieodwroconych_stosow) { prefiksy_sumy_stosow[i] = prefiksy_sumy_stosow[i-1] + sumy_stosow[stosy_nieodwrocone[i-1]]; } FOR(f, 0, liczba_nieodwroconych_stosow) { LL rem = k - f * m; if (rem >= 0 && rem <= liczba_nalesniki_odwrocone) { max_result = max(max_result, prefiksy_sumy_stosow[f] + odwrocone_prefiksy_sum[rem]); } } FOR(oprocz_stosu, 0, liczba_nieodwroconych_stosow-1) { LL *prefiksy_stosu = get_prefiks_stosu(stosy_nieodwrocone[oprocz_stosu]); FOR(ile_z_niepelengo, 1, m-1) { LL req = k - ile_z_niepelengo - liczba_nalesniki_odwrocone; LL L = (req > 0) ? (req + m - 1) / m : 0; LL r0 = liczba_nieodwroconych_stosow - 1; LL r1 = (k - ile_z_niepelengo) / m; // if (k < ile_z_niepelengo) { // cout << r1 << "\n"; // } LL R = min(r0, r1); if (L <= R) { while (R - L > 2) { LL m1 = L + (R - L) / 3; LL m2 = R - (R - L) / 3; if (oblicz(m1, ile_z_niepelengo, oprocz_stosu, prefiksy_stosu) < oblicz(m2, ile_z_niepelengo, oprocz_stosu, prefiksy_stosu)) { L = m1; } else { R = m2; } } for (LL ile_pelnych_stosow = L; ile_pelnych_stosow <= R; ++ile_pelnych_stosow) { if (k - ile_pelnych_stosow * m - ile_z_niepelengo >= 0) { max_result = max(max_result, oblicz(ile_pelnych_stosow, ile_z_niepelengo, oprocz_stosu, prefiksy_stosu)); } } } } } cout << max_result << "\n"; return 0; } |
English