Cod sursa(job #2948895)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 28 noiembrie 2022 18:39:19
Problema Parantezare optima de matrici Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
using ll = long long;

using namespace std;

const int NMAX = 505;
/*******************************/
// INPUT / OUTPUT

ifstream f("podm.in");
ofstream g("podm.out");
/*******************************/
/// GLOBAL DECLARATIONS

int N;
int v[NMAX];
ll dp[NMAX][NMAX];
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    f >> N;

    for (int i = 1 ; i <= N + 1 ; ++ i) f >> v[i];
}
///-------------------------------------
inline void Solution()
{
    int j;
    for (int len = 2 ; len <= N ; ++ len)
    {
        for (int i = 1 ; i <= N - len + 1 ; ++ i)
        {
            j = i + len - 1;
            dp[i][j] = INT64_MAX;
            for (int k = i ; k < j ; ++ k)
            {
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + 1LL * v[i] * v[j + 1] * v[k + 1]);
            }
        }
    }
}
///-------------------------------------
inline void Output()
{
    g << dp[1][N];
}
///-------------------------------------
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ReadInput();
    Solution();
    Output();
    return 0;
}