Submission

Status:

-PP-------

Subtask/Task Score:

20/100

Score: 20

User: spammer_destroyer

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

Language: cpp

Time: 0.002 second

Submitted On: 2025-11-04 17:44:49

#include <bits/stdc++.h>
using namespace std;
int f(int n) {
  if(n<0) {
    return 0;
  }
  else {
    return n;
  }
}
int main() {
    int i,j,n;
    cin >> n;
    vector<int> arr;
    vector<vector<int>> x(n,vector<int>(n));
    string str;
    cin >> str;
    int len=str.length();
    int temp=-1;
    for(i=0;i<len;i++) {
      if(str[i]>='0'&&str[i]<='9') {
        for(j=i;j<len;j++) {
          if(str[j]<'0'||str[j]>'9') {
            arr.push_back(temp);
            i=j;break;
          }
          else if(j==i) {
            temp=(int)str[j]-48;
          }
          else {
            temp*=10;
            temp+=((int)str[j]-48);
          }
        }
      }
    }
    int k=0;
    for(i=0;i<n;i++) {
      for(j=0;j<n;j++,k++) {
        x[i][j]=arr[k];
      }
    }
    int sum=0;
    for(i=0;i<n;i++) {
      for(j=0;j<n;j++) {
        sum+=2;
        if(j+1>=n) {
          sum+=x[i][j];
        }
        else {
          sum+=f(x[i][j]-x[i][j+1]);
        }
        
        if(i+1>=n) {
          sum+=x[i][j];
        }
        else {
          sum+=f(x[i][j]-x[i+1][j]);
        }
        
        if(j-1<0) {
          sum+=x[i][j];
        }
        else {
          sum+=f(x[i][j]-x[i][j-1]);
        }
        
        if(i-1<0) {
          sum+=x[i][j];
        }
        else {
          sum+=f(x[i][j]-x[i-1][j]);
        }
        
      }
    }
    /*
    for(i=0;i<n;i++) {
      for(j=0;j<n;j++,k++) {
        cout << x[i][j] << " ";
      }
      cout << "\n";
    }*/
    cout << sum;
    return 0;
}