#include #include #include #include using namespace std; int main() { int numOfTerr = 0; cin >> numOfTerr; vector m_Heights; vector open(1000000, -1); int curr = 0; int edges=0; for ( int i = 0; i < numOfTerr; i++ ) { cin >> curr; m_Heights.emplace_back(curr); } long long last=-1; for(int i = 0; i < m_Heights.size(); i++ ){ if(open[m_Heights[i]]!=-1 && m_Heights[i]!=last ){ edges+=i-open[m_Heights[i]]-1; open[m_Heights[i]]= i; } else{ open[m_Heights[i]]= i; } for(int j=0;j< m_Heights[i];j++){ open[j]= -1; } last=m_Heights[i]; } cout << edges; return 0; }