Cod sursa(job #2413247)

Utilizator lucametehauNamintratinlotXD lucametehau Data 23 aprilie 2019 10:33:06
Problema Ferma2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>

using namespace std;

ifstream cin ("ferma2.in");
ofstream cout ("ferma2.out");

int n, k, x, s, sum, sol, mn;

int sl[1005][1005], sc[1005][1005], sd[1005][1005];

int main() {
  cin >> n >> k;
  k = n - k;
  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= i; j++) {
      cin >> x;
      sol += x;
      sl[i][j] = sl[i][j - 1] + x;
      sc[i][j] = sc[i - 1][j] + x;
      sd[i][j] = sd[i - 1][j - 1] + x;
    }
  }
  for(int i = 1; i <= k; i++)
    sum += sl[i][i];
  mn = s = sum;
  for(int i = 2; i <= n - k + 1; i++) {
    s += sl[i + k - 1][k] - sd[i + k - 2][k];
    sum = s;
    mn = min(mn, s);
    for(int j = 2; j <= i; j++) {
      sum += sd[i + k - 1][j + k - 1] - sd[i - 1][j - 1] - sc[i + k - 1][j - 1] + sc[i - 1][j - 1];
      mn = min(mn, sum);
    }
  }
  sol -= mn;
  cout << sol;
  return 0;
}