2
购物
http://www.nowcoder.com/questionTerminal/6254d6801b104c84bdf32f5ce28b9908
include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int book[1005][1005];
ll tp[1000];
ll e[1005][1005];
ll jj[10000];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
tp[i] = 1;
jj[i] = 1;
for(int j = 1; j <= m; j++)
{
cin >> e[i][j];
}
}
ll sum = 0;
for(int i = 1; i <= n; i++)
{
int x, y;
ll minn = 0x3f3f3f3f3f3f3f3f;
for(int j = 1; j <= i; j++)
{
for(int k = 1; k <= m; k++)
{
if(book[j][k] == 0)
{
if(e[j][k] + jj[j] < minn)
{
minn = e[j][k] + jj[j];
x = j;
y = k;
}
}
}
}
//cout << minn << "\n";
sum += minn;
book[x][y] = 1;
ll c = tp[x];
jj[x] = (c + 1) * (c + 1) - c * c;
tp[x]++;
}
cout << sum << "\n";
return 0;
}