Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: mantaggez

Problemset: บริษัททำความสะอาด

Language: cpp

Time: 0.004 second

Submitted On: 2026-03-22 20:39:41

#include <bits/stdc++.h>

using namespace std;

const int nx = 5e2+5;

int n, ans;
int grid[nx][nx];
int di[] = {-1, 0, 0, 1};
int dj[] = {0, -1, 1, 0};
string s;

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin >> n >> s;
    
    int i = -1, j = 1;
    string num = "";
    for(char& c : s) {
        if(c == '[') {
            i++;
            j = 1;
        }
        if('0' <= c && c <= '9') {
            num += c;
        }
        if(c == ',' || c == ']') {
            if(num.empty()) continue;
            grid[i][j] = stoi(num);
            j++;
            num = "";
        }
    }

    for(int i=1;i<=n;i++) {
        for(int j=1;j<=n;j++) {
            ans += (grid[i][j] * 4);
            if(grid[i][j] > 0) ans += 2;
            for(int d=0;d<4;d++) {
                int ni = i + di[d], nj = j + dj[d];
                ans -= min(grid[i][j], grid[ni][nj]);
            }
        }
    }

    cout << ans << '\n';

    return 0;
}