Advertisement
Josif_tepe

Untitled

Apr 10th, 2023
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. typedef long long ll;
  5. ll arr[111], dp[111][111];
  6. ll sum(int i, int j) {
  7.     ll result = 0;
  8.     for(int k = i; k <= j; k++) {
  9.         result += arr[k];
  10.         result %= 100;
  11.     }
  12.     return result;
  13. }
  14. ll rec(int i, int j) {
  15.     if(i >= j) {
  16.         return 0;
  17.     }
  18.     if(i + 1 == j) {
  19.         return arr[i] * arr[j];
  20.     }
  21.     if(dp[i][j] != -1) {
  22.         return dp[i][j];
  23.     }
  24.     ll result = 2e15;
  25.     for(int k = i; k < j; k++) {
  26.         result = min(result, rec(i, k) + rec(k + 1, j) + sum(i, k) * sum(k + 1, j));
  27.     }
  28.     return dp[i][j] = result;
  29. }
  30. int main() {
  31.     ios_base::sync_with_stdio(false);
  32.    
  33.     int n;
  34.     while(cin >> n) {
  35.  
  36.         for(int i = 0; i < n; i++) {
  37.             cin >> arr[i];
  38.         }
  39.         memset(dp, -1, sizeof dp);
  40.         cout << rec(0, n - 1) << endl;
  41.     }
  42.     return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement