Cod sursa(job #2797221)

Utilizator Casian_doispeChiriac Casian Casian_doispe Data 9 noiembrie 2021 16:32:58
Problema Parantezare optima de matrici Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

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

long long v[509] ;

/// cautam cel mai mare element mereu si il scoatem parantezandul

int cauta_max(int n)
{
    int nmx = 2 ;

    for(int f = 2 ; f < n ; f ++)
        if(v[f] > v[nmx])nmx = f ;

    return nmx ;
}

void scoate(int poz, int &n)
{
    for(int f = poz ; f <= n ; f ++)
        v[f] = v[f + 1] ;

    n -- ;
}

int main()
{
    int n ;

    cin >> n ;

    n ++ ;

    for(int f = 1 ; f <= n ; f ++)
        cin >> v[f] ;

    long long rez = 0 ;

    while(n > 2)
    {
        int poz = cauta_max(n) ;

        rez += v[poz] * v[poz - 1] * v[poz + 1] ;

        scoate(poz, n) ;
    }

    cout << rez ;

    return 0;
}