Submission
Status:
PPPPPPPPPP
Subtask/Task Score:
100/100
Score: 100
User: gay69
Problemset: บริษัททำความสะอาด
Language: cpp
Time: 0.002 second
Submitted On: 2025-10-05 21:39:16
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int main() {
ll n;
string s;
cin >> n >> s;
ll m = s.size();
ll row = 0, col = 0;
ll arr[n][n];
for (int i = 0; i < m; i++) {
if (s[i] < '0' || s[i] > '9') {
continue;
}
string cur = "";
while (i < m && s[i] >= '0' && s[i] <= '9') {
cur.push_back(s[i]);
i++;
}
ll res = 0;
for (auto e : cur) {
res = (10 * res) + (e - '0');
}
arr[row][col++] = res;
if (col >= n) {
row++;
col = 0;
}
}
ll sum = 0, cur = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cur += (arr[i][j] == 0);
if (i == 0) {
sum += arr[i][j];
}
if (j == 0) {
sum += arr[i][j];
}
if (i == n - 1) {
sum += arr[i][j];
}
if (j == n - 1) {
sum += arr[i][j];
}
for (int k = 0; k < 4; k++) {
ll toi = i + dx[k];
ll toj = j + dy[k];
if (toi < 0 || toj < 0 || toi > n - 1 || toj > n - 1) {
continue;
}
sum += max(0ll, arr[i][j] - arr[toi][toj]);
}
}
}
cout << sum + 2 * (n * n - cur) << "\n";
return 0;
}