Submission
Status:
PPPPPPPPPP
Subtask/Task Score:
100/100
Score: 100
User: chayuth20
Problemset: บริษัททำความสะอาด
Language: cpp
Time: 0.004 second
Submitted On: 2026-10-05 21:05:30
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
int arr[505][505];
int main(){
int num;
int answer = 0;
cin >> num;
string s;
cin >> s;
int r = 0, c = 0, val = 0;
bool in_num = false;
for(char ch : s){
if(isdigit(ch)){
val = val * 10 + (ch - '0');
in_num = true;
} else if(in_num){
arr[r][c++] = val;
if(c == num){
c = 0;
r++;
}
val = 0;
in_num = false;
}
}
for(int i = 0; i < num; i++){
for(int j = 0; j < num; j++){
if(arr[i][j] > 0){
answer += 2;
}
int top = (i - 1 >= 0) ? arr[i-1][j] : 0;
if(arr[i][j] > top){
answer += arr[i][j] - top;
}
int bottom = (i + 1 < num) ? arr[i+1][j] : 0;
if(arr[i][j] > bottom){
answer += arr[i][j] - bottom;
}
int left = (j - 1 >= 0) ? arr[i][j-1] : 0;
if(arr[i][j] > left){
answer += arr[i][j] - left;
}
int right = (j + 1 < num) ? arr[i][j+1] : 0;
if(arr[i][j] > right){
answer += arr[i][j] - right;
}
}
}
cout << answer;
return 0;
}