# Cod sursa(job #636657)

Utilizator Data 19 noiembrie 2011 22:19:55 Ferma2 30 cpp done .com 2011 1.58 kb
``````#include <stdio.h>
#include <vector>
using namespace std;
#define maxn 1005
vector <int> tri[maxn];//tine triunghiul initial

int n,k;

int suma1(int foaia, int l2,int l3){//foile sunt num incepand cu 1
//latura 1
int i;
int s=0;
int li=foaia,ls=n-l2;
for(i=li;i<=ls;i++){
s+=tri[i][foaia-1];
}
return s;
}

int suma2(int foaia,int l1, int l3){
int i;
int s=0;
int li=l1,ls=n-l3-foaia+1;
for(i=li;i<ls;i++){
s+=tri[n-foaia+1][i];
}
return s;

}

int suma3(int foaia, int l1, int l2){
int i;
int s=0;
int li=l1+foaia,ls=n-l2;
for(i=li;i<=ls;i++){
s+=tri[i][i-foaia];
}
return s;
}

int main(){
FILE *fin=fopen("ferma2.in","r");
FILE *fout=fopen("ferma2.out","w");
fscanf(fin,"%d%d",&n,&k);
int a;
int i,j;
for(i=1;i<=n;i++){
for(j=0;j<i;j++){
fscanf(fin,"%d",&a);
tri[i].push_back(a);
}
}
//descompun k in suma de trei numere
int l1,l2,l3;
int maximulm=-1;
for(l1=0;l1<=k;l1++)
for(l2=0;l2<=(k-l1);l2++){
l3=k-l1-l2;
//printf("(%d,%d,%d), ",l1,l2,l3);
a=0;
//iau l1 laturi 1....
for(i=1;i<=l1;i++)a+=suma1(i,0,0);
//iau l2 laturi 2...
for(i=1;i<=l2;i++)a+=suma2(i,l1,0);
//iau l3 laturi 3...
for(i=1;i<=l3;i++)a+=suma3(i,l1,l2);

if(a>maximulm)maximulm=a;
}
//printf("\n");

fprintf(fout,"%d\n",maximulm);
/*printf("suma1(%d,%d,%d)=%d\n",1,0,0,suma1(1,0,0));
printf("suma3(%d,%d,%d)=%d\n",1,1,0,suma3(1,1,0));
printf("suma3(%d,%d,%d)=%d\n",2,1,0,suma3(2,1,0));
*/
return 0;
}

``````