**Bricks Game**

### Problem Statement :

You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered arr = [1,2,3,4,5]. You can remove either [1]=1, [1,2]=3 or [1,2,3] = 6. For your friend, your moves would leave the options of 1 to 3 elements from [2,3,4] = 9 leaving for you (total score = 6), [3,4,5] = 12 or [4,5] = 9. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is 6, achievable two ways: 1 first move and 5 the second, or [1,2,3] in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer t, the number of test cases. Each of the next t pairs of lines are in the following format: The first line contains an integer , the number of bricks in arr. The next line contains n space-separated integers $arr[i]. Constraints 1 <= t <= 5 1 <= n <= 10^5 0 <= arr[i] <= 10^9 Output Format For each test case, print a single line containing your maximum score.

### Solution :

` ````
Solution in C :
In C++ :
#include<math.h>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<map>
#include<ext/hash_map>
#include<ext/hash_set>
#include<set>
#include<string>
#include<assert.h>
#include<vector>
#include<time.h>
#include<queue>
#include<deque>
#include<sstream>
#include<stack>
#include<sstream>
#define MA(a,b) ((a)>(b)?(a):(b))
#define MI(a,b) ((a)<(b)?(a):(b))
#define AB(a) (-(a)<(a)?(a):-(a))
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define pob pop_back
#define ep 0.0000000001
#define Pi 3.1415926535897932384626433832795
using namespace std;
using namespace __gnu_cxx;
const long long MO=1000000000+7;
int i,j,n,m,k;
int a[1000000];
long long s[1000000],su[1000000];
bool f[1000000];
long long sol(int x)
{
if (x==n+1) return 0;
if (f[x]) return s[x];
if (n-x<=2) return s[x]=su[x];
f[x]=1;
s[x]=0;
if (x+1<=n+1)s[x]=max(s[x],su[x]-sol(x+1));
if (x+2<=n+1)s[x]=max(s[x],su[x]-sol(x+2));
if (x+3<=n+1)s[x]=max(s[x],su[x]-sol(x+3));
}
int main()
{
int t;
cin>>t;
while (t--)
{
cin>>n;
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
for (i=n;i>=1;i--)
su[i]=su[i+1]+a[i];
for (i=1;i<=n;i++)
f[i]=0;
cout<<sol(1)<<endl;
}
return 0;
}
In Java :
import java.util.Scanner;
public class Solution {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-- > 0) {
int n = scanner.nextInt();
int a[] = new int[n];
long[] b = new long[n];
long c[] = new long[n + 1];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
b[i] = -1;
}
c[n - 1] = a[n - 1];
c[n] = 0;
for (int i = n - 2; i >= 0; i--)
c[i] = c[i + 1] + a[i];
System.out.println(play(a, b, c, 0));
}
}
private static long play(int[] a, long[] b, long[] c, int i) {
int n = a.length;
if (i == n)
return 0;
if (b[i] != -1)
;
else if (i >= n - 3) {
b[i] = 0;
for (int j = i; j < n; j++)
b[i] += a[j];
} else {
b[i] = a[i] + a[i + 1] + a[i + 2] + c[i + 3] - play(a, b, c, i + 3);
b[i] = Math.max(a[i] + c[i + 1] - play(a, b, c, i + 1), b[i]);
b[i] = Math.max(b[i], a[i] + a[i + 1] + c[i + 2] - play(a, b, c, i + 2));
}
// System.out.println("returning " + i + " : " + b[i]);
return b[i];
}
}
In C :
#include <stdio.h>
long long min(long long a, long long b) { return (a<b?a:b); }
int main() {
int T,n;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
int t[n];
long long best[n+3], suff[n+3];
for (int i=n; i<n+3; i++) best[i] = suff[i] = 0;
for (int i=0; i<n; i++) scanf("%d", &t[i]);
for (int i=n-1; i>=0; i--) suff[i] = suff[i+1] + t[i];
for (int i=n-1; i>=0; i--)
best[i] = suff[i] - min(best[i+1], min(best[i+2], best[i+3]));
printf("%lld\n", best[0]);
}
return 0;
}
In Python3 :
INF = 10**18
def one_test():
n = int(input())
a = list(reversed(list(map(int, input().split()))))
dp = [0] * (n + 1)
for i in range(n):
take = 0
dp[i + 1] = -INF
for j in range(i, max(i - 3, -1), -1):
take += a[j]
dp[i + 1] = max(dp[i + 1], take - dp[j])
# print("!!!", dp)
return (sum(a) + dp[n]) // 2
t = int(input())
for i in range(t):
print(one_test())
```

## View More Similar Problems

## Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →## Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →## Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

View Solution →## Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

View Solution →## Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →## Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

View Solution →