#include<iostream>
#include<vector>
using namespace std;
void matrix_chain_order(vector<int> p)
{
int n=p.size()-1;
vector<vector<int>> m(n+1,vector<int>(n+1));
vector<vector<int>> s(n,vector<int>(n));
for(int i=1;i<=n;i++)
{
m[i][i]=0;
}
for(int l=2;l<=n;l++)
{
for(int i=1;i<=n-l+1;i++)
{
int j=i+l-1;
m[i][j]=INT_MAX;
for(int k=i;k<=j-1;k++)
{
int q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
if(q<m[i][j])
{
m[i][j]=q;
s[i][j]=k;
}
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
cout << "m[" << i << "," << j << "]" << "=" << m[i][j] << " ";
}
cout << endl;
}
cout << endl;
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
cout << "s[" << i << "," << j << "]" << "=" << s[i][j] << " ";
}
cout << endl;
}
}
int main()
{
vector<int> p={5,10,3,12,5,50,6};
matrix_chain_order(p);
return 0;
}