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
236
237
238
239
240
241
242
243
244
245
#include <algorithm>
#include <cstdio>
#include <map>
#include <numeric>
#include <vector>

struct Stos
{
  std::vector<long> sumy; // indeksowane od 0 przy czym zawsze sumy[0]=0

  Stos(int len) : sumy(len, 0l) { }
  
  Stos(const Stos& a, const Stos& b) : sumy(a.sumy.size() + b.sumy.size() - 1, 0l)
  {
    const int size_a = a.sumy.size();
    const int size_b = b.sumy.size();
    
    for (int m_a=0; m_a<size_a; ++m_a) {
      for (int m_b=0; m_b<size_b; ++m_b) {
        int m = m_a + m_b;
        long suma = a.sumy[m_a] + b.sumy[m_b];
        sumy[m] = std::max(sumy[m], suma);
      }
    }
  }
};

struct StosStruct
{
  int size;
  std::vector<long> liczby;
  std::vector<long> sumy;
  std::vector<long> a_sumy;
  std::vector<long> b_sumy;

  StosStruct(int size = 0) : size(size), liczby(size, 0l), sumy(size+1, 0l) {
    liczby.reserve(300000);
    sumy.reserve(300001);
    a_sumy.reserve(300001);
    b_sumy.reserve(300001);
  }

  // b musi być niemalejący
  void dolacz(const long* b, int b_size)
  {
    int a_size = size;
    a_sumy.resize(a_size+1, 0l);
    std::copy(sumy.begin(), sumy.end(), a_sumy.begin());

    b_sumy.resize(b_size+1, 0l);
    std::partial_sum(b, b+b_size, b_sumy.begin()+1);
    
    size += b_size;
    liczby.resize(size, 0l);
    sumy.resize(size + 1, 0l);

    // załatwiamy specjalny przypadek ze skrajnymi m_a/m_b
    for (int m_a=1; m_a<=a_size; ++m_a) {
      sumy[m_a+b_size] = std::max(sumy[m_a+b_size], a_sumy[m_a] + b_sumy[b_size]);
    }
    for (int m_b=1; m_b<=b_size; ++m_b) {
      sumy[m_b] = std::max(sumy[m_b], b_sumy[m_b]);
      sumy[m_b+a_size] = std::max(sumy[m_b+a_size], b_sumy[m_b] + a_sumy[a_size]);
    }
    long bmin = b[0], bmax = b[b_size-1];
    for (int m_a=1; m_a<a_size; ++m_a) {
      // m_a = ile elementów bierzemy z początku a -> sa[m_a]
      // m_b = ile elementów z początku b -> sb[m_b]
      if (liczby[m_a-1] >= liczby[m_a] && liczby[m_a] <= bmax && liczby[m_a-1] >= bmin) {
        int lb = std::lower_bound(b, b+b_size, liczby[m_a]) - b;
        int ub = std::upper_bound(b, b+b_size, liczby[m_a-1]) - b - 1;

        for (int i=lb; i<ub; ++i) {
          int m_b = i + 1;
          int m = m_a + m_b;
          sumy[m] = std::max(sumy[m], a_sumy[m_a] + b_sumy[m_b]);
        }
      }
    }
    for (int m=0; m<size; ++m) {
      liczby[m] = sumy[m+1] - sumy[m];
      //printf("%ld ", liczby[m]);
    }
    //putchar('\n');
  }
};

std::vector<long> rozmiary;

void read_rozmiary(int N, int M) {
  rozmiary.resize(N * M);
  long* p = rozmiary.data();
  for (int n=0; n<N; ++n) {
    for (int m=0; m<M; ++m) {
      scanf("%ld", p++);
    }
  }
}

bool is_rosnacy(long* StosStruct, int M) {
  for (int m=1; m<M; ++m) {
    if (StosStruct[m-1] < StosStruct[m]) {
      return true;
    }
    if (StosStruct[m-1] > StosStruct[m]) {
      return false;
    }
  }
  return false;
}

long brut(int N, int M, int K)
{
  std::vector<Stos> rosnace;
  std::vector<Stos> laczone;

  std::vector<long> pozostale;

  rosnace.reserve(N);
  laczone.reserve(N);
  pozostale.reserve(N * M);
  for (int n=0; n<N; ++n) {
    long* src = rozmiary.data() + n * M;
    if (is_rosnacy(src, M)) {
      rosnace.emplace_back(M+1);
      Stos& stos = rosnace.back();
      
      stos.sumy[0] = 0;
      for (int m=0; m<M; ++m) {
        stos.sumy[m+1] = stos.sumy[m] + src[m];
      }
    } else {
      for (int m=0; m<M; ++m) {
        pozostale.push_back(src[m]);
      }
    }
  }
  int NM_pos = pozostale.size();
  std::sort(pozostale.begin(), pozostale.end(), std::greater<long>());
  for (int i=1; i<NM_pos; ++i) {
    pozostale[i] += pozostale[i-1];
  }

  if (rosnace.empty()) {
    return pozostale[K-1];
  }

  int count;
  while ((count = rosnace.size()) > 1) {
    
    for (int i=0; i<count; i+=2) {
      if (i+1 < count) {
        laczone.emplace_back(rosnace[i], rosnace[i+1]);
      } else {
        laczone.push_back(rosnace[i]);
      }
    }
    
    std::swap(rosnace, laczone);
    laczone.clear();
  }


  long result = 0;
  int max_kr = rosnace[0].sumy.size() - 1;
  int max_kp = pozostale.size();
  for (int kr=0; kr<=K; ++kr) {
    // kr z rosnącego, pozostałe z pozostałych
    
    long candidate = rosnace[0].sumy[std::min(kr, max_kr)];
    int kp = std::min(max_kp, std::max(K - kr, 0));
    if (kp > 0) {
      candidate += pozostale[kp - 1];
    }
    result = std::max(result, candidate);
  }
  return result;
}

long opt(int N, int M, int K)
{
  StosStruct rosnace;
  std::vector<long> pozostale;

  pozostale.reserve(N * M);
  for (int n=0; n<N; ++n) {
    long* src = rozmiary.data() + n * M;
    if (is_rosnacy(src, M)) {
      rosnace.dolacz(src, M);
    } else {
      for (int m=0; m<M; ++m) {
        pozostale.push_back(src[m]);
      }
    }
  }
  int NM_pos = pozostale.size();
  std::sort(pozostale.begin(), pozostale.end(), std::greater<long>());
  for (int i=1; i<NM_pos; ++i) {
    pozostale[i] += pozostale[i-1];
  }

  if (rosnace.size == 0) {
    return pozostale[K-1];
  }

  long result = 0;
  int kr_max = rosnace.size;
  int kp_max = pozostale.size();
  for (int kr=0; kr<=K; ++kr) {
    // kr z rosnącego, pozostałe z pozostałych
    int kp = K - kr;

    long candidate = 0;
    if (kr > kr_max) {
      continue;
    }
    candidate += rosnace.sumy[kr];

    if (kp > 0) {
      if (kp > kp_max) {
        continue;
      }
      candidate += pozostale[kp-1];
    }
    result = std::max(result, candidate);
  }
  return result;
}


int main()
{
  int N, M, K;
  scanf("%d%d%d", &N, &M, &K);
  read_rozmiary(N, M);

  long result = 0;
  if (M <= 10) {
    result = brut(N, M, K);
  } else {
    result = opt(N, M, K);
  }

  printf("%ld\n", result);
}