<p>There are <code>n</code> children standing in a line. Each child is assigned a rating value given in the integer array <code>ratings</code>.</p>

<p>You are giving candies to these children subjected to the following requirements:</p>

<ul>
	<li>Each child must have at least one candy.</li>
	<li>Children with a higher rating get more candies than their neighbors.</li>
</ul>

<p>Return <em>the minimum number of candies you need to have to distribute the candies to the children</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> ratings = [1,0,2]
<strong>Output:</strong> 5
<strong>Explanation:</strong> You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> ratings = [1,2,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == ratings.length</code></li>
	<li><code>1 &lt;= n &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= ratings[i] &lt;= 2 * 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    int candy(vector<int>& arr) {
        // max == 1
        // min == -1
        // mind == 0
        // maxd == 2
        int n = arr.size();
        if(n==1) return 1;
        vector<pair<int,int>> v;
        vector<int> slopes;
        if(arr[0]>arr[1]) {v.push_back({0,1}); slopes.push_back(-1);}
        else if(arr[0]<arr[1]) {v.push_back({0,-1}); slopes.push_back(1);}
        else {v.push_back({0,0}); slopes.push_back(0);}
        for(int i=1;  i<n-1; i++){ 
            //int ind = v.back().first;
            //int ext = v.back().second;
            int slope = slopes.back();

            if(slope == 0){
                if(arr[i]==arr[i+1]) continue;
                else if(arr[i]<arr[i+1]){
                    v.push_back({i,0});
                    slopes.push_back(1);
                }
                else{
                    v.push_back({i,2});
                    slopes.push_back(-1);
                }
            }

            if(slope == 1){
                if(arr[i]<arr[i+1]) continue;
                else if(arr[i]>arr[i+1]){
                    v.push_back({i,1});
                    slopes.push_back(-1);
                }
                else{
                    v.push_back({i,2});
                    slopes.push_back(0);
                }
            }

            if(slope == -1){
                if(arr[i]>arr[i+1]) continue;
                else if(arr[i]<arr[i+1]){
                    v.push_back({i,-1});
                    slopes.push_back(1);
                }
                else{
                    v.push_back({i,0});
                    slopes.push_back(0);
                }
            }

            // if(ext == 1){
            //     if(arr[i]<arr[i-1]) continue;
            //     else if(arr[i]>arr[i-1]) v.push_back({i-1,-1});
            //     else v.push_back({i-1,0}); 
            // }

            // else if(ext == -1){
            //     if(arr[i]>arr[i-1]) continue;
            //     else if(arr[i]<arr[i-1]) v.push_back({i-1,1});
            //     else v.push_back({i-1,2});
            // }

            // else if(ext == 0){
            //     if(arr[i]==arr[i-1]) continue;
            //     else if(arr[i]<arr[i-1]) v.push_back({i-1,2});
            //     else v.push_back({i-1,0});
            // }

            // else if(ext == 2){
            //     if(arr[i]==arr[i-1]) continue;
            //     else if(arr[i]<arr[i-1]) //continue;
            //     else v.push_back({i-1,0});
            // }
            
        }

        int k = slopes.back();
        if(k==1){
            v.push_back({n-1,1});
        }
        else if(k==-1){
            v.push_back({n-1,-1});
        }
        else{
            v.push_back({n-1,0});
        }

        // int id = n-1;
        // if(arr[id]>arr[id-1]) v.push_back({id,1});
        // else if(arr[id]<arr[id-1]) v.push_back({id,-1});
        // else v.push_back({id,0});
        
        int t = v.size();
        vector<int> node(t);
        int sum1 = 0;

        for(int i=0; i<t; i++){
            if(v[i].second <= 0) node[i] = 1;
            else{
                if(v[i].second==1){
                    int d = -1;
                    if(i-1>=0) d = max(d,v[i].first-v[i-1].first+1);
                    if(i+1<t) d = max(d,v[i+1].first-v[i].first+1);
                    node[i] = d;
                }
                else{
                    if(v[i+1].second == 0 || v[i+1].second == 2){
                        node[i] = v[i].first-v[i-1].first+1;
                    }
                    else{
                        node[i] = v[i+1].first-v[i].first+1;
                    }
                }
                
            }
            sum1 += node[i];
            cout << v[i].first << ' ' << v[i].second << ' ' << node[i] << endl;

        }

        int sum = 0;

        for(int i=0; i<t-1; i++){
            int inc;
            if(v[i].second == -1){
                int n = v[i+1].first-v[i].first+1;
                inc = ((n+1)*(n-2))/2;
                sum += inc;
            }
            else if(v[i].second == 0){
                if(slopes[i]==0){
                //if(v[i+1].second == 0 || v[i+1].second == 2){
                    int n = v[i+1].first-v[i].first-1;
                    inc = n;
                    sum += n;
                }
                else{
                    int n = v[i+1].first-v[i].first+1;
                    inc = ((n+1)*(n-2))/2;
                    sum += inc;
                }
            }
            else if(v[i].second == 2){
                //if(v[i+1].second == 0 || v[i+1].second == 2){
                    if(slopes[i]==0){
                    int n = v[i+1].first-v[i].first-1;
                    inc = n;
                    sum += n;
                }
                else{
                    int n = v[i+1].first-v[i].first+1;
                    inc = (n+1)*(n-2)/2;
                    sum += inc;
                }
            }
            else{
                int n = v[i+1].first-v[i].first+1;
                inc = (n+1)*(n-2)/2;
                sum += inc;
            }
            cout << inc << ' ';
        }

        cout << endl;

        cout << sum << ' ' << sum1 ;

        int tot = sum + sum1;
        return tot;
    }
};
21300