Segmentation fault

i am not able to find out from where error is coming
#include<bits/stdc++.h>
using namespace std;
void solve(vector v,int &res,int c)
{
cout<<v.size()<<" ";

if(v.size()==0)
{
	return;
}
int val=max(*v.begin(),*v.end());
if(c%2!=0)
{
	res=res+val;
}
if(val==*v.begin())
{
	v.erase(v.begin()+0);
}
else
{
	v.erase(v.end()+0);
}
solve(v,res,c++);

}
int main() {
vector v;
int n,k;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>k;
v.push_back(k);
}
int res=0;
cout<<v.size();
solve(v,res,1);
cout<<res;
}

Hey @priyansh781
According to me u are always taking maximum

picking the greatest always won’t help ,say
4
100 1000 10 90

Here I will pick 90 first so that opponent don’t get access to 1000 and he will pick 100 then I pick 1000
and he picks 10 so answer is 1090
So this means its not always the greatest we pick .’

logic:
Remember both A and B are equally smart and we want A to get maximum.
if coins are indexed from i to j and both have to choose one out of two ends.

case 1: if A chooses a[i], now opponent can choose either a[i+1] or a[j] whatever is maximum out of two. which means A will get Minimum in his next chance based on opponent choice.
case 1.a: if opponent choose a[i+1], player will chose either a[i+2] or or a[j]
case 1.b: if opponent choose a[j], player will chose either a[i+1] or or a[j-1]
So A will get minimum out of case 1.a and case 1.b…as opponent is equally smarter and choose such a coin which give him maximum but minimum to player A.

case 2: if A chooses a[j], now opponent can choose either a[i] or a[j-1] whatever is maximum out of two. which means A will get Minimum in his next chance based on opponent choice.
case 2.a: if opponent choose a[i], player will chose either a[i+1] or or a[j-1]
case 2.b: if opponent choose a[j-1], player will chose either a[i] or or a[j-2]
So A will get minimum out of case 2.a and case 2.a…as opponent is equally smarter and choose such a coin which give him maximum but minimum to player A.

This will give you a recursive case in which final answer is max(case1,case2)