Advertisement
Saleh127

Light OJ 1169 / DP

Aug 28th, 2022
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. /***
  2.  created: 2022-08-28-13.37.36
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  13. #define get_lost_idiot return 0
  14. #define nl '\n'
  15.  
  16. ll a[2][10005],n;
  17. ll dp[2][10005];
  18. ll f1[2][20005];
  19.  
  20. ll solve(ll in ,ll fl)
  21. {
  22.     if(in==n) return 0;
  23.     if(dp[fl][in]!=-1) return dp[fl][in];
  24.     ll ans=INT_MAX;
  25.     ans=min(solve(in+1,fl)+a[fl][in] , solve(in+1,fl^1)+f1[fl][in]+a[fl][in]);
  26.     return dp[fl][in]=ans;
  27. }
  28.  
  29. int main()
  30. {
  31.     ios_base::sync_with_stdio(0);
  32.     cin.tie(0);
  33.     cout.tie(0);
  34.  
  35.     test
  36.     {
  37.         ll i,j,k,l;
  38.  
  39.         cin>>n;
  40.  
  41.         memset(dp,-1,sizeof dp);
  42.  
  43.         for(i=0;i<2;i++)
  44.         {
  45.             for(j=0;j<n;j++)
  46.             {
  47.                 cin>>a[i][j];
  48.             }
  49.         }
  50.  
  51.         for(i=0;i<2;i++)
  52.         {
  53.             for(j=0;j<n-1;j++)
  54.             {
  55.                 cin>>f1[i][j];
  56.             }
  57.         }
  58.  
  59.         ll ans=solve(0,0);
  60.         memset(dp,-1,sizeof dp);
  61.         ans=min(ans,solve(0,1));
  62.  
  63.         cout<<"Case "<<cs<<": "<<ans<<nl;
  64.     }
  65.     get_lost_idiot;
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement