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
#include <bits/stdc++.h>
using namespace std;

const long long INF=2e18;

struct Stack 
{
    vector<long long> pref;

    long long total;
};

bool compare_stacks(const Stack& a, const Stack& b) 
{
    return a.total>b.total;
}

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

    int n,m,k;
    cin>>n>>m>>k;

    vector<long long> vals;
    vector<Stack> stacks;

    for(int i=0; i<n; i++) 
    {
        vector<long long> s(m);
    
        for(int j=0; j<m; j++) 
        {
            cin>>s[j];
        }
        
        bool convex=false;

        if(m>1 && s[0]<s[m-1]) 
        {
            convex=true;
        }

        if(convex) 
        {
            Stack sb;
        
            sb.pref.resize(m+1,0);
        
            for(int j=0; j<m; j++) 
            {
                sb.pref[j+1]=sb.pref[j]+s[j];
            }

            sb.total=sb.pref[m];
        
            stacks.push_back(sb);
        } 
        else 
        {
            for(int j=0; j<m; j++) 
            {
                vals.push_back(s[j]);
            }
        }
    }

    sort(vals.begin(),vals.end(),greater<long long>());

    vector<long long> pref(vals.size()+1,0);

    for(int i=0; i<(int)vals.size(); i++) 
    {
        pref[i+1]=pref[i]+vals[i];
    }

    sort(stacks.begin(),stacks.end(),compare_stacks);
    
    int nb=(int)stacks.size();
    
    vector<vector<long long>> suf(nb+2,vector<long long>(m+1,-INF));
    vector<vector<long long>> diff(nb+1,vector<long long>(m+1,-INF));

    for(int j=nb; j>=1; j--) 
    {
        for(int x=0; x<=m; x++)
        {
            suf[j][x]=max(suf[j+1][x],stacks[j-1].pref[x]);
        }
    }

    for(int j=1; j<=nb; j++) 
    {
        for(int x=0; x<=m; x++)
        {
            diff[j][x]=max(diff[j-1][x],stacks[j-1].pref[x]-stacks[j-1].total);
        }
    }

    vector<long long> sum(nb+1,0);
    
    for(int i=1; i<=nb; i++) 
    {
        sum[i]=sum[i-1]+stacks[i-1].total;
    }

    long long best=0;
    int total=(int)vals.size();

    for(int r=max(0,k-total); r<=min(k,nb*m); r++) 
    {
        long long val=0;
    
        if(r>0) 
        {
            int p=(r-1)/m; 
            int x=r-p*m;

            long long opt1=-INF;
            long long opt2=-INF;

            if(p<nb) 
            {
                opt1=sum[p]+suf[p+1][x];
            }

            if(p<nb) 
            {
                opt2=sum[p+1]+diff[p+1][x];
            }

            val=max(opt1,opt2);
        }
        
        if(val>-INF/2) 
        {
            best=max(best,val+pref[k-r]);
        }
    }

    cout<<best<<endl;
    
    return 0;
}