# Help me with this problem

**URL:** <https://discuss.codechef.com/t/help-me-with-this-problem/90665>\
**Category:** help\
**Tags:** dynamic-programming\
**Created:** [June 3, 2021, 5:47am UTC](https://discuss.codechef.com/t/help-me-with-this-problem/90665 "2021-06-03T05:47:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilovevanshita](https://discuss.codechef.com/user_avatar/discuss.codechef.com/ilovevanshita/32/208155_2.png) [@ilovevanshita](https://discuss.codechef.com/u/ilovevanshita)\
**Post date:** [June 3, 2021, 5:47am UTC](https://discuss.codechef.com/t/help-me-with-this-problem/90665/1 "2021-06-03T05:47:04Z")

</div>

```auto
Plz. tell me what i am doing wrong in this problem it is given wrong answer on single test case.

#include<bits/stdc++.h>
using namespace std;  
#define ll long long
#define endl "\n" 
const int mod = 1e9+7;    
const int N = 1e6 + 2;      
  int dp[N];
  int a[N];
int main(){   
    ios_base::sync_with_stdio(false);     
       cin.tie(NULL);cout.tie(NULL);   
       int t ;
       cin>>t;
       while(t--){
     ll n;
     cin>>n;
     for(int i = 0 ; i < n ;i++){
      cin>>a[i];
     }
     stack<ll>st;   
     dp[0] = 1;  
     st.push(a[0]);       
     for(int i = 1; i < n; i++){
      if(!st.empty() && st.top() <= a[i]){
        st.push(a[i]);
        dp[i] = dp[i - 1] + st.size();
      }
      else{    
        while(!st.empty()){
          st.pop();
        }
        st.push(a[i]);
        dp[i] = dp[i - 1] + st.size();   
      }
     }        
     cout<<dp[n - 1]<<endl;
   }   
}

```

---

<div class="post-metadata">

**Author:** ![ssjgz](https://discuss.codechef.com/user_avatar/discuss.codechef.com/ssjgz/32/346011_2.png) [@ssjgz](https://discuss.codechef.com/u/ssjgz)\
**Post date:** [June 3, 2021, 5:48am UTC](https://discuss.codechef.com/t/help-me-with-this-problem/90665/2 "2021-06-03T05:48:03Z")

</div>

Please either [format your code](https://discuss.codechef.com/t/tutorial-color-format-the-code/36585) or (better!) link to your submission - the forum software has mangled it and it won’t compile! 🙂

---

<div class="post-metadata">

**Author:** ![ilovevanshita](https://discuss.codechef.com/user_avatar/discuss.codechef.com/ilovevanshita/32/208155_2.png) [@ilovevanshita](https://discuss.codechef.com/u/ilovevanshita)\
**Post date:** [June 3, 2021, 5:49am UTC](https://discuss.codechef.com/t/help-me-with-this-problem/90665/3 "2021-06-03T05:49:05Z")

</div>

sure

---

<div class="post-metadata">

**Author:** ![garvit2013](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@garvit2013](https://discuss.codechef.com/u/garvit2013)\
**Post date:** [June 3, 2021, 6:18am UTC](https://discuss.codechef.com/t/help-me-with-this-problem/90665/4 "2021-06-03T06:18:03Z")

</div>

Can ypu please help me why my code is giving TLE?

Problem:[CodeChef: Practical coding for everyone](https://www.codechef.com/START4A/problems/SUBCENTR)

LCA concept: [LCA in a tree using Binary Lifting Technique - GeeksforGeeks](https://www.geeksforgeeks.org/lca-in-a-tree-using-binary-lifting-technique/)  
And rest concept of Editorialsit’s solutions

Code: [CodeChef: Practical coding for everyone](https://www.codechef.com/viewsolution/47337440)

Thanks
