`{"id":"1ef39974-8f94-4ed1-9817-1b035d2aa1ce","name":"Optimal Strategy For A Game","description":"1. You are given N coins as an integer array, size of array is even\r\n 2. You are playing against an opponent where both of you get turns alternatingly\r\n 3. In each turn a player removes either last of the first element from the array permanently and adds that coin value to his pile\r\n 4. You need to find the maximum amount of money you can win if you go first\r\n 5. Input and output is handled for you\r\n 6. It is a functional problem ,please do not modify main()","inputFormat":"Input is handled for you","outputFormat":"Output is handled for you","constraints":"2 &lt;= N &lt;= 100\r\n 1&lt;= A[i]&lt;=10^6","sampleCode":{"cpp":{"code":"#include<iostream>\r\n #include<vector>\r\n using namespace std;\r\n \r\n \r\n // -----------------------------------------------------\r\n // This is a functional problem. Only this function has to be written.\r\n // This function takes as input array of integers\r\n // It should print the required output\r\n \r\n void optimalStrategy(vector<int>& a){\r\n //Write your code here\r\n }\r\n \r\n int main(int argc,char** argv) {\r\n int n ;\r\n cin>>n;\r\n vector<int> a;\r\n for (int i = 0; i <n; i++) {\r\n int x;\r\n cin>>x;\r\n a.push_back(x);\r\n }\r\n optimalStrategy(a);\r\n }"},"java":{"code":"import java.util.*;\r\n \r\n public class Main {\r\n \r\n \t public static void main(String[] args) {\r\n \t \t Scanner scn = new Scanner(System.in);\r\n \t \t \t int n = scn.nextInt();\r\n \t \t \t int a[] = new int[n];\r\n \t \t \t for (int i = 0; i < a.length; i++) {\r\n a[i] = scn.nextInt();\r\n }\r\n optimalStrategy(a);\r\n }\r\n \t // -----------------------------------------------------\r\n \t // This is a functional problem. Only this function has to be written.\r\n \t // This function takes as input array of integers\r\n \t // It should print the required output\r\n \r\n \t public static void optimalStrategy(int[] a){\r\n \t \t //Write your code here\r\n \t }\r\n }"}},"points":10,"difficulty":"easy","sampleInput":"4\n8 15 3 7","sampleOutput":"22","questionVideo":null,"hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"5539a6e8-c8bf-4f04-805c-e43e9d20e72a","name":"Dynamic Programming For Intermediate","slug":"dynamic-programming-for-intermediate-408","type":0},{"id":"aa92b2ff-44f4-4766-a104-010f67bd2b99","name":"Optimal Strategy For A Game","slug":"optimal-strategy-for-a-game","type":1}],"next":{"id":"e9449e05-b347-4640-a254-3114866c6271","name":"OPTIMAL STRATEGY FOR A GAME MCQ","type":0,"slug":"optimal-strategy-for-a-game-mcq"},"prev":{"id":"7dd3356c-d221-4b43-bff1-c9684969d94b","name":"Probability Of Knight In The Chessboard","type":3,"slug":"probability-of-knight-in-the-chessboard"}}}`

# Optimal Strategy For A Game

1. You are given N coins as an integer array, size of array is even 2. You are playing against an opponent where both of you get turns alternatingly 3. In each turn a player removes either last of the first element from the array permanently and adds that coin value to his pile 4. You need to find the maximum amount of money you can win if you go first 5. Input and output is handled for you 6. It is a functional problem ,please do not modify main()

`{"id":"1ef39974-8f94-4ed1-9817-1b035d2aa1ce","name":"Optimal Strategy For A Game","description":"1. You are given N coins as an integer array, size of array is even\r\n 2. You are playing against an opponent where both of you get turns alternatingly\r\n 3. In each turn a player removes either last of the first element from the array permanently and adds that coin value to his pile\r\n 4. You need to find the maximum amount of money you can win if you go first\r\n 5. Input and output is handled for you\r\n 6. It is a functional problem ,please do not modify main()","inputFormat":"Input is handled for you","outputFormat":"Output is handled for you","constraints":"2 &lt;= N &lt;= 100\r\n 1&lt;= A[i]&lt;=10^6","sampleCode":{"cpp":{"code":"#include<iostream>\r\n #include<vector>\r\n using namespace std;\r\n \r\n \r\n // -----------------------------------------------------\r\n // This is a functional problem. Only this function has to be written.\r\n // This function takes as input array of integers\r\n // It should print the required output\r\n \r\n void optimalStrategy(vector<int>& a){\r\n //Write your code here\r\n }\r\n \r\n int main(int argc,char** argv) {\r\n int n ;\r\n cin>>n;\r\n vector<int> a;\r\n for (int i = 0; i <n; i++) {\r\n int x;\r\n cin>>x;\r\n a.push_back(x);\r\n }\r\n optimalStrategy(a);\r\n }"},"java":{"code":"import java.util.*;\r\n \r\n public class Main {\r\n \r\n \t public static void main(String[] args) {\r\n \t \t Scanner scn = new Scanner(System.in);\r\n \t \t \t int n = scn.nextInt();\r\n \t \t \t int a[] = new int[n];\r\n \t \t \t for (int i = 0; i < a.length; i++) {\r\n a[i] = scn.nextInt();\r\n }\r\n optimalStrategy(a);\r\n }\r\n \t // -----------------------------------------------------\r\n \t // This is a functional problem. Only this function has to be written.\r\n \t // This function takes as input array of integers\r\n \t // It should print the required output\r\n \r\n \t public static void optimalStrategy(int[] a){\r\n \t \t //Write your code here\r\n \t }\r\n }"}},"points":10,"difficulty":"easy","sampleInput":"4\n8 15 3 7","sampleOutput":"22","questionVideo":null,"hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"5539a6e8-c8bf-4f04-805c-e43e9d20e72a","name":"Dynamic Programming For Intermediate","slug":"dynamic-programming-for-intermediate-408","type":0},{"id":"aa92b2ff-44f4-4766-a104-010f67bd2b99","name":"Optimal Strategy For A Game","slug":"optimal-strategy-for-a-game","type":1}],"next":{"id":"e9449e05-b347-4640-a254-3114866c6271","name":"OPTIMAL STRATEGY FOR A GAME MCQ","type":0,"slug":"optimal-strategy-for-a-game-mcq"},"prev":{"id":"7dd3356c-d221-4b43-bff1-c9684969d94b","name":"Probability Of Knight In The Chessboard","type":3,"slug":"probability-of-knight-in-the-chessboard"}}}` Editor

# Optimal Strategy For A Game

easy

1. You are given N coins as an integer array, size of array is even 2. You are playing against an opponent where both of you get turns alternatingly 3. In each turn a player removes either last of the first element from the array permanently and adds that coin value to his pile 4. You need to find the maximum amount of money you can win if you go first 5. Input and output is handled for you 6. It is a functional problem ,please do not modify main()

## Constraints

2 <= N <= 100 1<= A[i]<=10^6

## Format

### Input

Input is handled for you

### Output

Output is handled for you

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 8 15 3 7```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}22`

Discussions

Show Discussion

Related Resources 