Problem:
Score : 800 points

Problem Statement

There are N cards placed on a grid with H rows and W columns of squares.

The i-th card has an integer A_i written on it, and it is placed on the square at the R_i-th row from the top and the C_i-th column from the left.

Multiple cards may be placed on the same square.

You will first pick up at most one card from each row.

Then, you will pick up at most one card from each column.

Find the maximum possible sum of the integers written on the picked cards.

Constraints

All values are integers.

1 \leq N \leq 10^5

1 \leq H, W \leq 10^5

1 \leq A_i \leq 10^5

1 \leq R_i \leq H

1 \leq C_i \leq W

Input

Input is given from Standard Input in the following format:

N H W
R_1 C_1 A_1
R_2 C_2 A_2
\vdots
R_N C_N A_N

Output

Print the maximum possible sum of the integers written on the picked cards.

Sample Input 1

6 2 2
2 2 2
1 1 8
1 1 5
1 2 9
1 2 7
2 1 4

Sample Output 1

28

The sum of the integers written on the picked cards will be 28, the maximum value possible, if you pick up cards as follows:

Pick up the fourth card from the first row.

Pick up the sixth card from the second row.

Pick up the second card from the first column.

Pick up the fifth card from the second column.

Sample Input 2

13 5 6
1 3 35902
4 6 19698
4 6 73389
3 6 3031
3 1 4771
1 4 4784
2 1 36357
2 1 24830
5 6 50219
4 6 22645
1 2 30739
1 4 68417
1 5 78537

Sample Output 2

430590

Sample Input 3

1 100000 100000
1 1 1

Sample Output 3

1

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
using namespace std;
inline LL read()
{
	LL f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int stack[20];
template<typename T>inline void write(T x)
{
	if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int top=0;
    while(x)stack[++top]=x%10,x/=10;
    while(top)putchar(stack[top--]+'0');
}
template<typename T>inline void pr1(T x){write(x);putchar(' ');}
template<typename T>inline void pr2(T x){write(x);putchar('\n');}
const int MAXN=100005;
struct edge{int x,y,c,d,next;}a[6*MAXN];int len=1,last[3*MAXN];
void ins(int x,int y,int c,int d)
{
	len++;a[len].x=x;a[len].y=y;a[len].c=c;a[len].d=d;
	a[len].next=last[x];last[x]=len;
	len++;a[len].x=y;a[len].y=x;a[len].c=0;a[len].d=-d;
	a[len].next=last[y];last[y]=len;
}
deque<int> li;
bool v[3*MAXN];
LL d[3*MAXN];
int st,ed,pre[3*MAXN],tmp[3*MAXN];
bool spfa()
{
	memset(v,false,sizeof(v));v[st]=true;
	memset(d,-63,sizeof(d));d[st]=0;
	li.push_back(st);
	while(!li.empty())
	{
		int x=li.front();li.pop_front();v[x]=false;
		for(int k=last[x];k;k=a[k].next)
		{
			int y=a[k].y;
			if(d[y]<d[x]+a[k].d&&a[k].c)
			{
				d[y]=d[x]+a[k].d;
				pre[y]=x;tmp[y]=k;
				if(!v[y])
				{
					v[y]=true;
					if(!li.size()||d[y]>d[li.front()])li.push_front(y);
					else li.push_back(y);
				}
			}
		}
	}
	return d[ed]>0;
}
LL mcf()
{
	LL minn,ret=0;
	while(spfa())
	{
		int x=ed;minn=999999999;
		while(x!=st)
		{
			minn=min(minn,(LL)a[tmp[x]].c);
			x=pre[x];
		}
		ret+=1LL*minn*d[ed];x=ed;
		while(x!=st)
		{
			a[tmp[x]].c-=minn;a[tmp[x]^1].c+=minn;
			x=pre[x];
		}
	}
	return ret;
}
int n,A,B;
int via[MAXN],vib[MAXN];
int main()
{
	n=read();A=read();B=read();
	st=A+B+n+1;ed=st+1;
	int cnt1=0,cnt2=0;
	for(int i=1;i<=n;i++)
	{
		int u=read(),v=read(),C=read();
		if(!via[u])via[u]=++cnt1;
		if(!vib[v])vib[v]=++cnt2;
		ins(i,via[u]+n,1,0);ins(i,vib[v]+n+A,1,0);
		ins(st,i,1,C);
	}
	for(int i=1;i<=A;i++)if(via[i])ins(via[i]+n,ed,1,0);
	for(int i=1;i<=B;i++)if(vib[i])ins(vib[i]+n+A,ed,1,0);
	pr2(mcf());
	return 0;
}