Cod sursa(job #3251542)

Utilizator AlkacineLezau Andrei Ianis Alkacine Data 26 octombrie 2024 10:54:24
Problema Parantezare optima de matrici Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("file.in");
ofstream fout("file.out");

bool cmp(pair<int, int> &a, pair<int, int> &b)
{

    if (a.second > b.second)
    return a.second > b.second;

    else if (a.second == b.second)
        return a.first > b.first;
}


int main()
{
    int n;
    fin >> n;


    int a[n + 1];

    int x;
    for (int i = 0; i <= n; i++)
    {
        fin >> a[i];

    }

    n++;

    long long int cnt = 0;
    while (n > 2)
    {
        int ma = -1e9;
        int poz = 0;
        for (int i = 1 ; i < n - 1; i++)
            if (a[i] > ma ) {ma = a[i]; poz = i;}

        //cout << a[poz - 1] << ' ' << a[poz]    << "      " << a[poz] << ' ' << a[poz + 1] << endl;



        cnt += a[poz - 1] * a[poz] * a[poz + 1];
        for (int j = poz; j + 1 <n; j++)
            a[j] = a[j + 1];

        n--;

        //cout << "11111111111" << endl;
        //for (auto i : a) cout << i << ' ';
        //cout << endl << "1111111111111111" << endl;
    }


    fout << cnt;

}