YEZAELP

o60_may08_guesscost

Oct 18th, 2021
615
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int N = 300 + 10;
  5. const int INF = 1e9;
  6. int ar[N], dp[N][N];
  7.  
  8. int f(int i, int j){
  9.     if(i > j) return 0;
  10.     else if(i == j) return dp[i][j] = ar[i];
  11.     else if(j == i + 1) return dp[i][j] = ar[i] + ar[j];
  12.     if(dp[i][j] != INF) return dp[i][j];
  13.     for(int k=i+1;k<=j-1;k++)
  14.         dp[i][j] = min(dp[i][j], ar[k] + max(f(i, k-1), f(k+1, j)));
  15.     return dp[i][j];
  16. }
  17.  
  18. int main(){
  19.  
  20.     int n;
  21.     scanf("%d", &n);
  22.  
  23.     for(int i=1;i<=n;i++) scanf("%d", &ar[i]);
  24.  
  25.     for(int i=1;i<=n;i++)
  26.         dp[i][i] = ar[i];
  27.  
  28.     for(int i=n;i>=1;i--){
  29.         for(int j=i+1;j<=n;j++){
  30.             dp[i][j] = INF;
  31.             for(int k=i;k<=j;k++)
  32.                 dp[i][j] = min(dp[i][j], ar[k] + max(dp[i][k-1], dp[k+1][j]));
  33.         }
  34.     }
  35.  
  36.     printf("%d", dp[1][n]);
  37.  
  38.     return 0;
  39. }
RAW Paste Data