"Link" I am the link, point me:)
Test instructions
Exercises
Dynamic planning
Set Dp[i][j] represents the first I number, the minimum cost of J is selected.
Dp[i][j] = min (dp[k][j-1]+b[i]);//Where a[i]>a[k] and k<i
This is actually where the enumeration of the selected J-1 is located.
Obviously only in front of it, and satisfies a[i]>a[k] position k is possible.
Complexity O (n^2)
Code
#include <bits/stdc++.h>using namespace std;const int N = 3e3;const int INF = 4e8;int N;int a[n+10],b[n+10];in T Dp[n+10][4];int Main () {#ifdef local_define freopen ("Rush.txt", "R", stdin); #endif//Local_define ios::sync_with_stdio (0), Cin.tie (0); CIN >> N; for (int i = 1;i <= n;i++) cin >> A[i]; for (int i = 1;i <= n;i++) cin >> B[i]; for (int i = 1;i <= 3;i++) for (int j = 1;j <= n;j++) dp[j][i] = INF; for (int i = 1;i <= n;i++) dp[i][1] = B[i]; for (int j = 2;j <= 3;j++) for (int i = J;i <= n;i++) for (int k = j-1;k <= i-1;k++) if (A[i]>a[k] && dp[i][j]>dp[k][j-1]+b[i]) {dp[i][j] = Dp[k][j-1]+b[i]; } int ans = INF; for (int i = 3;i <= n;i++) ans = min (ans,dp[i][3]); if (Ans==inf) cout<<-1<<endl; else cout<<ans<<endl; return 0;}
"Codeforces 987C" three displays