Submission
Status:
PPPPPPPPPP
Subtask/Task Score:
100/100
Score: 100
User: Prap
Problemset: สูงต่ำในตาราง
Language: cpp
Time: 0.002 second
Submitted On: 2026-04-10 15:45:58
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
int main() {
int N,B;
cin>>N>>B;
int nig[N][N];
int maxid = 0;
int minid = 0;
int min,max;
int minamt = 0;
int maxamt = 0;
for (int i = 0; i<N; i++) {
for (int j = 0; j<N; j++) {
cin>>nig[i][j];
}
}
min = nig[0][0];
max = nig[0][0];
for (int i = 0; i<N; i++) {
for (int j = 0; j<N; j++) {
if (nig[i][j] > max) {
maxid = i;
max = nig[i][j];
}
if (nig[i][j] < min) {
minid = i;
min = nig[i][j];
}
}
}
for (int i = 0; i<N; i++) {
for (int j = 0; j<N; j++) {
if (nig[i][j] == max) {
maxamt++;
}
if (nig[i][j] == min) {
minamt++;
}
}
}
if (maxid != minid) {
cout<<max-min+2*B;
return 0;
}
else {
int anothermaxid = 0, anotherminid = 0;
int anothermin = 1000000, anothermax = -1000000;
int orig = max - min;
for (int i = 0; i<N; i++) {
for (int j = 0; j<N; j++) {
if (nig[i][j] < max && nig[i][j] > anothermax) {
anothermaxid = i;
anothermax = nig[i][j];
}
if (nig[i][j] < anothermin && nig[i][j] > min) {
anotherminid = i;
anothermin = nig[i][j];
}
}
}
int a,s,d;
a = anothermax - min;
s = max - anothermin;
d = anothermax - anothermin;
if (anothermaxid != minid) {
a+=2*B;
}
if (maxid != anotherminid) {
s+=2*B;
}
if (anothermaxid != anotherminid) {
d+=2*B;
}
if (orig >=a && orig >=s && orig >=d) {
cout<<orig;
return 0;
}
else if (a >= orig && a >=s && a >=d) {
cout<<a;
return 0;
}
else if (s >= a && s >= orig && s >=d) {
cout<<s;
return 0;
}
else {
cout<<d;
}
}
}