#include <stdio.h>
#include <stdlib.h>

typedef struct _data {
	int val;
	int count;
} data;

typedef struct _node {
	int nways;
	data *e1;
	data *e2;
	struct _node *lst;
	struct _node *mst;
	struct _node *rst;
} node;

node *allocNode(data *e) {
	node *p = (node *)malloc(sizeof(node));
	p->nways = -2;
	p->e1 = e;
	p->e2 = NULL;
	p->lst = NULL;
	p->mst = NULL;
	p->rst = NULL;
	return p;
}

node *reuse(node *p, int n, data *e1, data *e2, node *l, node *m, node *r) {
	p->nways = n;
	p->e1 = e1;
	p->e2 = e2;
	p->lst = l;
	p->mst = m;
	p->rst = r;
	return p;
}

node *deactivate(node *p) {
	p->nways = p->nways < 0 ? p->nways * (-1) : p->nways;
	return p;
}

int active(node *p) {
	return p != NULL && p->nways < 0;
}

node *balance2Li(node *t) {
	node *a = t->lst;
	if (!active(a)) return t;
	node *p = reuse(a,3,a->e1,t->e1,a->lst,a->rst,t->rst);
	free(t);
	return p;
}

node *balance2Ri(node *t) {
	node *a = t->rst;
	if (!active(a)) return t;
	node *p = reuse(a,3,t->e1,a->e1,t->lst,a->lst,a->rst);
	free(t);
	return p;
}

node *balance3Li(node *t) {
	node *a = t->lst;
	if (!active(a)) return t;
	node *r = (node *)malloc(sizeof(node));
	reuse(r,2,t->e2,NULL,t->mst,NULL,t->rst);
	return reuse(t,-2,t->e1,NULL,deactivate(a),NULL,r);
}

node *balance3Mi(node *t) {
	node *a = t->mst;
	if (!active(a)) return t;
	node *r = (node *)malloc(sizeof(node));
	a->rst = reuse(r,2,t->e2,NULL,a->rst,NULL,t->rst);
	a->lst = reuse(t,2,t->e1,NULL,t->lst,NULL,a->lst);
	return a;
}

node *balance3Ri(node *t) {
	node *a = t->rst;
	if (!active(a)) return t;
	node *l = (node *)malloc(sizeof(node));
	reuse(l,2,t->e1,NULL,t->lst,NULL,t->mst);
	return reuse(t,-2,t->e2,NULL,l,NULL,deactivate(a));
}

node *set(node *t, data *e, int *count) {
	if (t == NULL) return allocNode(e);
	int cmp1, cmp2;
	cmp1 = e->val - t->e1->val;
	switch (t->nways) {
		case 2:
			if (cmp1 < 0) {
				t->lst = set(t->lst,e,count);
				*count += t->e1->count + 1;
				return balance2Li(t);
			}
			else if (cmp1 > 0) {
				t->rst = set(t->rst,e,count);
				t->e1->count++;
				return balance2Ri(t);
			}
		case 3:
			if (cmp1 < 0) {
				t->lst = set(t->lst,e,count);
				*count += t->e1->count + 1;
				return balance3Li(t);
			}

			cmp2 = e->val - t->e2->val;
			if (cmp2 < 0) {
				t->mst = set(t->mst,e,count);
				*count += t->e2->count + 1;
				return balance3Mi(t);
			}
			else if (cmp2 > 0) {
				t->rst = set(t->rst,e,count);
				t->e2->count++;
				return balance3Ri(t);
			}
		default:
			exit(1);
	}
	return NULL;
}

void insert(node **root, int val, int *count) {
	data *e = (data *)malloc(sizeof(data));
	e->val = val;
	e->count = 0;
	*root = deactivate(set(*root, e, count));
}

void terminate(node *t) {
	if (t == NULL) return;
	terminate(t->lst);
	terminate(t->mst);
	terminate(t->rst);
	if (t->e1 != NULL) free(t->e1);
	if (t->e2 != NULL) free(t->e2);
	free(t);
}

int main() {
	int n, i, val, count;
	node *root;

	scanf("%d",&n);
	root = NULL;
	count = 0;
	for (i = 0; i < n; i++) {
		scanf("%d",&val);
		insert(&root,val,&count);
	}

	printf("%d\n",count);

	terminate(root);
	
	return 0;
}