## https://yukicoder.me/problems/no/1160 def main(): N = int(input()) pa = [] for _ in range(N): p, a = map(int, input().split()) pa.append((p, a)) dp = [[-1, -1] for _ in range(N + 1)] dp[0] = [0, -1] for i in range(N): p, a = pa[i] # 前のターンでボーナスpin倒さない場合 if dp[i][0] != -1: if p != a: dp[i + 1][0] = max(dp[i + 1][0], dp[i][0] + p) dp[i + 1][1] = max(dp[i + 1][1], dp[i][0] + a) else: dp[i + 1][1] = max(dp[i + 1][1], dp[i][0] + p) if dp[i][1] != -1: if p != a: dp[i + 1][0] = max(dp[i + 1][0], dp[i][1] + 2 * p) dp[i + 1][1] = max(dp[i + 1][1], dp[i][1] + 2 * a) else: dp[i + 1][1] = max(dp[i + 1][1], dp[i][1] + 2 * p) answer = max(dp[-1]) print(answer) if __name__ == "__main__": main()