Sanath12345

Untitled

May 15th, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. //Problem link :
  2. //done by sanath kumar
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define ll long long int
  6. #define ld long double
  7. #define mod 1000000007
  8. #define inf 1e9
  9. #define endl "\n"
  10. #define pb push_back
  11. #define vi vector<ll>
  12. #define vs vector<string>
  13. #define pii pair<ll,ll>
  14. #define ump unordered_map
  15. #define mp make_pair
  16. #define pq_max priority_queue<ll>
  17. #define pq_min priority_queue<ll,vector<ll>,greater<ll>>
  18. #define all(v) v.begin(),v.end()
  19. #define ff first
  20. #define ss second
  21. #define mid(l,r) (l+(r-l))/2
  22. #define bitc(x) __builtin_popcount(x)
  23. #define loop(i,a,b) for(int i=(a);i<=(b);i++)
  24. #define looprev(i,a,b) for(int i=(a);i>=(b);i--)
  25. #define iter(c,it) for(__typeof(c.begin()) it=c.begin();it!=c.end();it++)
  26. #define log(args...) {string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);err(_it,args);}
  27. #define logarr(arr,a,b) for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl;
  28. template<typename T> T gcd(T a,T b) {if(b==0) return a; return gcd(b,a%b);}
  29. template<typename T> T lcm(T a,T b) {return a*(b/gcd(a,b));}
  30.  
  31. void err(istream_iterator<string> it){}
  32. template<typename T,typename... Args>
  33. void err(istream_iterator<string> it,T a,Args... args){
  34. cout<<*it<<" = "<<a<<endl;
  35. err(++it,args...);
  36. }
  37.  
  38. void file_i_o(){
  39. ios_base::sync_with_stdio(false);
  40.  
  41. cin.tie(0);
  42. cout.tie(0);
  43. #ifndef ONLINE_JUDGE
  44. freopen("input.txt","r",stdin);
  45. freopen("output.txt","w",stdout);
  46. #endif
  47. }
  48. // ll solve(int *a,int n,int k){
  49.  
  50. // }
  51. int main(){
  52. file_i_o();
  53. clock_t begin=clock();
  54. int n;
  55. cin>>n;
  56. vi a(n+5);
  57. loop(i,1,n) cin>>a[i];
  58. // cout<<solve(a,n,i)<<endl;
  59. vi dp1(n+5),dp2(n+5),sum1(n+5),sum2(n+5);
  60. dp1[1]=0;
  61. sum1[1]=a[1];
  62. loop(i,2,n){
  63. ll opt1=2*(a[i]+a[i-1])+sum1[i-2]+dp1[i-2];
  64. ll opt2=sum1[i-1]+a[i]+dp1[i-1];
  65. dp1[i]=min(opt1,opt2);
  66. sum1[i]=sum1[i-1]+a[i];
  67. }
  68. dp2[n]=0;
  69. sum2[n]=a[n];
  70. looprev(i,n-1,1){
  71. ll opt1=2*(a[i]+a[i+1])+sum2[i+2]+dp2[i+2];
  72. ll opt2=sum2[i+1]+a[i]+dp2[i+1];
  73. dp2[i]=min(opt1,opt2);
  74. sum2[i]=sum2[i+1]+a[i];
  75. }
  76. ll ans=LLONG_MAX;
  77. loop(i,1,n){
  78. ans=min(ans,dp1[i]+dp2[i+1]+sum1[n]);
  79. }
  80. cout<<ans<<endl;
  81. #ifndef ONLINE_JUDGE
  82. clock_t end=clock();
  83. cout<<"\n\n\nExecuted in :"<<double(end-begin)/CLOCKS_PER_SEC*1000<<" ms"<<endl;
  84. #endif
  85. }
Advertisement
Add Comment
Please, Sign In to add comment