Cod sursa(job #168278)

Utilizator toni2007Pripoae Teodor Anton toni2007 Data 30 martie 2008 22:57:43
Problema Orase Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <stdio.h>
#include <stdlib.h>
#define N 50010
struct distanta{
       int d,l;
};
distanta v[N];
int cmb[N];
int swap(distanta &i,distanta &j){
    distanta aux;
    aux=i;
    i=j;
    j=aux;
}
void down_heap(int i,int n,distanta h[]){
     int max=i;
     if (2*i<=n && h[max].d<h[2*i].d)
        max=2*i;
     if (2*i+1<=n && h[max].d<h[2*i+1].d)
        max=2*i+1;
     if (i!=max){
        swap(h[i],h[max]);
        down_heap(max,n,h);
     }
}
void sort(int n){
     int i;
     for (i=n/2;i>=1;--i)
         down_heap(i,n,v);
     for (i=n;i>=1;--i){
         swap(v[1],v[i]);
         down_heap(1,i-1,v);
     }
}
int main(){
    int n,m,i,j,k,bestk,best,maxim=0;
    freopen("orase.in","r",stdin);
    freopen("orase.out","w",stdout);
    scanf("%d%d",&m,&n);
    for (i=1;i<=n;++i)
        scanf("%d%d",&v[i].d,&v[i].l);
    sort(n);
    cmb[1]=v[1].l-v[1].d;
    for (i=2;i<=n;++i){
        if (v[i].l-v[i].d>cmb[i-1])
           cmb[i]=v[i].l-v[i].d;
        else cmb[i]=cmb[i-1];
    }
    j=1;
    for (i=1;i<=n;++i){
        if (v[i].l+v[i].d+cmb[i-1]>maxim)
           maxim=v[i].l+v[i].d+cmb[i-1];
    }
    printf("%d\n",maxim);
    return 0;
}