number_test = int(input())

for k in range(number_test):
    n = int(input())
    T = list(map(int,input().split(" ")))
    sol = 0
    maxi = 0
    mini = None
    current = T[0] > 0
    for k in range(n-1):

        prochain = T[k+1] > 0
        if current and prochain  :
            if T[k] > maxi :
                maxi = T[k]
        elif not(current) and not(prochain):
            if mini == None :
                mini = T[k]
            elif T[k] > mini :
                mini = T[k]
            else :
                pass
        else:
            if current:
                if T[k] > maxi:
                    sol += T[k]
                    maxi = 0
                else:
                    sol += maxi
                    maxi = 0
            else:
                if mini == None:
                    sol += T[k]
                elif T[k] > mini :
                    sol += T[k]
                    mini = None
                else :
                    sol += mini
                    mini = None
        current = prochain
    prochain = current
    current = T[n-2] > 0
    if current != prochain :
        sol += T[n-1]
    elif current:
        if T[n-1] > maxi :
            sol += T[n-1]
        else :
            sol += maxi
    else :
        if T[n-1]> mini:
            sol += T[n-1]
        else :
            sol += mini
    print(str(sol))


number_test = int(input())

for k in range(number_test):
    n = int(input())
    T = list(map(int,input().split(" ")))
    sol = 0
    maxi = 0
    mini = None

    avant = T[0] > 0
    for k in range(n):
        current = T[k] > 0
        if current and avant:
            if T[k]> maxi :
                maxi = T[k]
        elif not(current) and not(avant):
            if mini == None:
                mini = T[k]
            elif T[k] > mini :
                mini = T[k]
        elif current:
            sol += mini
            mini = None
            maxi = T[k]
            avant = current
        else :
            sol += maxi
            maxi = 0
            mini = T[k]
            avant = current
    if current and avant:
        sol += maxi
    elif not(current) and not(avant):
        sol += mini
    print(sol)


t = int(input())

for i in range(t):
    n = int(input())

    sequence = list(map(int, input().split()))

    sum_value = 0

    val = sequence[0]
    pos = sequence[0] > 0

    for v in range(1, n):
        if pos == (sequence[v] > 0):
            val = max(val, sequence[v])
        else:
            sum_value += val
            val = sequence[v]

        pos = sequence[v] > 0

    sum_value += val

    print(sum_value)


t = int(input())

for i in range(t):
    n = int(input())

    sequence = list(map(int, input().split()))

    sum_value = 0

    val = sequence[0]
    pos = sequence[0] > 0

    for v in range(1, n):
        if pos == (sequence[v] > 0):
            val = max(val, sequence[v])
        else:
            sum_value += val
            val = sequence[v]

        pos = sequence[v] > 0

        if v == n - 1:
            sum_value += val

    print(sum_value)


T = int(input())
for t in range(T):
  n = int(input())
  l = list(map(int,input().split()))
  s=0
  k=l[0]
  last=l[0]
  for i in range(1,len(l)):
    if last*l[i]<0:
      s+=k
      k=l[i]
    else:
      if l[i]>k:
        k=l[i]
    last=l[i]
  s+=k

  print(s)


T = int(input())
for t in range(T):
  n = int(input())
  l = list(map(int,input().split()))
  maxd=0
  s=1
  k=l[0]
  m=-1e99
  last=l[0]
  for i in range(1,len(l)):
    if last*l[i]>0:
      if s>=maxd:
        maxd=s
        if k>m:
          m=k
      s=1
      k=l[i]
    else:
      s+=1
      k+=l[i]
    last=l[i]

  if s>=maxd:
    k=sum(l[len(l)-s:len(l)])
    if k>m:
      m=k

  print(m)


T = int(input())
for t in range(T):
  n = int(input())
  l = list(map(int,input().split()))
  maxd=0
  s=1
  r=[]
  last=l[0]
  for i in range(1,len(l)):
    if last*l[i]>0:
      if s>=maxd:
        maxd=s
        if s>maxd:
          r.clear()
        r.append([i-s,i])
      s=1
    else:
      s+=1
    last=l[i]

  if s>=maxd:
    if s>maxd:
      r.clear()
    r.append([len(l)-s,len(l)])

  m=sum(l[r[0][0]:r[0][1]])
  for e in r:
    k=sum(l[e[0]:e[1]])
    if k>m:
      m=k
  print(m)


T = int(input())
for t in range(T):
  n = int(input())
  l = list(map(int,input().split()))
  maxd=0
  s=1
  k=l[0]
  m=-1e99
  last=l[0]
  for i in range(1,len(l)):
    if last*l[i]>0:
      s=1
      k=l[i]
    else:
      s+=1
      k+=l[i]
    if s>=maxd:
        if s>maxd:
          maxd=s
          m=k
        if k>m:
          m=k
    last=l[i]

  print(m)


for _ in range(int(input())):
	l=int(input())
	arr=list(map(int,input().split()))
	pos=0;nev=-9999999999;finalsum=0
	for i in range(l):
		if arr[i]>0:
			if i<l-1 and arr[i+1]>0:
				if arr[i]>pos:
					pos=arr[i]
			elif i<l-1 and arr[i+1]<0:
				if arr[i]>pos:
					pos=arr[i]
					# print("number added ",pos)
					finalsum=pos+finalsum
					pos=0
			else:
				if arr[i]>pos:
					pos=arr[i]
					# print("number added ",pos)
					pos=0
					finalsum=pos+finalsum
				else:
					finalsum=pos+finalsum

		else:
			if i<l-1 and arr[i+1]<0:
				if arr[i]>nev:
					nev=arr[i]
					# print("new nev iss",nev)
			elif i<l-1 and arr[i+1]>0:
				if arr[i]>nev:
					nev=arr[i]
					# print("number added ",nev)
					finalsum=nev+finalsum
					nev=-99999999999
			else:
				if arr[i]>nev:
					nev=arr[i]
					# print("number added ",nev)
					finalsum=nev+finalsum
				else:
					finalsum=nev+finalsum
	print(finalsum)


for _ in range(int(input())):
	l=int(input())
	arr=list(map(int,input().split()))
	pos=0;nev=-9999999999;finalsum=0
	for i in range(l):
		if arr[i]>0:
			if i<l-1 and arr[i+1]>0:
				if arr[i]>pos:
					pos=arr[i]
			elif i<l-1 and arr[i+1]<0:
				if arr[i]>pos:
					pos=arr[i]
					# print("number added ",pos)
					finalsum=pos+finalsum
					pos=0
				else:
					finalsum=pos+finalsum
					pos=0
			else:
				if arr[i]>pos:
					pos=arr[i]
					# print("number added ",pos)
					finalsum=pos+finalsum
					pos=0
				else:
					finalsum=pos+finalsum

		else:
			if i<l-1 and arr[i+1]<0:
				if arr[i]>nev:
					nev=arr[i]
					# print("new nev iss",nev)
			elif i<l-1 and arr[i+1]>0:
				if arr[i]>nev:
					nev=arr[i]
					# print("number added ",nev)
					finalsum=nev+finalsum
					nev=-99999999999
				else:
					finalsum=nev+finalsum
					nev=-99999999999
			else:
				if arr[i]>nev:
					nev=arr[i]
					# print("number added ",nev)
					finalsum=nev+finalsum
				else:
					finalsum=nev+finalsum
	print(finalsum)


for _ in range(int(input())):
	l=int(input())
	arr=list(map(int,input().split()))
	pos=0;nev=-9999999999;finalsum=0
	for i in range(l):
		if arr[i]>0:
			if i<l-1 and arr[i+1]>0:
				if arr[i]>pos:
					pos=arr[i]
			elif i<l-1 and arr[i+1]<0:
				if arr[i]>pos:
					pos=arr[i]
					# print("number added ",pos)
					finalsum=pos+finalsum
					pos=0
			else:
				if arr[i]>pos:
					pos=arr[i]
					# print("number added ",pos)
					finalsum=pos+finalsum
					pos=0
				else:
					finalsum=pos+finalsum

		else:
			if i<l-1 and arr[i+1]<0:
				if arr[i]>nev:
					nev=arr[i]
					# print("new nev iss",nev)
			elif i<l-1 and arr[i+1]>0:
				if arr[i]>nev:
					nev=arr[i]
					# print("number added ",nev)
					finalsum=nev+finalsum
					nev=-99999999999
			else:
				if arr[i]>nev:
					nev=arr[i]
					# print("number added ",nev)
					finalsum=nev+finalsum
				else:
					finalsum=nev+finalsum
	print(finalsum)


def solve(a):
    max_ = a[0]
    prev_max = a[0]
    switch_counter = 0
    sum_ = 0

    for i in range(1, len(a)):
        current_item = a[i]
        previous_item = a[i-1]

        if (current_item > max_) & (current_item * previous_item >= 0):
            prev_max = max_
            max_ = current_item

        if current_item * previous_item < 0:
            if (current_item > max_):
                prev_max = max_
            sum_ += max_
            max_ = current_item
            prev_max = max_

            switch_counter += 1

        if i == len(a) - 1:
            sum_ += max_

    if switch_counter > 0:
        print(sum_)
    else:
        print(prev_max)

    if switch_counter > 0:
        print(sum_)
    else:
        print(prev_max)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


def solve(a):
    max_ = a[0]

    if len(a) > 1:
        sum_ = 0

        for i in range(1, len(a)):
            current_item = a[i]
            previous_item = a[i-1]

            if (current_item > max_) & (current_item * previous_item >= 0):
                max_ = current_item

            if current_item * previous_item < 0:
                sum_ += max_
                max_ = current_item

            if i == len(a) - 1:
                if current_item >= max_:
                    max_ = current_item
                sum_ += max_
    else:
        sum_ = a[0]

    print(sum_)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


def solve(a):
    max_ = a[0]
    prev_max = a[0]
    switch_counter = 0
    sum_ = 0

    for i in range(1, len(a)):
        current_item = a[i]
        previous_item = a[i-1]

        if (current_item > max_) & (current_item * previous_item >= 0):
            prev_max = max_
            max_ = current_item

        if current_item * previous_item < 0:
            if (current_item > max_):
                prev_max = max_
            sum_ += max_
            max_ = current_item
            prev_max = max_

            switch_counter += 1

        if i == len(a) - 1:
            sum_ += max_

    if switch_counter > 0:
        print(sum_)
    else:
        print(prev_max)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


def solve(a):
    max_ = a[0]
    prev_max = a[0]
    switch_counter = 0
    sum_ = 0

    for i in range(1, len(a)):
        current_item = a[i]
        previous_item = a[i-1]

        if (current_item > max_) & (current_item * previous_item >= 0):
            prev_max = max_
            max_ = current_item

        if current_item * previous_item < 0:
            if (current_item > max_):
                prev_max = max_
            sum_ += max_
            max_ = current_item
            prev_max = max_

            switch_counter += 1

        if i == len(a) - 1:
            if current_item >= max_:
                max_ = current_item
            sum_ += max_

    print(sum_)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


def solve(a):
    max_ = a[0]
    prev_max = a[0]
    switch_counter = 0
    sum_ = 0

    for i in range(1, len(a)):
        current_item = a[i]
        previous_item = a[i-1]

        if (current_item > max_) & (current_item * previous_item >= 0):
            prev_max = max_
            max_ = current_item

        if current_item * previous_item < 0:
            if (current_item > max_):
                prev_max = max_
            sum_ += prev_max
            max_ = current_item
            prev_max = max_

            switch_counter += 1

            if i == len(a) - 1:
                sum_ += max_

    if switch_counter > 0:
        print(sum_)
    else:
        print(prev_max)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


def solve(a):
    max_ = a[0]
    prev_max = a[0]
    switch_counter = 0
    if len(a) > 1:
        sum_ = 0

        for i in range(1, len(a)):
            current_item = a[i]
            previous_item = a[i-1]

            if (current_item > max_) & (current_item * previous_item >= 0):
                prev_max = max_
                max_ = current_item

            if current_item * previous_item < 0:

                sum_ += max_
                max_ = current_item
                prev_max = max_

                switch_counter += 1

            if i == len(a) - 1:
                if current_item >= max_:
                    max_ = current_item
                sum_ += max_
    else:
        sum_ = a[0]

    print(sum_)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


def solve(a):
    max_ = a[0]
    prev_max = a[0]
    switch_counter = 0
    if len(a) > 1:
        sum_ = 0

        for i in range(1, len(a)):
            current_item = a[i]
            previous_item = a[i-1]

            if (current_item > max_) & (current_item * previous_item >= 0):
                prev_max = max_
                max_ = current_item
                print('prev_max,max',prev_max,max_)

            if current_item * previous_item < 0:
                if (current_item > max_):
                    prev_max = max_
                    print(max_)
                sum_ += max_
                print('sum+',prev_max)
                max_ = current_item
                prev_max = max_

                switch_counter += 1

            if i == len(a) - 1:
                print('sum___+',max_)
                if current_item >= max_:
                    max_ = current_item
                sum_ += max_
    else:
        sum_ = a[0]

    print(sum_)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


def solve(a):
    max_ = a[0]
    prev_max = a[0]
    switch_counter = 0
    if len(a) > 1:
        sum_ = 0

        for i in range(1, len(a)):
            current_item = a[i]
            previous_item = a[i-1]

            if (current_item > max_) & (current_item * previous_item >= 0):
                prev_max = max_
                max_ = current_item

            if current_item * previous_item < 0:
                if (current_item > max_):
                    prev_max = max_
                sum_ += max_
                max_ = current_item
                prev_max = max_

                switch_counter += 1

            if i == len(a) - 1:
                if current_item >= max_:
                    max_ = current_item
                sum_ += max_
    else:
        sum_ = a[0]

    print(sum_)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


a = list(map(lambda x: int(x), input().split(' ')))


def solve(a):
    max_ = a[0]
    prev_max = a[0]
    sum_ = 0
    switch_counter = 0
    for i in range(1,len(a)):
        item = a[i]
        previous_item = a[i-1]
        if item*previous_item < 0:
            switch_counter += 1
            prev_max = max_
            max_ = item
            if switch_counter == 1:
                sum_ += sum([prev_max,max_])
            else:
                sum_ += max_
            #print(prev_max, max_)
        else:
            if (item > max_):
                max_ = item
    if switch_counter > 0:
        print(sum_)
    else:
        print(prev_max)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


def solve(a):
    max_ = a[0]
    prev_max = a[0]
    switch_counter = 0
    sum_ = 0

    for i in range(1, len(a)):
        current_item = a[i]
        previous_item = a[i-1]

        if (current_item > max_) & (current_item * previous_item >= 0):
            prev_max = max_
            max_ = current_item

        if current_item * previous_item < 0:
            if (current_item > max_):
                prev_max = max_
            sum_ += prev_max
            max_ = current_item
            prev_max = max_

            switch_counter += 1

            if i == len(a) - 1:
                sum_ += max_

    if switch_counter > 0:
        print(sum_)
    else:
        print(prev_max)

    if switch_counter > 0:
        print(sum_)
    else:
        print(prev_max)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


def solve(a):
    max_ = a[0]
    prev_max = a[0]
    switch_counter = 0
    sum_ = 0

    for i in range(1, len(a)):
        current_item = a[i]
        previous_item = a[i-1]

        if (current_item > max_) & (current_item * previous_item >= 0):
            prev_max = max_
            max_ = current_item

        if current_item * previous_item < 0:

            sum_ += prev_max
            max_ = current_item
            prev_max = max_

            switch_counter += 1

            if i == len(a) - 1:
                sum_ += max_

    if switch_counter > 0:
        print(sum_)
    else:
        print(prev_max)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


def solve(a):
    max_ = a[0]
    prev_max = a[0]
    switch_counter = 0
    sum_ = 0

    for i in range(1, len(a)):
        current_item = a[i]
        previous_item = a[i-1]

        if (current_item > max_) & (current_item * previous_item >= 0):
            prev_max = max_
            max_ = current_item

        if current_item * previous_item < 0:
            if (current_item > max_):
                prev_max = max_
            sum_ += prev_max
            max_ = current_item
            prev_max = max_

            switch_counter += 1

        if i == len(a) - 1:
            sum_ += max_

    if switch_counter > 0:
        print(sum_)
    else:
        print(prev_max)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


def solve(a):
    max_ = a[0]
    prev_max = a[0]
    sum_ = 0
    switch_counter = 0
    for i in range(1,len(a)):
        item = a[i]
        previous_item = a[i-1]
        if item*previous_item < 0:
            switch_counter += 1
            prev_max = max_
            max_ = item
            if switch_counter == 1:
                sum_ += prev_max
            else:
                sum_ += max_
            #print(prev_max, max_)
        else:
            if (item > max_):
                max_ = item
    if switch_counter == 1:
        sum_ += max_
    if switch_counter > 0:
        print(sum_)
    else:
        print(prev_max)

t = int(input())
for i in range(t):
    len_ = int(input())
    a = list(map(lambda x: int(x), input().split(' ')))
    solve(a)


t=int(input())
for i in range(t):
    n=int(input())
    a=[int(i) for i in input().split()]
    x=sum(a)
    z=[]
    j=1
    k=1
    if n==1:
        print(x)
    else:
        while(True):
            if a[j-1]*a[j]<0:
                z.append(max(a[0:j]))
                del a[0:j]
                j=0
            j+=1
            k+=1
            if k==n:
                z.append(max(a))
                break
        print(sum(z))


t=int(input())
for i in range(t):
    n=int(input())
    a=[int(i) for i in input().split()]
    z=[]
    j=1
    k=1
    while(True):
        if a[j-1]*a[j]<0:
            z.append(max(a[0:j]))
            del a[0:j]
            j=0
        j+=1
        k+=1
        if k>=n:
            z.append(max(a))
            break
    print(sum(z))


t=int(input())
for i in range(t):
    n=int(input())
    a=[int(i) for i in input().split()]
    x=a[0]
    z=0
    j=0
    while(j<n):
        if a[j]*x<0:
            z=z+x
            x=a[j]
        else:
            x=max(x,a[j])
        j+=1

    print(z+x)


for _ in range(int(input())):
    l=list(map(int,input().split()))
    mxs=l[0]
    c=0
    for i in l:
        if i*mxs<0:
            c+=mxs
            mxs=i
        else:
            mxs=max(mx,i)
    print c+mxs


for _ in range(int(input())):
    b=int(input())
    l=list(map(int,input().split()))
    mxs=l[0]
    c=0
    for i in range(1,b):
        if l[i]*mxs<0:
            c+=mxs
            mxs=l[i]
        else:
            mxs=max(mxs,l[i])
    print(c+mxs)


for _ in range(int(input())):
    b=int(input())
    l=list(map(int,input().split()))
    mxs=l[0]
    c=0
    for i in range(1,b):
        if i*mxs<0:
            c+=mxs
            mxs=i
        else:
            mxs=max(mxs,i)
    print(c+mxs)


for _ in range(int(input())):
    l=list(map(int,input().split()))
    mxs=l[0]
    c=0
    for i in l:
        if i*mxs<0:
            c+=mxs
            mxs=i
        else:
            mxs=max(mx,i)
    print(c+mxs)


for _ in range(int(input())):
    b=int(input())
    l=list(map(int,input().split()))

    mxs=l[0]
    c=0
    for i in range(1,b):
        if i*mxs<0:
            c+=mxs
            mxs=i
        else:
            mxs=max(mx,i)
    print(c+mxs)


for _ in range(int(input())):
    l=list(map(int,input().split()))
    b=int(input())
    mxs=l[0]
    c=0
    for i in range(1,b):
        if i*mxs<0:
            c+=mxs
            mxs=i
        else:
            mxs=max(mx,i)
    print(c+mxs)


t= int(input())
for i in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    p=0
    n=0
    flag1=0
    flag2=0
    sum=0
    for j in l:
        if(p==0 and j>0):
            sum=sum+j
            flag1=1
            p=1
            n=0
        elif(n==0 and j<0):
            sum=sum+j
            flag2=1
            n=1
            p=0
    if(flag1==0 or flag2==0):
        print(max(l))
    else:
        print(sum)


t= int(input())
for i in range(t):
    a=int(input())
    l=list(map(int,input().split()))
    p=0
    n=0
    sum=0
    for j in l:
        if(p==0 and j>0):
            r=j
            sum=sum+j
            p=1
            n=0
        elif(n==0 and j<0):
            r1=j
            sum=sum+j
            n=1
            p=0
        elif(p==1 and j>r):
            sum=sum-r
            sum=sum+j
            r=j
        elif(n==0 and (j>r and j<0)):
            sum=sum-r1
            sum=sum+j
            r1=j
    print(sum)


t= int(input())
for i in range(t):
    a=int(input())
    l=list(map(int,input().split()))
    p=0
    n=0
    sum=0
    r=0
    for j in l:
        if(p==0 and j>0):
            r=j
            sum=sum+j
            p=1
            n=0
        elif(n==0 and j<0):
            r=j
            sum=sum+j
            n=1
            p=0
        elif((p==1 or n==1) and j>r):
            sum=sum-r
            sum=sum+j
            r=j
    print(sum)


t=int(input())
for p in range(t):
    a=list(map(int,input().split()))
    n=len(a)
    count=0
    for i in range(n-1):
        if a[i]*a[i+1]>0:
            a[i+1]=max(a[i],a[i+1])
        else:
            count+=a[i]
            a[i+1]=min(a[i],a[i+1])
    if a[n-1]*a[n-2]>0:count+=max(a[n-1],a[n-2])
    else:count+=a[n-1]+a[n-2]
    print(count)


t=int(input())
for p in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    count=0
    for i in range(n-1):
        if a[i]*a[i+1]>0:
            a[i+1]=max(a[i],a[i+1])
        else:
            count+=a[i]
            a[i+1]=min(a[i],a[i+1])
    if a[n-1]*a[n-2]>0:count+=max(a[n-1],a[n-2])
    else:count+=a[n-1]+a[n-2]
    print(count)


t=int(input())
for p in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    count=0
    for i in range(n-2):
        if a[i]*a[i+1]>0:
            a[i+1]=max(a[i],a[i+1])
        else:
            count+=a[i]
    if a[n-1]*a[n-2]>0:count+=max(a[n-1],a[n-2])
    else:count+=a[n-1]+a[n-2]
    print(count)


t=int(input())
for p in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    count=0
    for i in range(n-1):
        if a[i]*a[i+1]>0:
            a[i+1]=max(a[i],a[i+1])
        else:
            count+=a[i]
    if a[n-1]*a[n-2]>0:count+=max(a[n-1],a[n-2])
    else:count+=a[n-1]+a[n-2]
    print(count)


for _ in range(int(input())):
    n=int(input())
    li=[];s=[];ans=[]
    lists=list(map(int,input().split()))
    current=lists[0]
    s.append(current)
    start=0
    for i in range(1,n):
        if(current<0):
            if(lists[i]<0):
                s.append(lists[i])
            else:
                start=i
                current=max(s)
                s=[]
                break
        if(current>0):
            if(lists[i]>0):
                s.append(lists[i])
            else:
                start=i
                current=max(s)
                s=[]
                break
    ans.append(current)
    i=start
    while(i<=n-1):
        if(current<0):
            if(lists[i]>0):
                li.append(lists[i])
            elif(len(li)>0):
                current=max(li)
                ans.append(current)
                li=[]
                i-=1
        else:
            if(lists[i]<0):
                li.append(lists[i])
            elif(len(li)>0):
                current=max(li)
                ans.append(current)
                li=[]
                i-=1
        i+=1
    if(len(li)>0):
        ans.append(max(li))
    print(sum(ans))


#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <cstring>
#include <cstddef>
#define _USE_MATH_DEFINES // for C++
#include <cmath>

using namespace std;

const long long mod = 1000000007;
long long powmod(long long a,long long b) {long long res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

const int N = 1003;
int weight[N];
int beauty[N];
bool f[N];
int dp[2][N];

int main(){
  ios :: sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *fin = freopen("D.in", "rt", stdin);
  assert(fin != NULL);
  /* FILE *fout = freopen("D.ans", "w", stdout); */
#endif

  int n, m, w;
  cin >> n >> m >> w;

  for(int i = 0; i < n; i++){
    cin >> weight[i];
  }
  for(int i = 0; i < n; i++){
    cin >> beauty[i];
  }

  for(int i = 0; i < m; i++){
    int x, y;
    cin >> x >> y;

    x--;
    y--;

    if(x > y){
      swap(x, y);
    }

    if(x + 1 == y){
      f[x] = true;
    }
  }
  /* for(int i = 0; i < n; i++){ */
  /*   cout << f[i] << " "; */
  /* } */
  /* cout << endl; */
  vector< vector< pair<int, int> > > groups(n);
  int cnt = 0;
  int tw = 0;
  int tb = 0;
  for(int i = 0; i < n; i++){
    groups[cnt].push_back({weight[i], beauty[i]});

    tw += weight[i];
    tb += beauty[i];
    if(f[i] == false){
      groups[cnt].push_back({tw, tb});
      /* cout << "tb = " << tb << endl; */

      cnt++;
      tw = 0;
      tb = 0;
    }
  }
  groups.resize(cnt);
  /* cout << "cnt = " << cnt << endl; */

  int p = 0;
  for(auto g : groups){
    /* cout << "g.size() = " << g.size() << endl; */
    for(int k = 1; k <= w; k++){
      dp[p][k] = dp[p^1][k];
      for(pair<int, int> girl : g){
        if(k - girl.first >= 0){
          /* cout << k << "\t" << dp[p][k]; */
          dp[p][k] = max(dp[p][k], dp[p^1][k - girl.first] + girl.second);
          /* cout << "\t" << dp[p][k] << endl; */
        }
      }
    }
    p ^= 1;
  }

  int ans = 0;
  for(int i = 0; i < 2; i++){
    for(int j = 1; j <= w; j++){
      ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << "\n";

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <cstring>
#include <cstddef>
#define _USE_MATH_DEFINES // for C++
#include <cmath>

using namespace std;

const long long mod = 1000000007;
long long powmod(long long a,long long b) {long long res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

const int N = 1003;
int weight[N];
int beauty[N];
bool f[N];
int dp[2][N];

int main(){
  ios :: sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *fin = freopen("D.in", "rt", stdin);
  assert(fin != NULL);
  /* FILE *fout = freopen("D.ans", "w", stdout); */
#endif

  int n, m, w;
  cin >> n >> m >> w;

  for(int i = 0; i < n; i++){
    cin >> weight[i];
  }
  for(int i = 0; i < n; i++){
    cin >> beauty[i];
  }

  for(int i = 0; i < m; i++){
    int x, y;
    cin >> x >> y;

    x--;
    y--;

    if(x > y){
      swap(x, y);
    }

    if(x + 1 == y){
      f[x] = true;
    }
  }
  /* for(int i = 0; i < n; i++){ */
  /*   cout << f[i] << " "; */
  /* } */
  /* cout << endl; */
  vector< vector< pair<int, int> > > groups(n);
  int cnt = 0;
  int tw = 0;
  int tb = 0;
  for(int i = 0; i < n; i++){
    groups[cnt].push_back({weight[i], beauty[i]});

    tw += weight[i];
    tb += beauty[i];
    if(f[i] == false){
      cnt++;

      groups[i].push_back({tw, tb});
      tw = 0;
      tb = 0;
    }
  }
  groups.resize(cnt);
  /* cout << "cnt = " << cnt << endl; */

  int p = 0;
  for(auto g : groups){
    for(int k = 1; k <= w; k++){
      dp[p][k] = dp[p^1][k];
      for(pair<int, int> girl : g){
        if(k - girl.first >= 0){
          dp[p][k] = max(dp[p][k], dp[p^1][k - girl.first] + girl.second);
        }
      }
    }
    p ^= 1;
  }

  int ans = 0;
  for(int i = 0; i < 2; i++){
    for(int j = 1; j <= w; j++){
      ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << "\n";

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <cstring>
#include <cstddef>
#define _USE_MATH_DEFINES // for C++
#include <cmath>

using namespace std;

const long long mod = 1000000007;
long long powmod(long long a,long long b) {long long res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

const int N = 1003;
int weight[N];
int beauty[N];
bool f[N];
int dp[2][N];

int main(){
  ios :: sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *fin = freopen("D.in", "rt", stdin);
  assert(fin != NULL);
  /* FILE *fout = freopen("D.ans", "w", stdout); */
#endif

  int n, m, w;
  cin >> n >> m >> w;

  for(int i = 0; i < n; i++){
    cin >> weight[i];
  }
  for(int i = 0; i < n; i++){
    cin >> beauty[i];
  }

  for(int i = 0; i < m; i++){
    int x, y;
    cin >> x >> y;

    x--;
    y--;

    if(x > y){
      swap(x, y);
    }

    if(x + 1 == y){
      f[x] = true;
    }
  }
  /* for(int i = 0; i < n; i++){ */
  /*   cout << f[i] << " "; */
  /* } */
  /* cout << endl; */
  vector< vector< pair<int, int> > > groups(n);
  int cnt = 0;
  int tw = 0;
  int tb = 0;
  for(int i = 0; i < n; i++){
    groups[cnt].push_back({weight[i], beauty[i]});

    tw += weight[i];
    tb += beauty[i];
    if(f[i] == false){
      cnt++;

      groups[i].push_back({tw, tb});
      tw = 0;
      tb = 0;
    }
  }
  groups.resize(cnt);
  /* cout << "cnt = " << cnt << endl; */

  int p = 0;
  for(auto g : groups){
    for(int k = 1; k <= w; k++){
      dp[p][k] = max(dp[p][k - 1], dp[p^1][k]);
      for(pair<int, int> girl : g){
        if(k - girl.first >= 0){
          dp[p][k] = max(dp[p][k], dp[p^1][k - girl.first] + girl.second);
        }
      }
    }
    p ^= 1;
  }
  cout << max(dp[0][w], dp[1][w]) << "\n";

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <cstring>
#include <cstddef>
#define _USE_MATH_DEFINES // for C++
#include <cmath>

using namespace std;

const long long mod = 1000000007;
long long powmod(long long a,long long b) {long long res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

const int N = 1003;
int weight[N];
int beauty[N];
bool f[N];
int dp[2][N];
int parent[N];

int findP(int u){
  if(parent[u] != u){
    parent[u] = findP(parent[u]);
  }

  return parent[u];
}

void uni(int v, int u){
  int pv = findP(v);
  int pu = findP(u);

  if(pv < pu){
    swap(pv, pu);
  }
  parent[ pv ] = pu;
}

int main(){
  ios :: sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *fin = freopen("D.in", "rt", stdin);
  assert(fin != NULL);
  /* FILE *fout = freopen("D.ans", "w", stdout); */
#endif

  int n, m, w;
  cin >> n >> m >> w;

  for(int i = 0; i < n; i++){
    cin >> weight[i];
  }
  for(int i = 0; i < n; i++){
    cin >> beauty[i];
  }
  for(int i = 0; i < n; i++){
    parent[i] = i;
  }

  for(int i = 0; i < m; i++){
    int x, y;
    cin >> x >> y;

    x--;
    y--;

    uni(x, y);
  }
  /* for(int i = 0; i < n; i++){ */
  /*   cout << f[i] << " "; */
  /* } */
  /* cout << endl; */
  unordered_map<int, vector< pair<int, int> > > groups(n);

  for(int i = 0; i < n; i++){
    if(groups.find(findP(i)) == groups.end()){
      groups[ findP(i) ].push_back({weight[i], beauty[i]});
    }else{
      groups[ findP(i) ].front().first += weight[i];
      groups[ findP(i) ].front().second += beauty[i];
    }
    groups[ findP(i) ].push_back({weight[i], beauty[i]});
  }
  /* cout << "cnt = " << cnt << endl; */

  int p = 0;
  for(auto g : groups){
    /* cout << "g.size() = " << g.size() << endl; */
    for(int k = 1; k <= w; k++){
      dp[p][k] = dp[p^1][k];
      for(pair<int, int> girl : g.second){
        if(k - girl.first >= 0){
          /* cout << k << "\t" << dp[p][k]; */
          dp[p][k] = max(dp[p][k], dp[p^1][k - girl.first] + girl.second);
          /* cout << "\t" << dp[p][k] << endl; */
        }
      }
    }
    p ^= 1;
  }

  int ans = 0;
  for(int i = 0; i < 2; i++){
    for(int j = 1; j <= w; j++){
      ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << "\n";

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <cstring>
#include <cstddef>
#define _USE_MATH_DEFINES // for C++
#include <cmath>

using namespace std;

const long long mod = 1000000007;
long long powmod(long long a,long long b) {long long res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

const int N = 1003;
int weight[N];
int beauty[N];
bool f[N];
int dp[2][N];
int parent[N];

int findP(int u){
  if(parent[u] != u){
    parent[u] = findP(parent[u]);
  }

  return parent[u];
}

void uni(int v, int u){
  int pv = findP(v);
  int pu = findP(u);

  if(pv < pu){
    swap(pv, pu);
  }
  parent[ pv ] = pu;
}

int main(){
  ios :: sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *fin = freopen("D.in", "rt", stdin);
  assert(fin != NULL);
  /* FILE *fout = freopen("D.ans", "w", stdout); */
#endif

  int n, m, w;
  cin >> n >> m >> w;

  for(int i = 0; i < n; i++){
    cin >> weight[i];
  }
  for(int i = 0; i < n; i++){
    cin >> beauty[i];
  }
  for(int i = 0; i < n; i++){
    parent[i] = i;
  }

  for(int i = 0; i < m; i++){
    int x, y;
    cin >> x >> y;

    x--;
    y--;

    uni(x, y);
  }
  /* for(int i = 0; i < n; i++){ */
  /*   cout << f[i] << " "; */
  /* } */
  /* cout << endl; */
  unordered_map<int, vector< pair<int, int> > > groups(n);

  for(int i = 0; i < n; i++){
    if(groups.find(parent[i]) == groups.end()){
      groups[ parent[i] ].push_back({weight[i], beauty[i]});
    }else{
      groups[ parent[i] ].front().first += weight[i];
      groups[ parent[i] ].front().second += beauty[i];
    }
    groups[ parent[i] ].push_back({weight[i], beauty[i]});
  }
  /* cout << "cnt = " << cnt << endl; */

  int p = 0;
  for(auto g : groups){
    /* cout << "g.size() = " << g.size() << endl; */
    for(int k = 1; k <= w; k++){
      dp[p][k] = dp[p^1][k];
      for(pair<int, int> girl : g.second){
        if(k - girl.first >= 0){
          /* cout << k << "\t" << dp[p][k]; */
          dp[p][k] = max(dp[p][k], dp[p^1][k - girl.first] + girl.second);
          /* cout << "\t" << dp[p][k] << endl; */
        }
      }
    }
    p ^= 1;
  }

  int ans = 0;
  for(int i = 0; i < 2; i++){
    for(int j = 1; j <= w; j++){
      ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << "\n";

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <cstring>
#include <cstddef>
#define _USE_MATH_DEFINES // for C++
#include <cmath>

using namespace std;

const long long mod = 1000000007;
long long powmod(long long a,long long b) {long long res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

const int N = 1003;
int weight[N];
int beauty[N];
bool f[N];
int dp[2][N];
int parent[N];

int findP(int u){
  if(parent[u] != u){
    parent[u] = findP(parent[u]);
  }

  return parent[u];
}

void uni(int v, int u){
  parent[ findP(v) ] = findP(u);
}

int main(){
  ios :: sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *fin = freopen("D.in", "rt", stdin);
  assert(fin != NULL);
  /* FILE *fout = freopen("D.ans", "w", stdout); */
#endif

  int n, m, w;
  cin >> n >> m >> w;

  for(int i = 0; i < n; i++){
    cin >> weight[i];
  }
  for(int i = 0; i < n; i++){
    cin >> beauty[i];
  }
  for(int i = 0; i < n; i++){
    parent[i] = i;
  }

  for(int i = 0; i < m; i++){
    int x, y;
    cin >> x >> y;

    x--;
    y--;

    uni(x, y);
  }
  /* for(int i = 0; i < n; i++){ */
  /*   cout << f[i] << " "; */
  /* } */
  /* cout << endl; */
  unordered_map<int, vector< pair<int, int> > > groups(n);

  for(int i = 0; i < n; i++){
    if(groups.find(parent[i]) == groups.end()){
      groups[ parent[i] ].push_back({weight[i], beauty[i]});
    }else{
      groups[ parent[i] ].front().first += weight[i];
      groups[ parent[i] ].front().second += beauty[i];
    }
    groups[ parent[i] ].push_back({weight[i], beauty[i]});
  }
  /* cout << "cnt = " << cnt << endl; */

  int p = 0;
  for(auto g : groups){
    /* cout << "g.size() = " << g.size() << endl; */
    for(int k = 1; k <= w; k++){
      dp[p][k] = dp[p^1][k];
      for(pair<int, int> girl : g.second){
        if(k - girl.first >= 0){
          /* cout << k << "\t" << dp[p][k]; */
          dp[p][k] = max(dp[p][k], dp[p^1][k - girl.first] + girl.second);
          /* cout << "\t" << dp[p][k] << endl; */
        }
      }
    }
    p ^= 1;
  }

  int ans = 0;
  for(int i = 0; i < 2; i++){
    for(int j = 1; j <= w; j++){
      ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << "\n";

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <math.h>

#define ll long long int

using namespace std;

const int MAXN = 1000 + 5;
const int MAXW = 1000 + 5;

vector<int> graph[MAXN];
int weight[MAXN], beauty[MAXN];
bool used[MAXN];

vector <pair<int, int>> groups[MAXN];
int components = 0;
int sumWeight = 0, sumBeauty = 0;

void dfs(int v) {
    used[v] = true;
    sumWeight += weight[v];
    sumBeauty += beauty[v];
    groups[components].push_back(make_pair(weight[v], beauty[v]));
    for (size_t t = 0; t < graph[v].size(); t++)
        if (!used[graph[v][t]])
            dfs(graph[v][t]);
}

int main()
{
    int dp[MAXW][MAXN];
    for (int i = 0; i < MAXW; i++)
        for (int j = 0; j < MAXN; j++)
            dp[i][j] = 0;

    for (int i = 0; i < MAXN; i++)
        used[i] = false;

    int n = 0, m = 0, w = 0;
    cin >> n >> m >> w;
    for (int i = 1; i <= n; i++)
        cin >> weight[i];
    for (int i = 1; i <= n; i++)
        cin >> beauty[i];

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            sumWeight = sumBeauty = 0;
            dfs(i);
            if (groups[components].size() > 1) {
                groups[components].push_back(make_pair(sumWeight, sumBeauty));
            }
            components++;
        }
    }

    for (int i = 1; i <= w; i++)
        for (int j = 0; j < components; j++) {
            if (j > 0)
                dp[i][j] = dp[i][j - 1];
            else
                dp[i][j] = dp[i - 1][j];
            for (size_t t = 0; t < groups[j].size(); t++)
                if (i - groups[j].at(t).first >= 0)
                    if (j > 0)
                        dp[i][j] = max(dp[i][j], dp[i - groups[j].at(t).first][j - 1] + groups[j].at(t).second);
                    else
                        dp[i][j] = max(dp[i][j], groups[j].at(t).second);
        }

    printf("%d", dp[w][components - 1]);

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <math.h>

#define ll long long int

using namespace std;

const int MAXN = 1000 + 5;
const int MAXW = 1000 + 5;

vector<int> graph[MAXN];
int weight[MAXN], beauty[MAXN];
bool used[MAXN];

vector <pair<int, int>> groups[MAXN];
int components = 0;
int sumWeight = 0, sumBeauty = 0;

void dfs(int v) {
    used[v] = true;
    sumWeight += weight[v];
    sumBeauty += beauty[v];
    groups[components].push_back(make_pair(weight[v], beauty[v]));
    for (size_t t = 0; t < graph[v].size(); t++)
        if (!used[graph[v][t]])
            dfs(graph[v][t]);
}

int main()
{
    int dp[MAXN][MAXW];
    for (int i = 0; i < MAXN; i++)
        for (int j = 0; j < MAXW; j++)
            dp[i][j] = 0;

    for (int i = 0; i < MAXN; i++)
        used[i] = false;

    int n = 0, m = 0, w = 0;
    cin >> n >> m >> w;
    for (int i = 1; i <= n; i++)
        cin >> weight[i];
    for (int i = 1; i <= n; i++)
        cin >> beauty[i];

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            sumWeight = sumBeauty = 0;
            dfs(i);
            if (groups[components].size() > 1) {
                groups[components].push_back(make_pair(sumWeight, sumBeauty));
            }
            components++;
        }
    }

    for (int i = 1; i <= w; i++)
        for (int j = 0; j < components; j++) {
            if (j > 0)
                dp[i][j] = dp[i][j - 1];
            else
                dp[i][j] = dp[i - 1][j];
            for (size_t t = 0; t < groups[j].size(); t++)
                if (i - groups[j].at(t).first >= 0)
                    dp[i][j] = max(dp[i][j], dp[i - groups[j].at(t).first][j - 1] + groups[j].at(t).second);
        }

    cout << dp[w][components - 1] << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <math.h>

#define ll long long int

using namespace std;

const int MAXN = 1000 + 5;
const int MAXW = 1000 + 5;

vector<int> graph[MAXN];
int weight[MAXN], beauty[MAXN];
bool used[MAXN];

vector <pair<int, int>> groups[MAXN];
int components = 0;
int sumWeight = 0, sumBeauty = 0;

void dfs(int v) {
    used[v] = true;
    sumWeight += weight[v];
    sumBeauty += beauty[v];
    groups[components].push_back(make_pair(weight[v], beauty[v]));
    for (size_t t = 0; t < graph[v].size(); t++)
        if (!used[graph[v][t]])
            dfs(graph[v][t]);
}

int main()
{
    int dp[MAXW][MAXN];
    for (int i = 0; i < MAXW; i++)
        for (int j = 0; j < MAXN; j++)
            dp[i][j] = 0;

    for (int i = 0; i < MAXN; i++)
        used[i] = false;

    int n = 0, m = 0, w = 0;
    cin >> n >> m >> w;
    for (int i = 1; i <= n; i++)
        cin >> weight[i];
    for (int i = 1; i <= n; i++)
        cin >> beauty[i];

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            sumWeight = sumBeauty = 0;
            dfs(i);
            if (groups[components].size() > 1) {
                groups[components].push_back(make_pair(sumWeight, sumBeauty));
            }
            components++;
        }
    }

    for (int i = 1; i <= w; i++)
        for (int j = 0; j < components; j++) {
            if (j > 0)
                dp[i][j] = dp[i][j - 1];
            else
                dp[i][j] = dp[i - 1][j];
            for (size_t t = 0; t < groups[j].size(); t++)
                if (i - groups[j].at(t).first >= 0)
                    dp[i][j] = max(dp[i][j], dp[i - groups[j].at(t).first][j - 1] + groups[j].at(t).second);
        }

    printf("%d", dp[w][components - 1]);

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <math.h>

#define ll long long int

using namespace std;

const int MAXN = 1000 + 5;
const int MAXW = 1000 + 5;

vector<int> graph[MAXN];
int weight[MAXN], beauty[MAXN];
bool used[MAXN];

vector <pair<int, int>> groups[MAXN];
int components = 0;
int sumWeight = 0, sumBeauty = 0;

void dfs(int v) {
    used[v] = true;
    sumWeight += weight[v];
    sumBeauty += beauty[v];
    groups[components].push_back(make_pair(weight[v], beauty[v]));
    for (size_t t = 0; t < graph[v].size(); t++)
        if (!used[graph[v][t]])
            dfs(graph[v][t]);
}

int main()
{
    int dp[MAXN][MAXW];
    for (int i = 0; i < MAXN; i++)
        for (int j = 0; j < MAXW; j++)
            dp[i][j] = 0;

    for (int i = 0; i < MAXN; i++)
        used[i] = false;

    int n = 0, m = 0, w = 0;
    cin >> n >> m >> w;
    for (int i = 1; i <= n; i++)
        cin >> weight[i];
    for (int i = 1; i <= n; i++)
        cin >> beauty[i];

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            sumWeight = sumBeauty = 0;
            dfs(i);
            if (groups[components].size() > 1) {
                groups[components].push_back(make_pair(sumWeight, sumBeauty));
            }
            components++;
        }
    }

    for (int i = 1; i <= w; i++)
        for (int j = 0; j < components; j++) {
            if (j > 0)
                dp[i][j] = dp[i][j - 1];
            else
                dp[i][j] = dp[i - 1][j];
            for (size_t t = 0; t < groups[j].size(); t++)
                if (i - groups[j].at(t).first >= 0)
                    dp[i][j] = max(dp[i][j], dp[i - groups[j].at(t).first][j - 1] + groups[j].at(t).second);
        }

    cout << dp[w][components - 1];

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <math.h>

#define ll long long int

using namespace std;

const int MAXN = 1000 + 5;
const int MAXW = 1000 + 5;

vector<int> graph[MAXN];
int weight[MAXN], beauty[MAXN];
bool used[MAXN];

vector <pair<int, int>> groups[MAXN];
int components = 0;
int sumWeight = 0, sumBeauty = 0;

void dfs(int v) {
    used[v] = true;
    sumWeight += weight[v];
    sumBeauty += beauty[v];
    groups[components].push_back(make_pair(weight[v], beauty[v]));
    for (size_t t = 0; t < graph[v].size(); t++)
        if (!used[graph[v][t]])
            dfs(graph[v][t]);
}

int main()
{
    int dp[MAXW][MAXN];
    for (int i = 0; i < MAXW; i++)
        for (int j = 0; j < MAXN; j++)
            dp[i][j] = 0;

    for (int i = 0; i < MAXN; i++)
        used[i] = false;

    int n = 0, m = 0, w = 0;
    cin >> n >> m >> w;
    for (int i = 1; i <= n; i++)
        cin >> weight[i];
    for (int i = 1; i <= n; i++)
        cin >> beauty[i];

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            sumWeight = sumBeauty = 0;
            dfs(i);
            if (groups[components].size() > 1) {
                groups[components].push_back(make_pair(sumWeight, sumBeauty));
            }
            components++;
        }
    }

    for (int i = 1; i <= w; i++)
        for (int j = 0; j < components; j++) {
            if (j > 0)
                dp[i][j] = dp[i][j - 1];
            else
                dp[i][j] = dp[i - 1][j];
            for (size_t t = 0; t < groups[j].size(); t++)
                if (i - groups[j].at(t).first >= 0)
                    dp[i][j] = max(dp[i][j], dp[i - groups[j].at(t).first][j - 1] + groups[j].at(t).second);
        }

    cout << dp[w][components - 1];

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <math.h>

#define ll long long int

using namespace std;

const int MAXN = 1000 + 5;
const int MAXW = 1000 + 5;

vector<int> graph[MAXN];
int weight[MAXN], beauty[MAXN];
bool used[MAXN];

vector <pair<int, int>> groups[MAXN];
int components = 0;
int sumWeight = 0, sumBeauty = 0;

void dfs(int v) {
    used[v] = true;
    sumWeight += weight[v];
    sumBeauty += beauty[v];
    groups[components].push_back(make_pair(weight[v], beauty[v]));
    for (size_t t = 0; t < graph[v].size(); t++)
        if (!used[graph[v][t]])
            dfs(graph[v][t]);
}

int main()
{
    int dp[MAXW][MAXN];
    for (int i = 0; i < MAXW; i++)
        for (int j = 0; j < MAXN; j++)
            dp[i][j] = 0;

    for (int i = 0; i < MAXN; i++)
        used[i] = false;

    int n = 0, m = 0, w = 0;
    cin >> n >> m >> w;
    for (int i = 1; i <= n; i++)
        cin >> weight[i];
    for (int i = 1; i <= n; i++)
        cin >> beauty[i];

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            sumWeight = sumBeauty = 0;
            dfs(i);
            if (groups[components].size() > 1) {
                groups[components].push_back(make_pair(sumWeight, sumBeauty));
            }
            components++;
        }
    }

    for (int i = 1; i <= w; i++)
        for (int j = 0; j < components; j++) {
            if (j > 0)
                dp[i][j] = dp[i][j - 1];
            else
                dp[i][j] = dp[i - 1][j];
            for (size_t t = 0; t < groups[j].size(); t++)
                if (i - groups[j].at(t).first >= 0)
                    dp[i][j] = max(dp[i][j], dp[i - groups[j].at(t).first][j - 1] + groups[j].at(t).second);
        }

    cout << dp[w][components - 1] / 2;

    return 0;
}


#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <vector>

using namespace std;

int main()
{
    return 0;
}


#include <bits/stdc++.h>
void dout() { std::cout << std::endl; }

template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
	#ifdef DESH
		std::cout << H << ' ';
		dout(T...);
	#endif
}

template <typename T>
void doutArr(std::string name, std::vector<T> &a) {
	#ifdef DESH
		std::cout << name;
		for (int i = 0; i < (int) a.size(); i++) {
			std::cout << a[i] << ' ';
		}
		dout();
	#endif
}

struct Item {
	int w, b;
	Item(int w, int b): w(w), b(b) {}
};

struct Group {
	std::vector<Item> items;
	Group() {}
	void add(Item item) {
		items.push_back(item);
	}
	void add(Group group) {
		if (items.size() == 1) {
			items.push_back(items[0]);
		}
		items[0].w += group.items[0].w;
		items[0].b += group.items[0].b;

		if (group.items.size() == 1) {
			items.push_back(group.items[0]);
		} else {
			for (int i = 1; i < (int) group.items.size(); i++) {
				items.push_back(group.items[i]);
			}
		}
	}
};

int find_set(int v, std::vector<int> &parent) {
	if (v == parent[v])
		return v;
	return find_set(parent[v], parent);
}

int main() {
	int n, nPairs, maxW; scanf("%d %d %d", &n, &nPairs, &maxW);
	std::vector<int> w(n);
	std::vector<int> b(n);

	std::vector<Group> groups(n);
	std::vector<int> dsu(n);

	for (int i = 0; i < n; i++) {
		scanf("%d", &w[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);
	}

	for (int i = 0; i < n; i++) {
		groups[i].add(Item(w[i], b[i]));
		dsu[i] = i;
	}

	for (int i = 0; i < nPairs; i++) {
		int x, y; scanf("%d %d", &x, &y);
		x--; y--;

		int px = find_set(x, dsu);
		int py = find_set(y, dsu);

		dout("add", x, y, px, py);

		if (px != py) {
			groups[px].add(groups[py]);
			groups[py].items.clear();
			dsu[py] = dsu[px];
		}

		doutArr("dsu ", dsu);
	}

	std::vector<Group> a;
	for (int i = 0; i < (int) groups.size(); i++) {
		if (groups[i].items.size() == 0) {
			continue;
		}

		a.push_back(groups[i]);
		dout("======== group", a.size());
		for (Item item : a.back().items) {
			dout(item.w, item.b);
		}
	}

	std::vector<std::vector<int>> dp(1 + maxW, std::vector<int>(a.size(), 0));

	int ans = 0;
	for (int curW = 1; curW <= maxW; curW++) {
		for (int curGroup = 0; curGroup < (int) a.size(); curGroup++) {
			Group group = a[curGroup];

			if (curGroup > 0) {
				dp[curW][curGroup] = dp[curW][curGroup - 1];
			}

			for (Item item : group.items) {
				if (curW - item.w < 0) {
					continue;
				}

				if (curGroup == 0) {
					dp[curW][curGroup] = std::max(dp[curW][curGroup], item.b);
				} else {
					dp[curW][curGroup] = std::max(dp[curW][curGroup], dp[curW - item.w][curGroup - 1] + item.b);
				}
			}

			ans = std::max(ans, dp[curW][curGroup]);
			dout(curW, curGroup, dp[curW][curGroup]);
		}
	}

	std::printf("%d", ans);

	return 0;
}


#include <bits/stdc++.h>
void dout() { std::cout << std::endl; }

template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
	#ifdef DESH
		std::cout << H << ' ';
		dout(T...);
	#endif
}

struct Item {
	int w, b;
	Item(int w, int b): w(w), b(b) {}
};

struct Group {
	std::vector<Item> items;
	Group() {}
	void add(Item item) {
		items.push_back(item);
	}
	void add(Group group) {
		if (items.size() == 1) {
			items.push_back(items[0]);
		}
		items[0].w += group.items[0].w;
		items[0].b += group.items[0].b;

		if (group.items.size() == 1) {
			items.push_back(group.items[0]);
		} else {
			for (int i = 1; i < (int) group.items.size(); i++) {
				items.push_back(group.items[i]);
			}
		}
	}
};

int main() {
	int n, nPairs, maxW; scanf("%d %d %d", &n, &nPairs, &maxW);
	std::vector<int> w(n);
	std::vector<int> b(n);

	std::vector<Group> groups(n);
	std::vector<int> dsu(n);

	for (int i = 0; i < n; i++) {
		scanf("%d", &w[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);
	}

	for (int i = 0; i < n; i++) {
		groups[i].add(Item(w[i], b[i]));
		dsu[i] = i;
	}

	for (int i = 0; i < nPairs; i++) {
		int x, y; scanf("%d %d", &x, &y);
		x--; y--;

		if (dsu[x] != dsu[y]) {
			groups[dsu[x]].add(groups[dsu[y]]);
			groups[dsu[y]].items.clear();
			dsu[y] = dsu[x];
		}
	}

	std::vector<Group> a;
	for (int i = 0; i < (int) groups.size(); i++) {
		if (groups[i].items.size() == 0) {
			continue;
		}

		a.push_back(groups[i]);
	}

	std::vector<std::vector<int>> dp(1 + maxW, std::vector<int>(a.size(), 0));

	int ans = 0;
	for (int curW = 1; curW <= maxW; curW++) {
		for (int curGroup = 0; curGroup < (int) a.size(); curGroup++) {
			Group group = a[curGroup];

			if (curGroup > 0) {
				dp[curW][curGroup] = dp[curW][curGroup - 1];
			}

			for (Item item : group.items) {
				if (curW - item.w < 0) {
					continue;
				}

				if (curGroup == 0) {
					dp[curW][curGroup] = std::max(dp[curW][curGroup], item.b);
				} else {
					dp[curW][curGroup] = std::max(dp[curW][curGroup], dp[curW - item.w][curGroup - 1] + item.b);
				}
			}

			ans = std::max(ans, dp[curW][curGroup]);
			dout(curW, curGroup, dp[curW][curGroup]);
		}
	}

	std::printf("%d", ans);

	return 0;
}


#include <bits/stdc++.h>
void dout() { std::cout << std::endl; }

template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
	#ifdef DESH
		std::cout << H << ' ';
		dout(T...);
	#endif
}

struct Item {
	int count;
	int sumW;
	int sumB;
	int bestW;
	int bestB;
	Item(): count(0), sumW(0), sumB(0), bestW(0), bestB(0) {}
	void clear() {
		count = 0;
		sumW = 0;
		sumB = 0;
		bestW = 0;
		bestB = 0;
	}
	void add(int w, int b) {
		count++;
		sumW += w;
		sumB += b;
		if (b * bestW > bestB * w) {
			bestB = b;
			bestW = w;
		}
	}

	void add(Item item) {
		count += item.count;
		sumW += item.sumW;
		sumB += item.sumB;

		if (item.bestB * bestW > bestB * item.bestW) {
			bestB = item.bestB;
			bestW = item.bestW;
		}
	}
};

struct Group {
	int id, w, b;
	Group(int id, int w, int b):id(id), w(w), b(b) {}
};

int main() {
	int n, nPairs, maxW; scanf("%d %d %d", &n, &nPairs, &maxW);
	std::vector<int> w(n);
	std::vector<int> b(n);

	std::vector<Item> items(n);
	std::vector<int> dsu(n);

	for (int i = 0; i < n; i++) {
		scanf("%d", &w[i]);

		items[i].sumW = w[i];
		items[i].bestW = w[i];
		dsu[i] = i;
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);

		items[i].sumB = b[i];
		items[i].bestB = b[i];
	}

	for (int i = 0; i < nPairs; i++) {
		int x, y; scanf("%d %d", &x, &y);
		x--; y--;

		if (dsu[x] != dsu[y]) {
			items[dsu[x]].add(items[dsu[y]]);
			items[dsu[y]].sumW = 0;
			dsu[y] = dsu[x];
		}
	}

	std::vector<Item> a;
	for (int i = 0; i < (int) items.size(); i++) {
		if (items[i].sumW == 0) {
			continue;
		}

		dout("item=", items[i].sumW, items[i].sumB, items[i].bestW, items[i].bestB);
		a.push_back(items[i]);
	}

	std::vector<std::vector<int>> dp(1 + maxW, std::vector<int>(a.size(), 0));

	int ans = 0;
	for (int curW = 1; curW <= maxW; curW++) {
		for (int curItem = 0; curItem < (int) a.size(); curItem++) {
			Item item = a[curItem];

			if (curW - item.sumW >= 0) {
				if (curItem == 0) {
					dp[curW][curItem] = std::max(dp[curW][curItem], item.sumB);
				} else {
					dp[curW][curItem] = std::max(dp[curW][curItem], dp[curW - item.sumW][curItem - 1] + item.sumB);
				}
				ans = std::max(ans, dp[curW][curItem]);
			}
			if (curW - item.bestW >= 0) {
				if (curItem == 0) {
					dp[curW][curItem] = std::max(dp[curW][curItem], item.bestB);
				} else {
					dp[curW][curItem] = std::max(dp[curW][curItem], dp[curW - item.bestW][curItem - 1] + item.bestB);
				}
				ans = std::max(ans, dp[curW][curItem]);
			}
		}
	}

	std::printf("%d", ans);

	return 0;
}


#include <bits/stdc++.h>
void dout() { std::cout << std::endl; }

template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
	#ifdef DESH
		std::cout << H << ' ';
		dout(T...);
	#endif
}

struct Item {
	int count;
	int sumW;
	int sumB;
	int bestW;
	int bestB;
	Item(): count(0), sumW(0), sumB(0), bestW(0), bestB(0) {}
	void clear() {
		count = 0;
		sumW = 0;
		sumB = 0;
		bestW = 0;
		bestB = 0;
	}
	void add(int w, int b) {
		count++;
		sumW += w;
		sumB += b;
		if (b * bestW > bestB * w) {
			bestB = b;
			bestW = w;
		}
	}

	void add(Item item) {
		count += item.count;
		sumW += item.sumW;
		sumB += item.sumB;

		if (item.bestB * bestW > bestB * item.bestW) {
			bestB = item.bestB;
			bestW = item.bestW;
		}
	}
};

struct Group {
	int id, w, b;
	Group(int id, int w, int b):id(id), w(w), b(b) {}
};

int main() {
	int n, nPairs, maxW; scanf("%d %d %d", &n, &nPairs, &maxW);
	std::vector<int> w(n);
	std::vector<int> b(n);

	std::vector<Item> items(n);
	std::vector<int> dsu(n);

	for (int i = 0; i < n; i++) {
		scanf("%d", &w[i]);

		items[i].sumW = w[i];
		items[i].bestW = w[i];
		dsu[i] = i;
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);

		items[i].sumB = b[i];
		items[i].bestB = b[i];
	}

	for (int i = 0; i < nPairs; i++) {
		int x, y; scanf("%d %d", &x, &y);
		x--; y--;

		if (dsu[x] != dsu[y]) {
			items[dsu[x]].add(items[dsu[y]]);
			items[dsu[y]].sumW = 0;
			dsu[y] = dsu[x];
		}
	}

	std::vector<Item> a;
	for (int i = 0; i < (int) items.size(); i++) {
		if (items[i].sumW == 0) {
			continue;
		}

		dout("item=", items[i].sumW, items[i].sumB, items[i].bestW, items[i].bestB);
		a.push_back(items[i]);
	}

	std::vector<std::vector<int>> dp(1 + maxW, std::vector<int>(a.size(), 0));

	int ans = 0;
	for (int curW = 1; curW <= maxW; curW++) {
		for (int curItem = 0; curItem < (int) a.size(); curItem++) {
			Item item = a[curItem];

			if (curItem > 0) {
				dp[curW][curItem] = dp[curW][curItem - 1];
			}

			if (curW - item.sumW >= 0) {
				if (curItem == 0) {
					dp[curW][curItem] = std::max(dp[curW][curItem], item.sumB);
				} else {
					dp[curW][curItem] = std::max(dp[curW][curItem], dp[curW - item.sumW][curItem - 1] + item.sumB);
				}
				ans = std::max(ans, dp[curW][curItem]);
			}
			if (curW - item.bestW >= 0) {
				if (curItem == 0) {
					dp[curW][curItem] = std::max(dp[curW][curItem], item.bestB);
				} else {
					dp[curW][curItem] = std::max(dp[curW][curItem], dp[curW - item.bestW][curItem - 1] + item.bestB);
				}
				ans = std::max(ans, dp[curW][curItem]);
			}

			dout(curW, curItem, dp[curW][curItem]);
		}
	}

	std::printf("%d", ans);

	return 0;
}


/**********************************************************************\
\     _       _                                               ____     /
/     |\     /|   oooo     ssss       H     H     II  U    U  |   \    \
\     | \   / |  oo  oo   ss    s     H     H     II  U    U  |    |   /
/     |  \ /  |  oo  oo    ss         H     H     II  U    U  |__ /    \
\     |   v   |  oo  oo      ss       HHHHHHH     II  U    U  |   \    /
/    _|       |_  oooo    s    ss     H     H     II   uuuu  _|    \_  \
\                          ssss       H     H     II                   /
/                                     H     H                          \
\                                                                      /
/   common handle names: "moshiur.bd"  or "moshiur_bd"                 \
\**********************************************************************/

#include<bits/stdc++.h>
using namespace std;
#define inf 999999
//#define MAX 100000
#define gcd(a,b) __gcd(a,b)
#define i64 long long
int getInt(){int x;scanf("%d",&x);return x;}
long long getLongLong(){long long x;scanf("%I64d",&x);return x;}
double getDouble(){double x;scanf("%lf",&x);return x;}
char  getChar(){char x;scanf("%c",&x);return x;}
#define Int getInt()
#define Char getChar()
#define I64 getLongLong()
#define Double getDouble()
#define bug printf("debug\n");
#define br puts("");
#define rep(i,n) for(int i=0;i<n;i++)
#define lcm(a,b) a*b/gcd(a,b)
#define pb push_back
#define vi vector<int>
#define ii pair<int,int>
#define dd pair<double,double>
#define ll long long
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define EPS numeric_limits<double>::epsilon()
#define popcount __builtin_popcount
template<typename t>
t abs(t a)
{
    if(a>=0)
        return a;
    return -a;
}
//error
#define error(args...) { vector<string> _v = split(#args, ','); err(_v.begin(), args); puts(""); }

vector<string> split(const string& s, char c) {
	vector<string> v;
	stringstream ss(s);
	string x;
	while (getline(ss, x, c))
		v.emplace_back(x);
	return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
	cout << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << "  ";
	err(++it, args...);
}
//end

void FastIO()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
}

////////////////////////////////////
const int Max=1123;
int par[Max+10]={0};
int findP(int x){
    if(x==par[x]) return x;
    return par[x]=findP(par[x]);
}

void  Union(int u,int v){

    int pu=findP(u);
    int pv=findP(v);
    if(pu!=pv)
        par[pv]=pu;

    return ;
}

vi p[Max];
int b[Max]={0};
int bp[Max]={0};
int w[Max]={0};
int wp[Max]={0};
int n,W;

vi a;

int dp[Max][Max];
bool vis[Max]={0};

int calc(int x=0,int y=W,int z=0){
    //error(x,y,z);
    if(x>=n) return z;
    int u=a[x];
    int pu=findP(u);

    int &ans=dp[x][y];
    if(ans!=-1) return z+ans;
    ans=0;
    if(!vis[pu]){
        vis[pu]=1;

        if(w[u]<=y){
            ans=max(ans,calc(x+1,y-w[u],z+b[u])-z);
        }
        if(wp[pu]<=y){
            ans=max(ans,calc(x+1,y-wp[pu],z+bp[pu])-z);
        }
        vis[pu]=0;
    }

    ans=max(ans,calc(x+1,y,z)-z);

    return ans+z;

}

int main()
{
    //freopen("in.txt","r",stdin);
    rep(i,Max)par[i]=i;
    int m;
    cin>>n>>m>>W;

    for(int i=1;i<=n;i++)
        w[i]=Int;

    for(int i=1;i<=n;i++)
        b[i]=Int;

    int u,v;
    rep(i,m){
        scanf("%d%d",&u,&v);
        Union(u,v);
    }

    ///safety

    for(int i=1;i<=n;i++)
        findP(i);

    for(int i=1;i<=n;i++)
        p[findP(i)].pb(i);

    for(int i=1;i<=n;i++)
    {
        wp[i]=0;
        bp[i]=0;
        for(auto xx:p[i]){
            a.pb(xx);
            wp[i]+=w[xx];
            bp[i]+=b[xx];
        }
    }

    memset(dp,-1,sizeof dp);

    //error(n,a.size());

    cout<<calc()<<endl;

    return 0;

}


/**********************************************************************\
\     _       _                                               ____     /
/     |\     /|   oooo     ssss       H     H     II  U    U  |   \    \
\     | \   / |  oo  oo   ss    s     H     H     II  U    U  |    |   /
/     |  \ /  |  oo  oo    ss         H     H     II  U    U  |__ /    \
\     |   v   |  oo  oo      ss       HHHHHHH     II  U    U  |   \    /
/    _|       |_  oooo    s    ss     H     H     II   uuuu  _|    \_  \
\                          ssss       H     H     II                   /
/                                     H     H                          \
\                                                                      /
/   common handle names: "moshiur.bd"  or "moshiur_bd"                 \
\**********************************************************************/

#include<bits/stdc++.h>
using namespace std;
#define inf 999999
//#define MAX 100000
#define gcd(a,b) __gcd(a,b)
#define i64 long long
int getInt(){int x;scanf("%d",&x);return x;}
long long getLongLong(){long long x;scanf("%I64d",&x);return x;}
double getDouble(){double x;scanf("%lf",&x);return x;}
char  getChar(){char x;scanf("%c",&x);return x;}
#define Int getInt()
#define Char getChar()
#define I64 getLongLong()
#define Double getDouble()
#define bug printf("debug\n");
#define br puts("");
#define rep(i,n) for(int i=0;i<n;i++)
#define lcm(a,b) a*b/gcd(a,b)
#define pb push_back
#define vi vector<int>
#define ii pair<int,int>
#define dd pair<double,double>
#define ll long long
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define EPS numeric_limits<double>::epsilon()
#define popcount __builtin_popcount
template<typename t>
t abs(t a)
{
    if(a>=0)
        return a;
    return -a;
}
//error
#define error(args...) { vector<string> _v = split(#args, ','); err(_v.begin(), args); puts(""); }

vector<string> split(const string& s, char c) {
	vector<string> v;
	stringstream ss(s);
	string x;
	while (getline(ss, x, c))
		v.emplace_back(x);
	return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
	cout << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << "  ";
	err(++it, args...);
}
//end

void FastIO()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
}

////////////////////////////////////
const int Max=1123;
int par[Max+10]={0};
int findP(int x){
    if(x==par[x]) return x;
    return par[x]=findP(par[x]);
}

void  Union(int u,int v){

    int pu=findP(u);
    int pv=findP(v);
    if(pu!=pv)
        par[pv]=pu;

    return ;
}

vi p[Max];
int b[Max]={0};
int bp[Max]={0};
int w[Max]={0};
int wp[Max]={0};
int n,W;
int to[Max];
vi a;

int dp[Max][Max];
bool vis[Max]={0};

int calc(int x=0,int y=W,int z=0){
    //error(x,y,z);
    if(x>=n) return z;
    int u=a[x];
    int pu=findP(u);

    int &ans=dp[x][y];
    if(ans!=-1) return z+ans;
    ans=0;
    if(!vis[pu]){
        vis[pu]=1;

        if(w[u]<=y){
            ans=max(ans,calc(x+to[x],y-w[u],z+b[u])-z);
        }
        if(wp[pu]<=y){
            ans=max(ans,calc(x+to[x],y-wp[pu],z+bp[pu])-z);
        }
        vis[pu]=0;
    }

    ans=max(ans,calc(x+1,y,z)-z);

    return ans+z;

}

int main()
{
    //freopen("in.txt","r",stdin);
    rep(i,Max)par[i]=i;
    int m;
    cin>>n>>m>>W;

    for(int i=1;i<=n;i++)
        w[i]=Int;

    for(int i=1;i<=n;i++)
        b[i]=Int;

    int u,v;
    rep(i,m){
        scanf("%d%d",&u,&v);
        Union(u,v);
    }

    ///safety

    for(int i=1;i<=n;i++)
        findP(i);

    for(int i=1;i<=n;i++)
        p[findP(i)].pb(i);

    for(int i=1;i<=n;i++)
    {
        wp[i]=0;
        bp[i]=0;
        int tx=p[i].size();
        for(auto xx:p[i]){
            to[(int)a.size()]=tx;
            a.pb(xx);
            wp[i]+=w[xx];
            bp[i]+=b[xx];
            tx--;

        }
    }

    memset(dp,-1,sizeof dp);

    //error(n,a.size());

    cout<<calc()<<endl;

    return 0;

}


/**********************************************************************\
\     _       _                                               ____     /
/     |\     /|   oooo     ssss       H     H     II  U    U  |   \    \
\     | \   / |  oo  oo   ss    s     H     H     II  U    U  |    |   /
/     |  \ /  |  oo  oo    ss         H     H     II  U    U  |__ /    \
\     |   v   |  oo  oo      ss       HHHHHHH     II  U    U  |   \    /
/    _|       |_  oooo    s    ss     H     H     II   uuuu  _|    \_  \
\                          ssss       H     H     II                   /
/                                     H     H                          \
\                                                                      /
/   common handle names: "moshiur.bd"  or "moshiur_bd"                 \
\**********************************************************************/

#include<bits/stdc++.h>
using namespace std;
#define inf 999999
//#define MAX 100000
#define gcd(a,b) __gcd(a,b)
#define i64 long long
int getInt(){int x;scanf("%d",&x);return x;}
long long getLongLong(){long long x;scanf("%I64d",&x);return x;}
double getDouble(){double x;scanf("%lf",&x);return x;}
char  getChar(){char x;scanf("%c",&x);return x;}
#define Int getInt()
#define Char getChar()
#define I64 getLongLong()
#define Double getDouble()
#define bug printf("debug\n");
#define br puts("");
#define rep(i,n) for(int i=0;i<n;i++)
#define lcm(a,b) a*b/gcd(a,b)
#define pb push_back
#define vi vector<int>
#define ii pair<int,int>
#define dd pair<double,double>
#define ll long long
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define EPS numeric_limits<double>::epsilon()
#define popcount __builtin_popcount
template<typename t>
t abs(t a)
{
    if(a>=0)
        return a;
    return -a;
}
//error
#define error(args...) { vector<string> _v = split(#args, ','); err(_v.begin(), args); puts(""); }

vector<string> split(const string& s, char c) {
	vector<string> v;
	stringstream ss(s);
	string x;
	while (getline(ss, x, c))
		v.emplace_back(x);
	return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
	cout << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << "  ";
	err(++it, args...);
}
//end

void FastIO()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
}

////////////////////////////////////
const int Max=1123;
int par[Max]={0};
int findP(int x){
    if(x==par[x]) return x;
    return par[x]=findP(par[x]);
}

void  Union(int u,int v){

    int pu=findP(u);
    int pv=findP(v);
    if(pu!=pv)
        par[pv]=pu;

    return ;
}

vi p[Max];
int b[Max]={0};
int bp[Max]={0};
int w[Max]={0};
int wp[Max]={0};
int n,W;

vi a;

int dp[Max][Max];
bool vis[Max]={0};

int calc(int x=0,int y=W,int z=0){
    //error(x,y,z);
    if(x>=n) return z;
    int u=a[x];
    int pu=findP(u);

    int &ans=dp[x][y];
    if(ans!=-1) return z+ans;
    ans=0;
    if(!vis[pu]){
        vis[pu]=1;

        if(w[u]<=y){
            ans=max(ans,calc(x+1,y-w[u],z+b[u])-z);
        }
        if(wp[pu]<=y){
            ans=max(ans,calc(x+1,y-wp[pu],z+bp[pu])-z);
        }
        vis[pu]=0;
    }

    ans=max(ans,calc(x+1,y,z)-z);

    return ans+z;

}

int main()
{
    //freopen("in.txt","r",stdin);
    rep(i,Max)par[i]=i;
    int m;
    cin>>n>>m>>W;

    for(int i=1;i<=n;i++)
        w[i]=Int;

    for(int i=1;i<=n;i++)
        b[i]=Int;

    int u,v;
    rep(i,n){
        scanf("%d%d",&u,&v);
        Union(u,v);
    }

    ///safety

    for(int i=1;i<=n;i++)
        findP(i);

    for(int i=1;i<=n;i++)
        p[findP(i)].pb(i);

    for(int i=1;i<=n;i++)
    {
        wp[i]=0;
        bp[i]=0;
        for(auto xx:p[i]){
            a.pb(xx);
            wp[i]+=w[xx];
            bp[i]+=b[xx];
        }
    }

    memset(dp,-1,sizeof dp);

    cout<<calc()<<endl;

    return 0;

}


/**********************************************************************\
\     _       _                                               ____     /
/     |\     /|   oooo     ssss       H     H     II  U    U  |   \    \
\     | \   / |  oo  oo   ss    s     H     H     II  U    U  |    |   /
/     |  \ /  |  oo  oo    ss         H     H     II  U    U  |__ /    \
\     |   v   |  oo  oo      ss       HHHHHHH     II  U    U  |   \    /
/    _|       |_  oooo    s    ss     H     H     II   uuuu  _|    \_  \
\                          ssss       H     H     II                   /
/                                     H     H                          \
\                                                                      /
/   common handle names: "moshiur.bd"  or "moshiur_bd"                 \
\**********************************************************************/

#include<bits/stdc++.h>
using namespace std;
#define inf 999999
//#define MAX 100000
#define gcd(a,b) __gcd(a,b)
#define i64 long long
int getInt(){int x;scanf("%d",&x);return x;}
long long getLongLong(){long long x;scanf("%I64d",&x);return x;}
double getDouble(){double x;scanf("%lf",&x);return x;}
char  getChar(){char x;scanf("%c",&x);return x;}
#define Int getInt()
#define Char getChar()
#define I64 getLongLong()
#define Double getDouble()
#define bug printf("debug\n");
#define br puts("");
#define rep(i,n) for(int i=0;i<n;i++)
#define lcm(a,b) a*b/gcd(a,b)
#define pb push_back
#define vi vector<int>
#define ii pair<int,int>
#define dd pair<double,double>
#define ll long long
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define EPS numeric_limits<double>::epsilon()
#define popcount __builtin_popcount
template<typename t>
t abs(t a)
{
    if(a>=0)
        return a;
    return -a;
}
//error
#define error(args...) { vector<string> _v = split(#args, ','); err(_v.begin(), args); puts(""); }

vector<string> split(const string& s, char c) {
	vector<string> v;
	stringstream ss(s);
	string x;
	while (getline(ss, x, c))
		v.emplace_back(x);
	return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
	cout << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << "  ";
	err(++it, args...);
}
//end

void FastIO()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
}

////////////////////////////////////
const int Max=1123;
int par[Max+10]={0};
int findP(int x){
    if(x==par[x]) return x;
    return par[x]=findP(par[x]);
}

void  Union(int u,int v){
    par[findP(u)]=findP(v);
}

vi p[Max];
int b[Max]={0};
int bp[Max]={0};
int w[Max]={0};
int wp[Max]={0};
int n,W;
int to[Max];
vi a;

int dp[Max][Max];
bool vis[Max]={0};

int calc(int x=0,int y=W,int z=0){
    //error(x,y,z);
    if(x>=n) return z;
    int u=a[x];
    int pu=findP(u);

    int &ans=dp[x][y];
    if(ans!=-1) return z+ans;
    ans=0;
    if(!vis[pu]){
        vis[pu]=1;

        if(w[u]<=y){
            ans=max(ans,calc(x+to[x],y-w[u],z+b[u])-z);
        }
        if(wp[pu]<=y){
            ans=max(ans,calc(x+to[x],y-wp[pu],z+bp[pu])-z);
        }
        vis[pu]=0;
    }

    ans=max(ans,calc(x+1,y,z)-z);

    return ans+z;

}

int main()
{
    //freopen("in.txt","r",stdin);
    rep(i,Max)par[i]=i;
    int m;
    cin>>n>>m>>W;

    for(int i=1;i<=n;i++)
        w[i]=Int;

    for(int i=1;i<=n;i++)
        b[i]=Int;

    int u,v;
    rep(i,m){
        scanf("%d%d",&u,&v);
        Union(u,v);
    }

    ///safety

    for(int i=1;i<=n;i++)
        findP(i);

    for(int i=1;i<=n;i++)
        p[findP(i)].pb(i);

    for(int i=1;i<=n;i++)
    {
        wp[i]=0;
        bp[i]=0;
        int tx=p[i].size();
        for(auto xx:p[i]){
            to[(int)a.size()]=tx;
            a.pb(xx);
            wp[i]+=w[xx];
            bp[i]+=b[xx];
            tx--;

        }
    }

    memset(dp,-1,sizeof dp);

    //error(n,a.size());

    cout<<calc()<<endl;

    return 0;

}


#include <iostream>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <utility>
#include <string.h>
#include <map>
#include <stack>
#include <iomanip>
#include <chrono>
#include <random>
#include <math.h>
#include <time.h>
#include <assert.h>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) x.begin(),x.end()
#define PII pair <ll,ll>
#define N 300005
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int w[1005], b[1005], dp[1005][1005];
bool vis[1005];
int c = 0, we = 0, bt = 0;
vector <int> v[1005];
vector <pair <int, int> > gr[1005];
void dfs(int s)
{
	we += w[s];
	bt += b[s];
	vis[s] = true;
	gr[c].push_back({w[s], b[s]});
	for (auto g : v[s])
	{
		if(!vis[g])
			dfs(g);
	}
}
int main()
{
	int n, m, tw, x, y;
	cin >> n >> m >> tw;
	for (int i = 1; i <= n; ++i)
	{
		cin >> w[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
	}
	for (int i = 0; i < m; ++i)
	{
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= n; ++i)
	{
		if(!vis[i])
		{
			we = 0;
			bt = 0;
			c++;
			dfs(i);
			gr[c].push_back({we, bt});
		}
	}
	memset(dp, 0, sizeof(dp));
	for (int i = 1; i <= c; ++i)
	{
		for (auto g : gr[i])
		{
			we = g.ff;
			bt = g.ss;
			for (int wt = 1; wt <= tw; ++wt)
			{
				if(we <= wt)
				{
					dp[i][wt] = max(max(dp[i][wt], dp[i - 1][wt]), dp[i - 1][wt - we] + bt);
				}
				else
				{
					dp[i][wt] = max(dp[i][wt], dp[i - 1][wt]);
				}
			}

 		}
	}
	cout << dp[c][tw];
}


#include <iostream>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <utility>
#include <string.h>
#include <map>
#include <stack>
#include <iomanip>
#include <chrono>
#include <random>
#include <math.h>
#include <time.h>
#include <assert.h>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) x.begin(),x.end()
#define PII pair <ll,ll>
#define N 300005
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int w[1005], b[1005], dp[1005][1005];
bool vis[1005];
int c = 0;
vector <int> v[105], gr[105];
void dfs(int s)
{
	vis[s] = true;
	gr[c].push_back(s);
	for (auto g : v[s])
	{
		if(!vis[g])
			dfs(g);
	}
}
int main()
{
	int n, m, tw, x, y, bt, we;
	cin >> n >> m >> tw;
	for (int i = 1; i <= n; ++i)
	{
		cin >> w[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
	}
	for (int i = 0; i < m; ++i)
	{
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= n; ++i)
	{
		if(!vis[i])
		{
			c++;
			dfs(i);
		}
	}
	memset(dp, 0, sizeof(dp));
	for (int i = 1; i <= c; ++i)
	{
		we = 0;
		bt = 0;
		for (auto g : gr[i])
		{
			we += w[g];
			bt += b[g];
		}
		for (int wt = 1; wt <= tw; ++wt)
		{
			if(we <= wt)
			{
				dp[i][wt] = max(dp[i][wt], dp[i - 1][wt - we] + bt);
			}
			else
			{
				dp[i][wt] = dp[i - 1][wt];
			}
		}
	}
	for (int i = 1; i <= c; ++i)
	{
		for (auto g : gr[i])
		{
			for (int wt = 1; wt <= tw; ++wt)
			{
				if(w[g] <= wt)
				{
					dp[i][wt] = max(dp[i][wt], dp[i - 1][wt - w[g]] + b[g]);
				}
				else
				{
					dp[i][wt] = max(dp[i - 1][wt], dp[i][wt]);
				}
			}
		}
	}
	cout << dp[c][tw];
}


#include <iostream>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <utility>
#include <string.h>
#include <map>
#include <stack>
#include <iomanip>
#include <chrono>
#include <random>
#include <math.h>
#include <time.h>
#include <assert.h>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) x.begin(),x.end()
#define PII pair <ll,ll>
#define N 300005
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int w[1005], b[1005], dp[1005][1005];
bool vis[1005];
int c = 0;
vector <int> v[1005], gr[1005];
void dfs(int s)
{
	vis[s] = true;
	gr[c].push_back(s);
	for (auto g : v[s])
	{
		if(!vis[g])
			dfs(g);
	}
}
int main()
{
	int n, m, tw, x, y, bt, we;
	cin >> n >> m >> tw;
	for (int i = 1; i <= n; ++i)
	{
		cin >> w[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
	}
	for (int i = 0; i < m; ++i)
	{
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= n; ++i)
	{
		if(!vis[i])
		{
			c++;
			dfs(i);
		}
	}
	memset(dp, 0, sizeof(dp));
	for (int i = 1; i <= c; ++i)
	{
		we = 0;
		bt = 0;
		for (auto g : gr[i])
		{
			we += w[g];
			bt += b[g];
		}
		for (int wt = 1; wt <= tw; ++wt)
		{
			if(we <= wt)
			{
				dp[i][wt] = max(max(dp[i - 1][wt], dp[i][wt]), dp[i - 1][wt - we] + bt);
			}
			else
			{
				dp[i][wt] = max(dp[i - 1][wt], dp[i][wt]);
			}
		}
	}
	for (int i = 1; i <= c; ++i)
	{
		for (auto g : gr[i])
		{
			for (int wt = 1; wt <= tw; ++wt)
			{
				if(w[g] <= wt)
				{
					dp[i][wt] = max(max(dp[i - 1][wt], dp[i][wt]), dp[i - 1][wt - w[g]] + b[g]);
					// cout << i << " " << wt << " " << dp[i][wt] << "\n";
				}
				else
				{
					dp[i][wt] = max(dp[i - 1][wt], dp[i][wt]);
				}
			}
		}
	}
	cout << dp[c][tw];
}


#include <iostream>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <utility>
#include <string.h>
#include <map>
#include <stack>
#include <iomanip>
#include <chrono>
#include <random>
#include <math.h>
#include <time.h>
#include <assert.h>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) x.begin(),x.end()
#define PII pair <ll,ll>
#define N 300005
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int w[1005], b[1005], dp[1005][1005];
bool vis[1005];
int c = 0;
vector <int> v[1005], gr[1005];
void dfs(int s)
{
	vis[s] = true;
	gr[c].push_back(s);
	for (auto g : v[s])
	{
		if(!vis[g])
			dfs(g);
	}
}
int main()
{
	int n, m, tw, x, y, bt, we;
	cin >> n >> m >> tw;
	for (int i = 1; i <= n; ++i)
	{
		cin >> w[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
	}
	for (int i = 0; i < m; ++i)
	{
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= n; ++i)
	{
		if(!vis[i])
		{
			c++;
			dfs(i);
		}
	}
	memset(dp, 0, sizeof(dp));
	for (int i = 1; i <= c; ++i)
	{
		we = 0;
		bt = 0;
		for (auto g : gr[i])
		{
			we += w[g];
			bt += b[g];
		}
		for (int wt = 1; wt <= tw; ++wt)
		{
			if(we <= wt)
			{
				dp[i][wt] = max(dp[i - 1][wt], dp[i - 1][wt - we] + bt);
			}
			else
			{
				dp[i][wt] = dp[i - 1][wt];
			}
		}
	}
	for (int i = 1; i <= c; ++i)
	{
		for (auto g : gr[i])
		{
			for (int wt = 1; wt <= tw; ++wt)
			{
				if(w[g] <= wt)
				{
					dp[i][wt] = max(max(dp[i - 1][wt], dp[i][wt]), dp[i - 1][wt - w[g]] + b[g]);
					// cout << i << " " << wt << " " << dp[i][wt] << "\n";
				}
				else
				{
					dp[i][wt] = max(dp[i - 1][wt], dp[i][wt]);
				}
			}
		}
	}
	for (int i = 1; i <= tw; ++i)
	{
		dp[c][i] = max(dp[c][i - 1], dp[c][i]);
	}
	cout << dp[c][tw];
}


#include <iostream>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <utility>
#include <string.h>
#include <map>
#include <stack>
#include <iomanip>
#include <chrono>
#include <random>
#include <math.h>
#include <time.h>
#include <assert.h>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) x.begin(),x.end()
#define PII pair <ll,ll>
#define N 300005
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int w[1005], b[1005], dp[1005][1005];
bool vis[1005];
int c = 0;
vector <int> v[1005], gr[1005];
void dfs(int s)
{
	vis[s] = true;
	gr[c].push_back(s);
	for (auto g : v[s])
	{
		if(!vis[g])
			dfs(g);
	}
}
int main()
{
	int n, m, tw, x, y, bt, we;
	cin >> n >> m >> tw;
	for (int i = 1; i <= n; ++i)
	{
		cin >> w[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
	}
	for (int i = 0; i < m; ++i)
	{
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= n; ++i)
	{
		if(!vis[i])
		{
			c++;
			dfs(i);
		}
	}
	memset(dp, 0, sizeof(dp));
	// for (int i = 1; i <= c; ++i)
	// {
	// 	we = 0;
	// 	bt = 0;
	// 	for (auto g : gr[i])
	// 	{
	// 		we += w[g];
	// 		bt += b[g];
	// 	}
	// 	for (int wt = 1; wt <= tw; ++wt)
	// 	{
	// 		if(we <= wt)
	// 		{
	// 			dp[i][wt] = max(max(dp[i - 1][wt], dp[i][wt]), dp[i - 1][wt - we] + bt);
	// 		}
	// 		else
	// 		{
	// 			dp[i][wt] = max(dp[i - 1][wt], dp[i][wt]);
	// 		}
	// 	}
	// }
	for (int i = 1; i <= c; ++i)
	{
		we = 0;
		bt = 0;
		for (auto g : gr[i])
		{
			for (int wt = 1; wt <= tw; ++wt)
			{
				if(w[g] <= wt)
				{
					dp[i][wt] = max(max(dp[i - 1][wt], dp[i][wt]), dp[i - 1][wt - w[g]] + b[g]);
					// cout << i << " " << wt << " " << dp[i][wt] << "\n";
				}
				else
				{
					dp[i][wt] = max(dp[i - 1][wt], dp[i][wt]);
				}
			}
			we += w[g];
			bt += b[g];
		}
		for (int wt = 1; wt <= tw; ++wt)
		{
			if(we <= wt)
			{
				dp[i][wt] = max(max(dp[i - 1][wt], dp[i][wt]), dp[i - 1][wt - we] + bt);
			}
			else
			{
				dp[i][wt] = max(dp[i - 1][wt], dp[i][wt]);
			}
		}
	}
	// for (int i = 1; i <= tw; ++i)
	// {
	// 	dp[c][i] = max(dp[c][i - 1], dp[c][i]);
	// }
	cout << dp[c][tw];
}


#include <iostream>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <utility>
#include <string.h>
#include <map>
#include <stack>
#include <iomanip>
#include <chrono>
#include <random>
#include <math.h>
#include <time.h>
#include <assert.h>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) x.begin(),x.end()
#define PII pair <ll,ll>
#define N 300005
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int w[1005], b[1005], dp[1005][1005];
bool vis[1005];
int c = 0;
vector <int> v[105], gr[105];
void dfs(int s)
{
	vis[s] = true;
	gr[c].push_back(s);
	for (auto g : v[s])
	{
		if(!vis[g])
			dfs(g);
	}
}
int main()
{
	int n, m, tw, x, y, bt, we;
	cin >> n >> m >> tw;
	for (int i = 1; i <= n; ++i)
	{
		cin >> w[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
	}
	for (int i = 0; i < m; ++i)
	{
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= n; ++i)
	{
		if(!vis[i])
		{
			c++;
			dfs(i);
		}
	}
	memset(dp, 0, sizeof(dp));
	for (int i = 1; i <= c; ++i)
	{
		we = 0;
		bt = 0;
		for (auto g : gr[i])
		{
			we += w[g];
			bt += b[g];
		}
		for (int wt = 1; wt <= tw; ++wt)
		{
			if(we <= wt)
			{
				dp[i][wt] = max(dp[i - 1][wt], dp[i][wt - we] + bt);
			}
			else
			{
				dp[i][wt] = dp[i - 1][wt];
			}
		}
	}
	for (int i = 1; i <= c; ++i)
	{
		for (auto g : gr[i])
		{
			for (int wt = 1; wt <= tw; ++wt)
			{
				if(w[g] <= wt)
				{
					dp[i][wt] = max(dp[i][wt], dp[i - 1][wt - w[g]] + b[g]);
				}
				else
				{
					dp[i][wt] = max(dp[i - 1][wt], dp[i][wt]);
				}
			}
		}
	}
	cout << dp[c][tw];
}


#include <iostream>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <utility>
#include <string.h>
#include <map>
#include <stack>
#include <iomanip>
#include <chrono>
#include <random>
#include <math.h>
#include <time.h>
#include <assert.h>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) x.begin(),x.end()
#define PII pair <ll,ll>
#define N 300005
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int w[1005], b[1005], dp[1005][1005];
bool vis[1005];
int c = 0;
vector <int> v[105], gr[105];
void dfs(int s)
{
	vis[s] = true;
	gr[c].push_back(s);
	for (auto g : v[s])
	{
		if(!vis[g])
			dfs(g);
	}
}
int main()
{
	int n, m, tw, x, y, bt, we;
	cin >> n >> m >> tw;
	for (int i = 1; i <= n; ++i)
	{
		cin >> w[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
	}
	for (int i = 0; i < m; ++i)
	{
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= n; ++i)
	{
		if(!vis[i])
		{
			c++;
			dfs(i);
		}
	}
	memset(dp, 0, sizeof(dp));
	for (int i = 1; i <= c; ++i)
	{
		we = 0;
		bt = 0;
		for (auto g : gr[i])
		{
			we += w[g];
			bt += b[g];
		}
		for (int wt = 1; wt <= tw; ++wt)
		{
			if(we <= wt)
			{
				dp[i][wt] = max(dp[i - 1][wt], dp[i - 1][wt - we] + bt);
			}
			else
			{
				dp[i][wt] = dp[i - 1][wt];
			}
		}
	}
	for (int i = 1; i <= c; ++i)
	{
		for (auto g : gr[i])
		{
			for (int wt = 1; wt <= tw; ++wt)
			{
				if(w[g] <= wt)
				{
					dp[i][wt] = max(max(dp[i - 1][wt], dp[i][wt]), dp[i - 1][wt - w[g]] + b[g]);
					// cout << i << " " << wt << " " << dp[i][wt] << "\n";
				}
				else
				{
					dp[i][wt] = max(dp[i - 1][wt], dp[i][wt]);
				}
			}
		}
	}
	for (int i = 1; i <= tw; ++i)
	{
		dp[c][i] = max(dp[c][i - 1], dp[c][i]);
	}
	cout << dp[c][tw];
}


#include <iostream>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <utility>
#include <string.h>
#include <map>
#include <stack>
#include <iomanip>
#include <chrono>
#include <random>
#include <math.h>
#include <time.h>
#include <assert.h>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) x.begin(),x.end()
#define PII pair <ll,ll>
#define N 300005
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int w[1005], b[1005], dp[1005][1005];
bool vis[1005];
int c = 0;
vector <int> v[1005], gr[1005];
void dfs(int s)
{
	vis[s] = true;
	gr[c].push_back(s);
	for (auto g : v[s])
	{
		if(!vis[g])
			dfs(g);
	}
}
int main()
{
	int n, m, tw, x, y, bt, we;
	cin >> n >> m >> tw;
	for (int i = 1; i <= n; ++i)
	{
		cin >> w[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
	}
	for (int i = 0; i < m; ++i)
	{
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= n; ++i)
	{
		if(!vis[i])
		{
			c++;
			dfs(i);
		}
	}
	memset(dp, 0, sizeof(dp));
	for (int i = 1; i <= c; ++i)
	{
		we = 0;
		bt = 0;
		for (auto g : gr[i])
		{
			we += w[g];
			bt += b[g];
		}
		for (int wt = 1; wt <= tw; ++wt)
		{
			if(we <= wt)
			{
				dp[i][wt] = max(dp[i - 1][wt], dp[i - 1][wt - we] + bt);
			}
			else
			{
				dp[i][wt] = dp[i - 1][wt];
			}
		}
	}
	for (int i = 1; i <= c; ++i)
	{
		for (auto g : gr[i])
		{
			for (int wt = 1; wt <= tw; ++wt)
			{
				if(w[g] <= wt)
				{
					dp[i][wt] = max(max(dp[i - 1][wt], dp[i][wt]), dp[i - 1][wt - w[g]] + b[g]);
					// cout << i << " " << wt << " " << dp[i][wt] << "\n";
				}
				else
				{
					dp[i][wt] = max(dp[i - 1][wt], dp[i][wt]);
				}
			}
		}
	}
	// for (int i = 1; i <= tw; ++i)
	// {
	// 	dp[c][i] = max(dp[c][i - 1], dp[c][i]);
	// }
	cout << dp[c][tw];
}


#include <iostream>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <utility>
#include <string.h>
#include <map>
#include <stack>
#include <iomanip>
#include <chrono>
#include <random>
#include <math.h>
#include <time.h>
#include <assert.h>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) x.begin(),x.end()
#define PII pair <ll,ll>
#define N 300005
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int w[1005], b[1005], dp[1005][1005];
bool vis[1005];
int c = 0, we = 0, bt = 0;
vector <int> v[1005];
vector <pair <int, int> > gr[1005];
void dfs(int s)
{
	we += w[s];
	bt += b[s];
	vis[s] = true;
	gr[c].push_back({w[s], b[s]});
	for (auto g : v[s])
	{
		if(!vis[g])
			dfs(g);
	}
}
int main()
{
	int n, m, tw, x, y;
	cin >> n >> m >> tw;
	for (int i = 1; i <= n; ++i)
	{
		cin >> w[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
	}
	for (int i = 0; i < m; ++i)
	{
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= n; ++i)
	{
		if(!vis[i])
		{
			we = 0;
			bt = 0;
			c++;
			dfs(i);
			gr[i].push_back({we, bt});
		}
	}
	memset(dp, 0, sizeof(dp));
	for (int i = 1; i <= c; ++i)
	{
		for (auto g : gr[i])
		{
			we = g.ff;
			bt = g.ss;
			for (int wt = 1; wt <= tw; ++wt)
			{
				if(we <= wt)
				{
					dp[i][wt] = max(dp[i - 1][wt], dp[i - 1][wt - we] + bt);
				}
				else
				{
					dp[i][wt] = dp[i - 1][wt];
				}
			}
		}
	}

	// for (int i = 1; i <= c; ++i)
	// {
	// 	we = 0;
	// 	bt = 0;
	// 	for (auto g : gr[i])
	// 	{
	// 		we += w[g];
	// 		bt += b[g];
	// 	}
	// 	for (int wt = 1; wt <= tw; ++wt)
	// 	{
	// 		if(we <= wt)
	// 		{
	// 			dp[i][wt] = max(dp[i - 1][wt], dp[i - 1][wt - we] + bt);
	// 		}
	// 		else
	// 		{
	// 			dp[i][wt] = dp[i - 1][wt];
	// 		}
	// 	}
	// }
	// for (int i = 1; i <= c; ++i)
	// {
	// 	for (auto g : gr[i])
	// 	{
	// 		for (int wt = 1; wt <= tw; ++wt)
	// 		{
	// 			if(w[g] <= wt)
	// 			{
	// 				dp[i][wt] = max(max(dp[i - 1][wt], dp[i][wt]), dp[i - 1][wt - w[g]] + b[g]);
	// 				// cout << i << " " << wt << " " << dp[i][wt] << "\n";
	// 			}
	// 			else
	// 			{
	// 				dp[i][wt] = max(dp[i - 1][wt], dp[i][wt]);
	// 			}
	// 		}
	// 	}
	// }
	cout << dp[c][tw];
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
stack<int> st;
vector<vector<int>> G;
int team[1002], w[1002], w_team[10002], b[1002], b_team[1002];
bool seen1[1002], seen2[1002];
pair<int, int> dfs1(int u, int id) {
    seen1[u] = true;
    team[u] = id;
    pair<int, int> ret = make_pair(w[u], b[u]);
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!seen1[v]) {
            pair<int, int> tmp = dfs1(v, id);
            ret.first += tmp.first;
            ret.second += tmp.second;
        }
    }
    return ret;
}

void dfs2(int u, int nw, int nb) {
    seen2[u] = true;
    w_team[u] = nw;
    b_team[u] = nb;
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!seen2[v]) {
            dfs2(v, nw, nb);
        }
    }
}

int dp[1002][1002];

int main() {
    ios_base::sync_with_stdio(0);
    //cin.tie(0);
    int n, m, W; cin >> n >> m >> W;

    for(int i = 0; i < n; i++) cin >> w[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    G = vector<vector<int>> (n+2);
    for(int i = 0; i < m; i++) {
        int a, b; cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    int id = 0;
    for(int i = 0; i < n; i++) {
        if(!seen1[i]) {
            pair<int, int> tmp = dfs1(i, id++);
            dfs2(i, tmp.first, tmp.second);
        }
    }

    int ans = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= W; j++) {
            if(j-w[i] >= 0) dp[i][j] = max(dp[i][j], b[i]);
            if(j-w_team[i] >= 0) dp[i][j] = max(dp[i][j], b_team[i]);
//            cout << b[i] << " " << b_team[i] << " " << dp[i][j] << endl;
            for(int k = 0; k < i; k++) {
                if(j - w[i] >= 0) {
                    if(team[k] == team[i])
                        dp[i][j] = max(dp[i][j], max(dp[k][j-w[i]], b[i]) );
                    else
                        dp[i][j] = max(dp[i][j], dp[k][j-w[i]]+b[i] );
                }
                if(j - w_team[i] >= 0){
                    if(team[k] == team[i])
                        dp[i][j] = max(dp[i][j], max(dp[k][j-w_team[i]], b_team[i]) );
                    else
                        dp[i][j] = max(dp[i][j], dp[k][j-w_team[i]]+b_team[i] );
                }
            }
            ans = max(ans, dp[i][j]);
//            cout << dp[i][j] << " ";
        }
//        cout << endl;
    }

    cout << ans;

}