Advertisement
MaskerQwQ

A - Three displays

Apr 30th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N=1e9+7;
  6.  
  7. int a[3005],b[3005];
  8.  
  9. int n;
  10.  
  11. int main()
  12. {
  13.     cin>>n;
  14.     for(int i=1;i<=n;i++){
  15.         cin>>a[i];
  16.     }
  17.     for(int i=1;i<=n;i++){
  18.         cin>>b[i];
  19.     }
  20.     int ans=N;
  21.     for(int i=1;i<=n;i++){
  22.         int p=N,q=N;
  23.         for(int j=1;j<i;j++){
  24.             if(a[j]<a[i]){
  25.                 p=min(p,b[j]);
  26.             }
  27.         }
  28.         for(int j=i+1;j<=n;j++){
  29.             if(a[j]>a[i]){
  30.                 q=min(q,b[j]);
  31.             }
  32.         }
  33.         ans=min(ans,p+q+b[i]);
  34.     }
  35.     if(ans==N){
  36.         cout<<"-1"<<endl;
  37.     }else{
  38.         cout<<ans<<endl;
  39.     }
  40.    
  41.    
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement