Contest Duration: - (local time) (100 minutes) Back to Home

## B - Blocks on Grid Editorial by en_translator

It is optimal to set them to the minimum value in the grid.

First find the minimum value, and then calculate the sum of each element subtracted by the minimum value.

Sample Code (C)

#include<stdio.h>

int a[110][110];
int main(){
int h,w;
scanf("%d%d",&h,&w);
for(int i=0;i<h;i++)for(int j=0;j<w;j++)scanf("%d",&a[i][j]);
int m=1e9;
for(int i=0;i<h;i++)for(int j=0;j<w;j++)if(m>a[i][j])m=a[i][j];
int ans=0;
for(int i=0;i<h;i++)for(int j=0;j<w;j++)ans+=a[i][j]-m;
printf("%d\n",ans);
}


Sample Code (Python)

import numpy as np
H,W = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(H)]
A = np.array(A)
print(np.sum(A-np.min(A)))


Note that this problem can also be solved in an $$O(1)$$ time.

posted:
last update: