// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the maximum sum
// that can be obtained from two given
// based on the following conditions
static int MaximumSum(int []a, int []b, int n)
{
// Stores the maximum
// sum from 0 to i
int [,]dp = new int[n,2];
// Initialize the value of
// dp[0][0] and dp[0][1]
dp[0,0] = a[0];
dp[0,1] = b[0];
// Traverse the array A[] and B[]
for (int i = 1; i < n; i++)
{
// If A[i] is considered
dp[i,0] = Math.Max(dp[i - 1,0], dp[i - 1,1]) + a[i];
// If B[i] is not considered
dp[i,1] = Math.Max(dp[i - 1,0], dp[i - 1,1]);
// If B[i] is considered
if (i - 2 >= 0) {
dp[i,1] = Math.Max(dp[i,1],
Math.Max(dp[i - 2,0],
dp[i - 2,1])
+ b[i]);
}
else
{
// If i = 1, then consider the
// value of dp[i][1] as b[i]
dp[i,1] = Math.Max(dp[i,1], b[i]);
}
}
// Return maximum Sum
return Math.Max(dp[n - 1,0], dp[n - 1,1]);
}
// Driver Code
public static void Main()
{
// Given Input
int []A = { 10, 1, 10, 10 };
int []B = { 5, 50, 1, 5 };
int N = A.Length;
// Function Call
Console.Write(MaximumSum(A, B, N));
}
}
// This code is contributed by ipg2016107.