import sys import os IS_LOCAL = os.environ.get("LOCAL") == "true" def debug(*args, sep=" ", end="\n", flush=False) -> None: if IS_LOCAL: print(*args, sep=sep, end=end, file=sys.stderr, flush=flush) def yn(flg: bool) -> bool: print('Yes' if flg else 'No') return flg def gcd(a, b): a = abs(a); b = abs(b) if a < b: a, b = b, a while b > 0: a, b = b, a % b return a def lcm(a, b): return a * b // gcd(a, b) def main(): readline = sys.stdin.readline N = int(readline()) dp, ndp = [0] * 2, [0] * 2 pre = list(map(int, readline().split()))[::-1] if pre[0] == pre[1]: dp[0] = dp[1] = pre[0] for _ in range(N - 1): tmp = list(map(int, readline().split())) for i in range(2): ndp[i] = max(dp[j] + tmp[i] * (tmp[i] == pre[j]) for j in range(2)) if tmp[0] == tmp[1]: ndp[i] += tmp[0] pre = tmp[::-1] dp = ndp[:] debug(dp) print(max(dp)) if __name__ == "__main__": main()