#include
<
iostream
>
#define MAXN 10001
using namespace std;
int JUMP[MAXN];
int cmp( const void * a, const void * b)
{
return (( int * )b)[ 0 ] - (( int * )a)[ 0 ];
}
int find( int x) /* 查找包含接点x的集合的根节点 */
{
int i = x, t;
while (JUMP[i] != i)
i = JUMP[i]; /* 最终求出i为树根 */
while (x != i) /* 压缩路径以提高以后检索效率 */
{
t = JUMP[x];
JUMP[x] = i;
x = t;
}
return i; /* 返回根结点 */
}
int main()
{
int n, i, ans, day, max;
int p[MAXN][ 2 ];
bool mk[MAXN];
while (scanf( " %d " , & n) != EOF)
{
ans = 0 ;
max = 0 ;
memset(mk, 0 , sizeof (mk));
for (i = 0 ; i < n; i ++ )
{
scanf( " %d %d " , & p[i][ 0 ], & p[i][ 1 ]);
if (p[i][ 1 ] > max)
max = p[i][ 1 ];
}
for (i = 1 ; i <= max; i ++ )
JUMP[i] = i;
qsort(p, n, sizeof ( int ) * 2 , cmp);
for (i = 0 ; i < n; i ++ )
{
day = p[i][ 1 ];
if (mk[day])
day = find(day);
if (day > 0 )
{
ans += p[i][ 0 ];
mk[day] = true ;
JUMP[day] = day - 1 ;
}
}
printf( " %d\n " , ans);
}
return 0 ;
}
#define MAXN 10001
using namespace std;
int JUMP[MAXN];
int cmp( const void * a, const void * b)
{
return (( int * )b)[ 0 ] - (( int * )a)[ 0 ];
}
int find( int x) /* 查找包含接点x的集合的根节点 */
{
int i = x, t;
while (JUMP[i] != i)
i = JUMP[i]; /* 最终求出i为树根 */
while (x != i) /* 压缩路径以提高以后检索效率 */
{
t = JUMP[x];
JUMP[x] = i;
x = t;
}
return i; /* 返回根结点 */
}
int main()
{
int n, i, ans, day, max;
int p[MAXN][ 2 ];
bool mk[MAXN];
while (scanf( " %d " , & n) != EOF)
{
ans = 0 ;
max = 0 ;
memset(mk, 0 , sizeof (mk));
for (i = 0 ; i < n; i ++ )
{
scanf( " %d %d " , & p[i][ 0 ], & p[i][ 1 ]);
if (p[i][ 1 ] > max)
max = p[i][ 1 ];
}
for (i = 1 ; i <= max; i ++ )
JUMP[i] = i;
qsort(p, n, sizeof ( int ) * 2 , cmp);
for (i = 0 ; i < n; i ++ )
{
day = p[i][ 1 ];
if (mk[day])
day = find(day);
if (day > 0 )
{
ans += p[i][ 0 ];
mk[day] = true ;
JUMP[day] = day - 1 ;
}
}
printf( " %d\n " , ans);
}
return 0 ;
}