Write a program which reads dimensions of Mi, and finds the minimum number of scalar multiplications to compute the maxrix-chain multiplication M1M2...Mn.
Constraints
1≤n≤100
1≤r,c≤100
Input
In the first line, an integer n is given. In the following n lines, the dimension of matrix Mi (i=1...n) is given by two integers r and c which respectively represents the number of rows and columns of Mi.
Output
Print the minimum number of scalar multiplication in a line.
Example
Input
6
30 35
35 15
15 5
5 10
10 20
20 25
Output
15125
We are given a classic matrix chain multiplication problem: Given n matrices with dimensions r_i, c_i for each matrix M_i (rows r_i, columns c_i). The multiplication of adjacent matrices must be compatible: matrix i has dimensions (r_i, c_i) and matrix i+1 has dimensions (r_{i+1}, c_{i+1}) and we require that c_i = r_{i+1} for multiplication to be defined? Actually typical matrix chain multiplication problem: Each matrix A_i has dimension p_{i-1} x p_i for some array p0, p1,..., pn. But here each matrix is given as r_i (rows) and c_i (columns). The chain multiplication M1 * M2 * ... * Mn is possible only if for each adjacent pair, the column count of the left equals the row count of the right. However in the problem statement they don't explicitly say that the input ensures compatibility? Typically yes, it is implied that c_i = r_{i+1} because otherwise chain multiplication is not defined. So we can assume that c_i = r_{i+1} for i=1..n-1. So we can extract an array p[0..n] where p0 = r1, p1 = c1 = r2, p2 = c2 = r3, ..., p_{n-1} = c_{n-1} = r_n, p_n = c_n. So the chain multiplication cost is defined as the minimal number of scalar multiplications to compute product A_1...A_n given dimensions p_0, p_1, ..., p_n.
We need to compute the minimum number of scalar multiplications.
Constraints: n <= 100, so O(n^3) DP is fine.
We'll implement DP: Let dp[i][j] be min cost to multiply matrices from i to j inclusive (1-indexed). The recurrence: dp[i][i] = 0. For i < j, dp[i][j] = min_{k=i}^{j-1} (dp[i][k] + dp[k+1][j] + p_{i-1} * p_k * p_j). The answer is dp[1][n].
Alternatively, we could use an array p of length n+1. Dimensions: For matrix i, rows = p[i-1], cols = p[i]. So we read r_i, c_i, and set p[i-1] = r_i, and if i==n, we also set p[n] = c_n. But careful: If we read each row/col, we must ensure consistency: For i from 1 to n-1, c_i should equal r_{i+1}. We can just assume that, but we could also verify. We'll just construct p: p[0] = r_1; for i = 1 to n-1, p[i] = c_i (and also r_{i+1} should be same, but we'll assign r_{i+1} from input later). Then after reading last matrix, set p[n] = c_n. Alternatively, we can just store r_i and c_i and treat p[i-1]=r_i, p[i]=c_i. However that would double set p[i] for i=1..n-1 from both c_i and r_{i+1}. But we can trust input consistency. So typical approach: read n, then read first matrix's rows and cols, set p0 = rows, p1 = cols. For each subsequent matrix i (2..n), read rows and cols, set p_i = cols, and verify rows == previous cols? But we can ignore verification and just use p array of length n+1: after reading all, p0 = first rows, for i from 1 to n: p_i = c_i (where c_i is columns of matrix i). But then rows of matrix i is p_{i-1}, so we must have p_{i-1} = r_i. We can read and check: For i=1, we have r_1 = p0 (set before). For i>=2, we need to ensure r_i equals p_{i-1} which is already set to previous columns. The problem likely ensures that. So we can simply do: read n; create array p of size n+1; read r and c for first matrix: p[0] = r; p[1] = c; then for i from 2 to n: read r, c; assert r == p[i-1] (or ignore); p[i] = c.
Alternatively, we can store all r_i and c_i and treat p_i = c_i, and p_{i-1} = r_i. That yields p[0..n] where p[0] = r_1 and p[i] = c_i for i=1..n. This is fine.
Now DP: Use 2D array dp of size n+1 x n+1 (1-indexed for matrices). We can fill using length L from 2 to n (chain length). For L=2 to n: for i=1 to n-L+1: j=i+L-1; dp[i][j] = INF; for k=i to j-1: dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j]). Output dp[1][n].
Time O(n^3) ~ 1e6 operations worst-case, fine.
We need to output integer. Input dimensions up to 100 each, n up to 100, maximum possible cost might be large? Let's compute worst-case: all dimensions could be 100, cost can be O(100 * 100 * 100 * 100) maybe? Actually worst-case cost: If all p_i = 100, then cost for a chain of length n can be huge: The product of a chain multiplication involves many multiplications. Actually the DP will compute the number of scalar multiplications, which is sum over each multiplication step of product of dimensions. For a chain of 100 matrices all 100x100, the optimal multiplication cost is something like (n-1)100^3? Let's consider: Multiplying two 100x100 matrices costs 100^3 = 1,000,000 scalar multiplications (assuming naive algorithm). Multiplying 100 of them: total cost if we multiply sequentially from left to right is: first multiply M1M2 (cost 1e6) yielding 100x100; then resultM3 (cost 1e6) -> 100x100, etc. So total 991e6 = 99,000,000. That's within 32-bit signed? Actually 99e6 < 2^31. But if dimensions are up to 100 and n=100, worst-case could be if the dimensions vary and cause larger intermediate sizes? Since max dimension is 100, any intermediate matrix cannot exceed 100 rows or columns. So maximum per multiplication is at most 100 * 100 * 100 = 1e6. And there are at most n-1 multiplications, so total scalar multiplications <= (n-1)*1e6 <= 99e6. That fits within 32-bit int. But we can also use long long for safety. However the example output is 15125. Since constraints are small, int is fine. But I'll use long long to be safe.
Implementation details: We'll read n, then loop i=0 to n-1? Let's decide indexing. We'll use 0-index for p array of length n+1, but DP indices for matrices often 1-indexed. We can use 0-index for matrices too: dp[i][j] for i from 0 to n-1, j from i to n-1. Then recurrence: dp[i][i]=0; for len>=2, dp[i][j] = min_{k=i}^{j-1} dp[i][k] + dp[k+1][j] + p[i]*p[k+1]*p[j+1]? Wait careful. If we use 0-index matrices: matrix i has dimensions p[i] x p[i+1]. Then chain from i to j inclusive (i<=j) has dimensions p[i] x p[j+1]. The cost: dp[i][j] = min_{k=i}^{j-1} dp[i][k] + dp[k+1][j] + p[i] * p[k+1] * p[j+1]. Because when we split at k, left part i..k yields dimension p[i] x p[k+1]; right part k+1..j yields p[k+1] x p[j+1]; multiply these gives p[i]*p[k+1]*p[j+1] scalar multiplications. That's the typical recurrence.
Alternatively, use 1-index for matrices: matrix i dimensions p[i-1] x p[i]; chain from i to j: dp[i][j] = min_{k=i}^{j-1} dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j]. That is simpler perhaps.
I'll do 1-index for matrices for clarity, with p array of length n+1 (0-index for p). We'll read first matrix: set p[0] and p[1]; then for i from 2 to n: read r, c, set p[i] = c (assuming r equals previous p[i-1]). So p indices 0..n.
Then DP: define dp as 2D vector of size (n+1) x (n+1) (we only need i <= j). Initialize dp[i][i] = 0.
for length from 2 to n:
for i from 1 to n-length+1:
j = i+length-1;
dp[i][j] = INF;
for k from i to j-1:
cost = dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j];
if cost < dp[i][j] -> update.
Return dp[1][n].
Complexities O(n^3). Use long long INF = 9e18 or something. But since maximum is <1e8, we can set INF = 1e18.
Edge cases: n=1 -> no multiplication, output 0. The DP loop for length from 2 to n will not run, dp[1][1] = 0. So output 0. That's valid.
Now we need to write the program in C++ (I assume). The prompt says "Write a program", but doesn't specify language. Usually they expect C++ or Java, but we can output in C++.
We'll produce final answer: code with proper reading, DP, output.
But there may be nuance: The problem input may not guarantee that c_i = r_{i+1}? The problem statement: "reads dimensions of M_i, and finds the minimum number of scalar multiplications to compute the matrix-chain multiplication M_1 M_2 ... M_n." Usually it implies the multiplication is possible, so the dimensions are compatible. So we assume that.
Let's test with example: n=6; dimensions: (30,35),(35,15),(15,5),(5,10),(10,20),(20,25). Then p: p0=30,p1=35,p2=15,p3=5,p4=10,p5=20,p6=25. DP yields 15125. Good.
Implementation in C++:
code
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> p(n+1);
int r, c;
cin >> r >> c;
p[0] = r;
p[1] = c;
for (int i = 2; i <= n; ++i) {
cin >> r >> c;
// Assuming r equals p[i-1]; we could ignore
p[i] = c;
}
// DP
vector<vector<long long>> dp(n+1, vector<long long>(n+1, 0));
for (int len = 2; len <= n; ++len) {
for (int i = 1; i <= n - len + 1; ++i) {
int j = i + len - 1;
long long min_cost = LLONG_MAX; // or 1e18
for (int k = i; k < j; ++k) {
long long cost = dp[i][k] + dp[k+1][j] + p[i-1] * p[k] * p[j];
if (cost < min_cost) min_cost = cost;
}
dp[i][j] = min_cost;
}
}
cout << dp[1][n] << endl;
return 0;
}
But note: When n=1, p[1] is set from first matrix's columns. Then dp[1][1] = 0 (since we initialized dp with zeros). But we have dp size (n+1)x(n+1) with all zeros, so dp[1][1] = 0. So output 0. That's fine.
But we might want to explicitly set dp[i][i]=0 for all i, but it's already zero due to vector initialization. That's okay.
However, for n=1, we output dp[1][1] which is 0. Works.
Edge: n=0? Not allowed, as n>=1.
Potential overflow: p[i-1]p[k]p[j] might be up to 100100100 = 1e6, times dp sums up to at most 99e6, all within 32-bit. But using long long is safe.
One more nuance: The DP initialization: dp as vector of vector initialized to 0 is fine. But we only need to fill for i<j, and dp[i][i] stays 0.
Time complexity: O(n^3) with n<=100, fine.
But there is a subtlety: if n is 100, dp matrix size is 101x101 = 10201, fine.
Thus answer seems correct.
But maybe the problem expects reading all rows and columns and then deriving p as: p[0] = r_1; for i=1 to n-1, p[i] = c_i; and p[n] = c_n? Wait, careful: For matrix i, dimensions are r_i (rows) and c_i (cols). Chain multiplication requires c_i = r_{i+1}. So we could define array p where p_0 = r_1, p_1 = c_1 = r_2, p_2 = c_2 = r_3, ..., p_{n-1} = c_{n-1} = r_n, p_n = c_n. So there are n+1 numbers. So after reading all matrices, we can construct p accordingly. My method of reading first matrix sets p0 and p1; then for each subsequent matrix, I read r,c and set the next p_i = c. But I also need to ensure that p_i-1 (which is c_{i-1}) equals r_i. But the input may give r_i and c_i; to correctly build p, we could simply set p[0] = r_1; then for i=1..n-1, set p[i] = c_i? Actually if we set p[0] = r_1; then for i=1 to n-1: after reading first matrix we already have p[1]=c_1. Then for i=2: we read r_2,c_2; we must set p[2] = c_2 and ensure r_2 == p[1] (c_1). That's fine. So after reading all, p[0] = r_1; p[1] = c_1; p[2] = c_2; ... p[n] = c_n. However note that p[n] is c_n, but according to the definition, p_n = c_n, and there are n+1 numbers: p_0..p_n. So it's fine.
But is p[1] correctly set as c_1? Yes. For matrix 1, its dimensions: p[0] x p[1]. For matrix 2, its dimensions should be p[1] x p[2]. So after reading matrix 2, we set p[2]=c_2, and indeed p[1] (c_1) should equal r_2. So if the input is consistent, it works.
Thus the code is correct.
One more point: The problem statement might expect that you compute p from all rows and columns by setting p[i-1] = r_i for i=1..n, and p[n] = c_n. That yields same array: p[0]=r_1, p[1]=r_2, ..., p[n-1]=r_n, p[n]=c_n. But we need to also have c_i = r_{i+1} ensures p[i] = r_{i+1} = c_i? Actually if we set p[i-1] = r_i, then for i from 1..n, p[0]=r_1, p[1]=r_2, ..., p[n-1]=r_n. Then also set p[n]=c_n. The condition for multiplication is that for each i, c_i = r_{i+1}. That means c_i = p[i] because p[i] = r_{i+1}. So indeed p[i] = c_i for i=1..n-1, and p[n]=c_n. So both representations are equivalent. So either way we can just read all matrices into two arrays and then construct p. Simpler: read r and c for each matrix; set p[i] = r_i for i from 0 to n-1? Wait, careful.
Actually easiest: create p of size n+1. For i=0..n-1, read r_i+1? Let's think:
We could read all into arrays rows[1..n] and cols[1..n]. Then set p[0] = rows[1]; for i=1..n-1: p[i] = cols[i] (which should equal rows[i+1]); and p[n] = cols[n]. That is consistent.
But we can also set p[0] = rows[1]; for i=1..n: p[i] = cols[i]. That yields same, but requires rows[i] = p[i-1] (ensured by input). So I'll keep my approach.
Thus final answer: Provide code as above.
However, the prompt might be from an online judge like AOJ or similar, and they might want a function, but we'll output full main.
Also note that we might want to handle input where n=0? Not needed.
Let's produce final answer with code and maybe brief explanation. But the instruction: "Write a program ..." So final output is code. Usually in these tasks they expect just the code, but as a ChatGPT answer we can provide explanation and then code.
I'll produce the final answer with a concise code snippet in C++.
But careful: I used LLONG_MAX, which might be defined in . That's fine.
Alternatively, we can define INF as a large number like 1e18.
We'll do:
code
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> p(n+1);
for (int i = 0; i < n; ++i) {
int r, c;
cin >> r >> c;
if (i == 0) p[0] = r;
p[i+1] = c;
}
vector<vector<long long>> dp(n+1, vector<long long>(n+1, 0));
for (int len = 2; len <= n; ++len) {
for (int i = 1; i <= n-len+1; ++i) {
int j = i + len - 1;
dp[i][j] = LLONG_MAX;
for (int k = i; k < j; ++k) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i-1] * p[k] * p[j]);
}
}
}
cout << dp[1][n] << endl;
return 0;
}
This code reads all matrices and fills p: p[0] = first matrix rows; for each i, set p[i+1] = columns. That's simpler. For i=0 (first matrix), we set p[0] = r; also p[1] = c; for i=1 (second), p[2] = c; etc. This works.