#include<bits/stdc++.h> #define ll long long int #define mp make_pair #define pb push_back using namespace std; ll a[200000]; int main() { ll n,i,s,z,x,j,pos; cin>>n; for(i=0;i<200000;i++) { a[i]=100000; } s=0; for(i=0;i<n;i++) { cin>>x; a[i]=x; } if((a[0]<=a[1])&&(a[0]<=a[2])) { i=0; } else if((a[1]<=a[2])&&(a[1]<=a[0])) { i=1; } else i=2; //cout<<i<<" "; s=s+a[i]; for(;i<n;) { if((a[i+1]<=a[i+2])&&(a[i+1]<=a[i+3])) { pos=i+1; } else if((a[i+2]<=a[i+1])&&(a[i+2]<=a[i+3])) { pos=i+2; } else pos=i+3; //cout<<a[pos]<<" "<<pos<<" "; i=pos; s=s+a[pos]; if(i>=(n-2)) break; } cout<<s; return 0; }
Asked by: Manish_Kumar_Savita on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.
the intuition behind your code is not correct. so, try to fix it.
note1 :) you should improve your skills regarding how to write code.
note2 :) you should explain the working of your program or how you think to solve the problem instead of just posting the code.
note 3:) always try some random test cases and corner cases before submitting the solution and don't just stick with test cases.