#include #pragma warning(disable:4996) int studentStack[500010] = { 0 }; int weight[500010] = { 0 }; int main() { int sum = 0; int top = -1; int tmpstu; int times[2] = { 1,1 }; long long int competitionSum = 0; scanf("%d", &sum); for (int i = 0; i < sum; i++) { scanf("%d", &tmpstu); for (int i = top; i >= -1; i--) { if (i==-1) { top = i + 1; studentStack[top] = tmpstu; weight[top] = 1; break; } else if (studentStack[i] > tmpstu) { top = i + 1; studentStack[top] = tmpstu; weight[top] = 1; competitionSum+=1; break; } else if (studentStack[i] == tmpstu) { if (i == 0) { top = i; competitionSum += weight[top]; weight[top] += 1; break; } else { top = i; competitionSum += weight[top] + 1; weight[top] += 1; break; } } else { competitionSum += weight[i]; } } } printf("%lld", competitionSum); }