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
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cassert>

constexpr long long INF = 1e18;

std::vector<long long> GetPref(std::vector<long long> tab) {
    const int n = (int)tab.size();
    for (int i = 1; i < n; ++i) {
        tab[i] += tab[i - 1];
    }
    return tab;
}

std::vector<std::vector<long long>> ReorderFromLargest(const std::vector<std::vector<long long>>& prefs) {
    const int n = (int)prefs.size();
    std::vector<int> indices(n);
    if (n == 0) {
        return {};
    }
    const int m = (int)prefs[0].size();
    std::iota(indices.begin(), indices.end(), 0);
    std::sort(indices.begin(), indices.end(), [&](int lhs, int rhs) {
        return prefs[lhs].back() > prefs[rhs].back();
    });
    std::vector<std::vector<long long>> reordered(n, std::vector<long long>(m));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            reordered[i][j] = prefs[indices[i]][j];
        }
    }
    return reordered;
}

std::vector<std::vector<long long>> GetDiffFromLast(std::vector<std::vector<long long>> tab) {
    const int n = (int)tab.size();
    std::vector<int> indices(n);
    if (n == 0) {
        return {};
    }
    const int m = (int)tab[0].size();
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            tab[i][j] = tab[i][m - 1] - tab[i][j];
        }
    }
    return tab;
}

std::vector<std::vector<long long>> GetVerticalPreffixMin(std::vector<std::vector<long long>> tab) {
    const int n = (int)tab.size();
    std::vector<int> indices(n);
    if (n == 0) {
        return {};
    }
    const int m = (int)tab[0].size();
    for (int i = 1; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            tab[i][j] = std::min(tab[i - 1][j], tab[i][j]);
        }
    }
    return tab;
}

std::vector<std::vector<long long>> GetVerticalSuffixMax(std::vector<std::vector<long long>> tab) {
    const int n = (int)tab.size();
    std::vector<int> indices(n);
    if (n == 0) {
        return {};
    }
    const int m = (int)tab[0].size();
    for (int i = n - 2; i >= 0; --i) {
        for (int j = 0; j < m; ++j) {
            tab[i][j] = std::max(tab[i + 1][j], tab[i][j]);
        }
    }
    return tab;
}

std::vector<std::vector<long long>> GetVerticalPrefixSum(std::vector<std::vector<long long>> tab) {
    const int n = (int)tab.size();
    std::vector<int> indices(n);
    if (n == 0) {
        return {};
    }
    const int m = (int)tab[0].size();
    for (int i = 1; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            tab[i][j] = tab[i - 1][j] + tab[i][j];
        }
    }
    return tab;
}

int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);

    long long n, m, k;
    std::cin >> n >> m >> k;

    // prefix sums for increasing series
    std::vector<std::vector<long long>> prefs_in_incr;
    std::vector<long long> values_in_decr;

    for (int i = 0; i < n; ++i) {
        std::vector<long long> tab(m);
        for (int j = 0; j < m; ++j) {
            std::cin >> tab[j];
        }
        if (tab[0] >= tab.back()) {
            for (auto u : tab) {
                values_in_decr.push_back(u);
            }
        } else {
            prefs_in_incr.push_back(GetPref(tab));
        }
    }

    // extra structures for incr case
    prefs_in_incr = ReorderFromLargest(prefs_in_incr);
    std::vector<std::vector<long long>> min_diff_from_last_on_vertical_prefix_in_incr = GetVerticalPreffixMin(GetDiffFromLast(prefs_in_incr));
    std::vector<std::vector<long long>> max_on_vertical_suffix_in_incr = GetVerticalSuffixMax(prefs_in_incr);
    std::vector<std::vector<long long>> prefs_prefs_in_incr = GetVerticalPrefixSum(prefs_in_incr);

    // decr case
    std::sort(values_in_decr.begin(), values_in_decr.end(), [](long long a, long long b) {
        return a > b;
    });
    std::vector<long long> prefs_in_decr = GetPref(values_in_decr);

    auto GetFromDecr = [&](int how_many) {
        if (how_many == 0)
        {
            return 0ll;
        }
        
        if (how_many > (int)prefs_in_decr.size()) {
            return -INF;
        }
        return prefs_in_decr[how_many - 1];
    };

    auto GetFromIncr = [&](int how_many) {
        if (how_many == 0) {
            return 0ll;
        }
        const int pancakes_available = prefs_in_incr.size() * m;
        if (how_many > pancakes_available) {
            return -INF;
        }
        const int full_rows = how_many / m;
        const int rest = how_many % m;

        if (rest == 0) {
            return prefs_prefs_in_incr[full_rows - 1].back();
        }

        if (full_rows == 0) {
            return max_on_vertical_suffix_in_incr[0][rest - 1];
        }

        // Either we take first full_rows and then the best suffix
        // or take first full_rows - 1 except for the one with the smallest difference
        return std::max(prefs_prefs_in_incr[full_rows - 1].back() + max_on_vertical_suffix_in_incr[full_rows][rest - 1],
                        prefs_prefs_in_incr[full_rows].back() - min_diff_from_last_on_vertical_prefix_in_incr[full_rows][rest - 1]); 
    };

    long long result = 0;
    for (int from_incr = 0; from_incr <= k; ++from_incr) {
        result = std::max(result, GetFromIncr(from_incr) + GetFromDecr(k - from_incr));
    }

    std::cout << result << '\n';
}