251422026-02-18 09:17:08Leventusz09Maximális összegű útcpp17Wrong answer 5/100192ms6260 KiB
#include <iostream>
#include <vector>
#define DEBUG false
#define int long long

#define IC for(int c=0; c<2; c++)

using namespace std;

signed main(){
    int N, M;
    cin >> N >> M;

    int A[N][M];
    for(int i=0; i<N; i++) for(int j=0; j<M; j++) cin >> A[i][j];
    int C[N][M];
    for(int i=0; i<N; i++) for(int j=0; j<M; j++){
        cin >> C[i][j];
        C[i][j]--;
    }

    //int L[N][M][500];
    int o = A[0][0];
    int LR[M][500];
    int LN[500];
    for(int i=0; i<M; i++) IC LR[i][c] = 0;

    for(int i=0; i<N; i++){
        for(int j=0; j<M; j++){
            IC{
                if(i == 0 && j == 0){
                    LN[c] = 0;
                    if(C[0][0] == c) LN[c] = A[0][0];
                    LR[0][c] = LN[c];
                }else
                if(i == 0){
                    if(LN[c] > 0){
                        LN[c] += A[i][j];
                    }else{
                        LN[c] = C[i][j] == c ? A[i][j] : LN[c] + A[i][j];
                    }
                    
                    LR[j][c] = LN[c];
                }
                else if(j==0){
                    if(LR[0][c] > 0){
                        LN[c] = LR[0][c] + A[i][j];
                    }else{
                        LN[c] = C[i][j] == c ? A[i][j] : LN[c] + A[i][j];
                    }

                    LR[j][c] = LN[c];
                }else{
                    int lu = LR[j][c];
                    int ll = LN[c];
                    if(lu < 0 && ll < 0){
                        if(C[i][j] == c){
                            LN[c] = A[i][j];
                        }else{
                            LN[c] = max(lu, ll) + A[i][j];
                        }
                    //}else LN[c] = C[i][j] == c ? max(lu, ll) + A[i][j] : 0;
                    }else if(lu == 0 && ll == 0){
                        LN[c] = C[i][j] == c ? A[i][j] : LN[c] + A[i][j];
                    }else LN[c] = max(lu, ll) + A[i][j];

                    LR[j][c] = LN[c];
                }
                if(C[i][j] == c) if(LN[c] > o){o = LN[c];
                    #if DEBUG
                    cout << "D" << i <<" " << j <<" "<<c<< endl;
                    #endif
                }
            }
        }

        #if DEBUG
        for(int i=0; i<M; i++){
            IC{
                cout << LR[i][c] << ".";
            }
            cout << " ";
        }
        cout << endl;
        #endif
    }

    cout << o << endl;
    
    return 0;
}

/*
3 3
1 1 1
1 1 1
1 1 1
1 2 3
4 5 6
7 8 9

*/
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer1ms316 KiB
2Accepted1ms512 KiB
subtask20/4
3Accepted1ms316 KiB
4Accepted1ms316 KiB
5Accepted1ms316 KiB
6Accepted1ms316 KiB
7Wrong answer1ms408 KiB
subtask30/7
8Accepted1ms316 KiB
9Accepted1ms316 KiB
10Accepted1ms316 KiB
11Accepted1ms316 KiB
12Wrong answer1ms408 KiB
13Accepted1ms316 KiB
14Accepted1ms316 KiB
15Accepted1ms316 KiB
16Accepted1ms316 KiB
17Accepted1ms536 KiB
18Wrong answer1ms316 KiB
19Wrong answer1ms316 KiB
20Wrong answer1ms316 KiB
21Wrong answer1ms316 KiB
22Wrong answer2ms316 KiB
23Wrong answer2ms316 KiB
24Accepted2ms332 KiB
25Wrong answer2ms316 KiB
26Wrong answer1ms316 KiB
27Wrong answer2ms336 KiB
subtask40/18
28Accepted1ms316 KiB
29Accepted1ms316 KiB
30Accepted1ms316 KiB
31Accepted1ms316 KiB
32Wrong answer1ms408 KiB
33Accepted1ms316 KiB
34Accepted1ms316 KiB
35Accepted1ms316 KiB
36Accepted1ms316 KiB
37Accepted1ms536 KiB
38Wrong answer1ms316 KiB
39Wrong answer1ms316 KiB
40Wrong answer1ms316 KiB
41Wrong answer1ms316 KiB
42Wrong answer2ms316 KiB
43Wrong answer2ms316 KiB
44Accepted2ms332 KiB
45Wrong answer2ms316 KiB
46Wrong answer1ms316 KiB
47Wrong answer2ms336 KiB
48Accepted4ms840 KiB
49Accepted4ms820 KiB
50Wrong answer4ms948 KiB
51Wrong answer4ms820 KiB
52Wrong answer6ms820 KiB
53Wrong answer6ms1012 KiB
54Wrong answer6ms1012 KiB
55Wrong answer6ms916 KiB
56Wrong answer8ms708 KiB
57Wrong answer8ms712 KiB
58Accepted8ms820 KiB
59Wrong answer8ms820 KiB
60Wrong answer8ms820 KiB
61Wrong answer8ms820 KiB
subtask55/5
62Accepted1ms316 KiB
63Accepted1ms316 KiB
64Accepted101ms6060 KiB
65Accepted103ms6040 KiB
66Accepted168ms6008 KiB
67Accepted158ms5684 KiB
68Accepted175ms6196 KiB
subtask60/12
69Accepted2ms316 KiB
70Accepted2ms316 KiB
71Wrong answer104ms6188 KiB
72Wrong answer104ms6196 KiB
73Wrong answer168ms5976 KiB
74Wrong answer158ms5704 KiB
75Wrong answer178ms6056 KiB
subtask70/54
76Wrong answer1ms316 KiB
77Accepted2ms316 KiB
78Accepted1ms316 KiB
79Accepted1ms316 KiB
80Accepted1ms316 KiB
81Accepted1ms316 KiB
82Wrong answer1ms408 KiB
83Accepted1ms316 KiB
84Accepted1ms316 KiB
85Accepted1ms316 KiB
86Accepted1ms316 KiB
87Accepted1ms536 KiB
88Wrong answer1ms316 KiB
89Wrong answer1ms316 KiB
90Wrong answer1ms316 KiB
91Wrong answer1ms316 KiB
92Wrong answer2ms316 KiB
93Wrong answer2ms316 KiB
94Accepted2ms332 KiB
95Wrong answer2ms316 KiB
96Wrong answer1ms316 KiB
97Wrong answer2ms336 KiB
98Accepted4ms840 KiB
99Accepted4ms820 KiB
100Wrong answer4ms948 KiB
101Wrong answer4ms820 KiB
102Wrong answer6ms820 KiB
103Wrong answer6ms1012 KiB
104Wrong answer6ms1012 KiB
105Wrong answer6ms916 KiB
106Wrong answer8ms708 KiB
107Wrong answer8ms712 KiB
108Accepted8ms820 KiB
109Wrong answer8ms820 KiB
110Wrong answer8ms820 KiB
111Wrong answer8ms820 KiB
112Accepted1ms316 KiB
113Accepted1ms316 KiB
114Accepted101ms6060 KiB
115Accepted103ms6040 KiB
116Accepted168ms6008 KiB
117Accepted158ms5684 KiB
118Accepted175ms6196 KiB
119Accepted2ms316 KiB
120Accepted2ms316 KiB
121Wrong answer104ms6188 KiB
122Wrong answer104ms6196 KiB
123Wrong answer168ms5976 KiB
124Wrong answer158ms5704 KiB
125Wrong answer178ms6056 KiB
126Wrong answer1ms508 KiB
127Wrong answer2ms316 KiB
128Wrong answer119ms6260 KiB
129Wrong answer120ms6236 KiB
130Wrong answer184ms5880 KiB
131Wrong answer173ms5684 KiB
132Wrong answer177ms6192 KiB
133Wrong answer177ms6208 KiB
134Wrong answer181ms6196 KiB
135Wrong answer182ms6200 KiB
136Wrong answer192ms6196 KiB