Prefix Sum Concept.
Code

# Code
```cpp []
class Solution {
public:

    int sumOfBeauties(vector<int>& nums) {
        int n = nums.size() ;
        vector <int> prevlargest(n,0) ;
        prevlargest[0] = -1;
        for (int i=1; i<n; i++) {
            prevlargest[i] = max(prevlargest[i-1], nums[i-1]) ;
        }
        vector <int> nextsmallest(n,0) ;
        nextsmallest[n-1] = INT_MAX;
        for (int i=n-2; i>=0; i--) {
            nextsmallest[i] = min(nextsmallest[i+1], nums[i+1]) ;
        }
        vector <int> beauty(n,0) ;
        for (int i=1; i<n-1; i++) {
            if (prevlargest[i] < nums[i] && nums[i] < nextsmallest[i]) {
                beauty[i] = 2;
            }
            else if (nums[i-1]<nums[i] && nums[i]<nums[i+1]) {
                beauty[i] = 1;
            }
        }
        int res = 0 ;
        for (int i=1; i<n-1; i++) {
            res += beauty[i] ;
        }
        return res ;
    }
};
```
['C++']