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)


int a[110][110];
int main(){
	int 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;

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)

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

last update: