Cod sursa(job #125499)

Utilizator mariusdrgdragus marius mariusdrg Data 20 ianuarie 2008 14:07:28
Problema Inundatii Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include<stdio.h>
#define LL long long

const int maxd = 4;
const int maxn = 50010;

LL sps[maxd];
LL a[maxd][maxn];
LL ans[maxd];
const LL inf = ((LL)1 << 55);
int i,j,n;
LL spd[maxd];

LL calc(int x)
{
        LL aux;
        if (i < n - i) aux = (LL)i * (i + 1) / 2 + (n - i) * (n - i - 1) / 2;
        else aux = (LL) i * (i - 1) / 2 + (n - i) * (n - i + 1) / 2;
        LL ans = (LL)  sps[j] - x * i + (n - i + 1) * x - spd[j] + aux;
        if (ans < 0) return inf;
        return ans;
}


int main()
{
        freopen("inundatii.in","r",stdin);
        freopen("inundatii.out","w",stdout);
        scanf("%d",&n);
  //      printf("%d\n",sizeof(a) + sizeof(sps) + sizeof(spd));
        for(i = 1;i <= n; ++i)
        {
                scanf("%lld %lld %lld",&a[1][i],&a[2][i],&a[3][i]);
        }               /*
        for(i = 1;i <= n; ++i)
        {
                sps[1][i] = sps[1][i - 1] + a[1][i];
                sps[2][i] = sps[2][i - 1] + a[2][i];
                sps[3][i] = sps[3][i - 1] + a[3][i];
        }                 */
        for(i = n;i > 0; --i)
        {
                spd[1] += a[1][i];
                spd[2] += a[2][i];
                spd[3] += a[3][i];
        }
        
        for(j = 1;j <= 3; ++j) ans[j] = inf;
        for(i = 1;i <= n; ++i)
        {
                 for(j = 1;j <= 3; ++j)
                 {
                        sps[j] += a[j][i];
                        if (ans[j] > calc(a[j][i])) ans[j] = calc(a[j][i]);
                        spd[j] -= a[j][i];
                 
                 }
        }

        
        printf("%lld\n",(LL)ans[1] + ans[2] + ans[3]);

        return 0;
}