Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; import java.io.PrintWriter; public class Main{ static long[] a; static long[][] dp; static boolean[][] visited; public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); a=new long[n]; dp=new long[n][n]; visited=new boolean[n][n]; for(int i=0; i<n...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual soluti...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) a = tuple(map(int, input().split())) memo = tuple([None] * n for _ in range(n)) for i, aa in enumerate(a): memo[i][i] = aa for l in range(n - 1, -1, -1): for r in range(l, n): if memo[l][r] is None: memo[l][r] = max(a[r] - memo[l][r - 1], a[l] - memo[l + 1][r]) print(memo...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll bd[3001][3001], n, a; int main() { cin >> n; for (int i=0; i<n; i++) { cin >> a; bd[i][i] = a; } for (int i=1; i<=n-1; i++) { for (int j=0; j+i<n; j++) { bd[j][j+i] = max(bd[j][j]-bd[j+1][j+i], bd[j+i][j+i]-bd[j][j+i-1]); } } c...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std; int n; long long dp[3100][3100]; long long a[3100]; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; } for(int r=1;r<=n;r++) { for(int l=r;l>=1;l--){ dp[r][l]=max(a[l]-dp[r][l+1],a...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
/* Arnab Chanda */ // All imports here import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.InputStream; import java.util.*; import java.util.stream.Collectors; // Template code starts here // //////...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll arr[3005]; ll dp[3005][3005]; int main(){ int n; cin >> n; for(int i = 0; i < n; i++){ cin >> arr[i]; } for(int i = n-1; i >= 0; i--){ for(int j = i; j < n; j++){ if(i == j){ dp[i][j] = arr[i]; }else{ dp[i][j] = max(arr[i...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
/* https://atcoder.jp/contests/dp/tasks/dp_l */ import java.io.*; import java.util.*; public class Main { private static long solve(int n, long[] arr) { long[][] dp = new long[n][n]; for (int i = n - 1; i >= 0; i--) { for (int j = i; j < n; j++) { if (i == j) { ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) nums = list(map(int, input().split())) dp = [[0 for _ in range(n)] for _ in range(n)] for i in range(n): dp[i][i] = nums[i] for k in range(2, n+1): for l in range(n-k+1): r = l+k-1 dp[l][r] = max(nums[l]-dp[l+1][r], nums[r] - dp[l][r-1]) print(dp[0][-1])
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; for (int i=0; i<n; i++) { a[i] = sc.nextInt(); } long[] dp = new long[n * n]; for ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) a = list(map(int, input().split())) dp = [[0 for _ in range(n+1)] for _ in range(n+1)] for i in range(n): dp[i][i]=0 for l in range(1,n+1): for i in range(n+1): j=i+l if j>n: continue if (n-l)%2==0: # sente dp[i][j]= max(a[j-1]+dp[i][j-1],a[i]...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; public class Main { static int n; static long[] a=new long[3005]; static boolean[][] vis=new boolean[3005][3005]; static long[][] dp=new long[3005][3005]; static long DP(int x,int y) { if (x+y>=n) return 0; if (vis[x][y]) return dp[x][y]; vis[x][y]=true; if ((x+y)%2==0) dp[x][y]=Math.ma...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import sys from collections import deque def input(): return sys.stdin.readline().rstrip() def main(): N = int(input()) A = list(map(int, input().split())) dp = [[0] * (N + 1) for _ in range(N + 1)] for i in range(N): dp[i][i] = A[i] for k in range(1, N): # distance for i...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.*; import java.util.*; class Main { public static void main(String[] args) throws Exception { final Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } long[][] dp = new long[n + 1][n + 1]; for (int d ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; import java.io.*; class Main { public static void main(String[] args) { Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int N = in.nextInt(); long[] a = new long[N]; for (int i = 0; i < N; i++) { a[i] = in.nextInt(); } ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<iostream> using namespace std; int a[3001]; long long dp[3001][3001]; int main() { int n;cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int len=1;len<=n;len++) for(int i=1;i<=n;i++) { int j=i+len-1; if(j>n)continue; if(len==1)dp[i][j]=a[i]; else dp[i][j]=max(a[i]-dp[i+1][j],a[j]-dp[i][j-1]);...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; long a[3333], d[3333][3333]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n + 1; i++) for (int j = 0; j < n - i + 1; j++) d[j][i] = max(a[j] - d[j + 1][i - 1], a[j + i - 1] - d[j][i - 1]); cout << d[0][n] << endl; ...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import sys def input(): return sys.stdin.readline()[:-1] n = int(input()) a = [int(x) for x in input().split()] dp=[] for i in range(n): dp.append([0]*n) for i in range(n): dp[i][i] = a[i] for i in range(n - 2, -1, -1): for j in range(i + 1, n): dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; import java.io.*; import java.lang.*; public class Main{ public static long help(int[] arr,long[][] dp,int i, int j){ if(i>j||j<=0||i>arr.length-1){return 0;} if(dp[i][j]!=0){return dp[i][j];} long x=arr[i]+Math.min(help(arr,dp,i+2,j),help(arr,dp,i+1,j-1)); long y=arr[j]+Math.min(help(arr,dp,i+1...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std; #define ll long long ll dp[3005][3005];//dp[i][j] -> we get j heads from first i coins int main(){ int n; cin>>n; ll a[n]; for(int i=0;i<n;i++){ cin>>a[i]; dp[i][i]=a[i]; } for(int i=n-1;i>=0;--i){ for(int j=i+1;j<n;++j){ dp[i][j] = max(a[i]-dp[...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j; cin >> n; long long dp[n], a[n]; for(i = 0; i < n; ++i) cin >> a[i], dp[i] = a[i]; for(i = 1; i < n; ++i) for(j = 0; j < n-i; ++j) dp[j] = max(a[i+j] - dp[j], a[j] - dp[j+1]); cout << dp[0]; return 0;...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <iostream> using namespace std; long long n,i,k,a[3005], dp[3005][3005]; int main() { cin>>n; for(i=1;i<=n;i++) cin>>a[i]; for(k=0;k<n;k++) { for(i=1;i+k<=n;i++) { if(k==0) dp[i][i]=a[i]; else dp[i][i+k]=max(a[i]...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; public class Main{ public static void main(String args[]) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int arr[]=new int[n]; long sum=0; for(int i=0;i<n;i++) { arr[i]=s.nextInt(); sum+=arr[i]; } long dp[][]=new long[n+1][n+1]; for(int i=0;i<=n;i++) { for(int j=...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.*; class Main{ public static void main(String[] args) { FastReader f = new FastReader(); int n = ii(f); long[] arr = new long[n+1]; for(int i=1; i<=n; ++i) arr[i] =...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
def main(): N = int(input()) A = list(map(int, input().split())) dp = [[0] * N for _ in range(N+1)] dp[1] = A for length in range(2, N+1): for idx in range(N - length + 1): dp[length][idx] = max(A[idx] - dp[length-1][idx+1], A[idx+length-1] - dp[...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
N = int(input()) A = list(map(int, input().split())) #import random #N = 3000 #A = [random.randint(1, 10**9) for _ in range(N)] dp = [[0] * (N+1) for _ in range(N+1)] for g in range(N): for l in range(N-g): r = l + g n = r - l s = -1 if (N-n) % 2 == 0 else 1 lv = dp[l+1][r] + s*A[l] rv = dp[l][r...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main { public static class Pair{ long x; long y ; public Pair(long x,long y) { this.x=x; this.y=y; } public long diff() { ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) a = tuple(int(x) for x in input().split()) memo = tuple([None] * n for _ in range(n)) for i, aa in enumerate(a): memo[i][i] = aa for l in range(n - 1, -1, -1): for r in range(l, n): if memo[l][r] is None: memo[l][r] = max(a[r] - memo[l][r - 1], a[l] - memo[l + 1][r]) prin...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] a = new int[N]; for (int i = 0; i < N; i++) a[i] = sc.nextInt(); sc.close(); long[][] dp = new long[N][N]; for (int ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import ja...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std; #define int long long int dp[3005][3005]; int a[3005]; int n; signed main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%lld", &a[i]); for(int i=1;i<=n;i++)dp[i][i]=a[i]; for(int i=2;i<=n;i++) for(int j=1;i+j-1<=n;j++) dp[j][i+j-1]=max(a[j]-dp[j+1][i+j-1],-dp[j][i+j...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
def solve(): import sys input = sys.stdin.readline n=int(input()) a=list(map(int,input().split())) dp=a[:] for i in range(1,n): for j in range(i-1,-1,-1): dp[j]=max(a[i]-dp[j],a[j]-dp[j+1]) print(dp[0]) solve()
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 3002; ll dp[MAXN][MAXN]; bool vis[MAXN][MAXN]; int A[MAXN],N; ll solve(int i,int j) { if(vis[i][j]) return dp[i][j]; vis[i][j] = 1; if(i == j) return dp[i][j] = A[i]; return dp[i][j] = max(A[i] - solve(i+1,j), A[j] - solve(i,j-...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std; int NAX=3001; #define ll long long int main(){ ll n; scanf("%lld",&n); vector<ll> arr(n); for(int i=0;i<n;i++)scanf("%lld",&arr[i]); vector<vector<ll> > dp(NAX,vector<ll>(NAX)); for(int i=n-1;i>=0;i--){ for(int j=i;j<n;j++){ if(i==j) dp[i][j]=arr[i]; el...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; int const MAXN=3010; #define ll long long ll n, v[MAXN]; ll memo[MAXN][MAXN]; bool vis[MAXN][MAXN]; ll dp(int i, int j){ if(i==j) return v[i]; if(i>j) return 0; if(vis[i][j]) return memo[i][j]; vis[i][j]=true; ll l=v[i]-dp(i+1,j), r=v[j]-dp...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Main { static long solve(long arr[],int i,int j,long dp[][]){ if(i>j){ return 0; } ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <iostream> #include <algorithm> #include <vector> using namespace std; typedef long long int ll; int n; int a[3010]; ll dp[3010][3010]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = n; i >= 1; i--) for (int j = i; j <= n; j++) dp[i][j] = max(a[i] - d...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
//package atcoder.dpContest; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { // try { // FastScanner in = new FastScanner(new FileInputStream("src/input.in")); // PrintWriter out = new PrintWriter(new FileOutputStream("src/output.out"...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) a = list(map(int, input().split())) dp = [[0 for _ in range(n)] for _ in range(n)] for i in range(n): dp[i][i] = a[i] for w in range(1, n): for l in range(n - w): dp[l][l+w] = max(a[l] - dp[l+1][l+w], a[l+w] - dp[l][l+w-1]) print(dp[0][n-1])
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; for (int i=0; i<n; i++) { a[i] = sc.nextInt(); } long[] dp = new long[n*n]; for (in...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <iostream> using namespace std; const int maxN=3001; int n; int v[maxN]; long long dp[maxN][maxN]; int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>v[i]; } for(int i=1;i<=n;i++){ dp[i][i]=v[i]; } for(int i=n;i>=1;i--){ for(int j=i+1;j<=n;j++){ dp...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <iostream> using namespace std; long long a[100005]; long long dp[3005][3005]; int main(int argc, char** argv) { int n; cin >> n; for(int i=1;i<=n;i++) cin >> a[i]; for(int i=1;i<=n;i++) { for(int j=1;j<=n-i+1;j++) dp[j][j+i-1]=max(a[j]-dp[j+1][j+i-1],a[j+i-1]-dp[j][j+i-2]); } cout << dp[1][n]; ...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> #define re register #define SIZE 3005 #define LL long long using namespace std; int n; LL a[SIZE],dp[SIZE][SIZE]; int main() { cin>>n; for(re int i=1;i<=n;++i) cin>>a[i],dp[i][i]=a[i]; for(re int len=1;len<=n;++len) for(re int l=1;l<n;++l) { int r=l+len-1; if(r>n) break; d...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#EDPC L メモ化再帰じゃTLEで通らないのでやり直し N=int(input()) a=list(map(int,input().split())) #dp[l][r]:=区間[l,r]が残っている時の(直後の人の最終的な得点-そうじゃない方の最終的な得点) dp=[[-1]*N for _ in range(N)] for k in range(N): dp[k][k]=a[k] for r in range(1,N): for l in reversed(range(r)): dp[l][r]=max(a[l]-dp[l+1][r],a[r]-dp[l][r-1]) print(...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n=int(input()) a=[int(j) for j in input().split()] dp=[[0]*(n+1) for i in range(n+1)] for i in range(n)[::-1]: for j in range(i,n): if (n+i-j-1)%2==0: dp[i][j]=max(dp[i+1][j]+a[i],dp[i][j-1]+a[j]) else: dp[i][j]=min(dp[i+1][j]-a[i],dp[i][j-1]-a[j]) print(dp[0][n-1])
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> #define ll long long using namespace std; int n; int arr[3001]; ll dp[3001][3001]; int main() { cin >> n; for (int i=0;i<n;i++) {cin >> arr[i];} for (int L=n-1;L>=0;L--) { for (int R=L;R<=n-1;R++) { if (L==R) {dp[L][R]=arr[L];} else {dp[L][R]=max(ar...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int MAXN=4096; LL dp[MAXN][MAXN],a[MAXN]; int n; int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; dp[i][i]=a[i]; } for(int len=2;len<=n;len++){ for(int l=1;l<n;l++){ int r=l+len-1; if(r>n) break; dp[l][r]=max(a[l]-dp[l+1...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; import java.io.*; // Template for atcoder public class Main { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; final long MOD = 1000L * 1000L * 1000L + 7; private static final int[] dx = {0, -1, 0, 1}; private static final int[] dy = {1, 0, -1, 0}; ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; int main() { long long i,j,k,n; cin>>n; long long arr[n+1]; for(i=1;i<=n;i++){ cin>>arr[i]; } long long dp[n+1][n+1]; for(i=1;i<=n;i++){ dp[i][i]=arr[i]; } for(i=n;i>=1;i--){ for(j=i;j<=n;j++){ if(i!=j){ dp[i][j]=max(arr[i]-dp[i...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) a = list(map(int, input().split())) dp = [[0] * (n+1) for _ in range(n+1)] for l in range(1, n+1): for i in range(n - l + 1): j = i + l if (n - l) % 2 == 0: dp[i][j] = max(dp[i+1][j] + a[i], dp[i][j-1] + a[j-1]) else: dp[i][j] = min(dp[i+1][j] - a[i...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; int a[3010]; long long dp[3010][3010]; int n; int main(){ cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; for(int i = n - 1; i >= 0; i--){ for(int j = i + 1; j <= n; j++){ dp[i][j] = max(a[i] - dp[i + 1][j], a[j - 1] - dp[i][j - 1]); ...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) a = list(map(int, input().split())) dp = [[0] * n for _ in range(n)] for i in range(n): dp[i][i] = a[i] for i in range(n - 2, -1, -1): for j in range(i + 1, n): dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]) print(dp[0][n - 1])
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std; long long dp[3003][3003],n,a[3002]; int main(){ cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } for(int l=n-1;l>=0;l--){ for(int r=l;r<n;r++){ if(r==l) dp[r][l]=a[l]; else{ dp[l][r]=max(a[l]-dp[l+1][r],a[r]-d...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; public class Main { static long[][] dp; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] a = new int[N]; for(int i = 0; i < N; i++) { a[i] = sc.nextInt(); } //dp on the answe...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { private static final long MOD = 1000000007; InputStream is; PrintWriter out; String INPUT = ""; long[][] d...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; public class Main { private final static InputReader inputReader = new InputReader(System.in); private final s...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
N = int(input()) A = list(map(int, input().split())) def solve(N,A): s,e = 0, N-1 dp = [[0]*N for i in range(N)] for k in range(N): for i in range(N): j = i+k if j>=N: continue sign = (-1)**(N-1-k) if k==0: dp[i][j] = ...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; public class Main { public static long dp[][] = new long[3001][3001]; public static long solve(int i,int j,int arr[],int turn){ if(i > j) return 0; if(dp[i][j] != -1){ return dp[i][j]; } if(turn == 1) dp[i][j] = Math.max( arr[i]+solve(i+1,j,arr...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.LinkedList; import java.util.Map; import java.util.StringTokenizer; public class Main { private static Long[][][] dp; private static long calculate(int player, int left, int r...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long dpF[][] = new long[n + 1][n + 1]; long dpS[][] = new long[n + 1][n + 1]; for ( int i = 1; i <= n; i++ ) { dpF[i][i] = in.nextInt(); dpS[i][i] = 0; } ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; long long dp[3005][3005]; long long a[3005]; int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); int n; cin >> n; for (int i=0;i<n;i++) cin >> a[i]; for (int i=n;i>=0;i...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n,*a=map(int,open(0).read().split()) dp=[[0]*(n+1) for _ in range(n+1)] for l in range(n)[::-1]: for r in range(n)[::-1]: if l+r<=n-1: dp[l][r]=max(a[l]-dp[l+1][r],a[n-1-r]-dp[l][r+1]) print(dp[0][0])
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Main ans = new Main(); System.out.println(ans.solve()); } private long solve() { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long [] array = new long[N+1]; long sum=0; for(int i=1;i<=N;i...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std; int main() { int a[3000], n; cin >> n; vector<vector<long>> dp (n+1, vector<long> (n+1)); for (int i = 0; i < n; i++) cin >> a[i]; for (int L = n-1; L >= 0; L--) { for (int R = L; R < n; R++) { dp[L][R] = max(a[L] - dp[L+1][R]...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; public class Main { static long dp[][][]=new long[3002][3002][2]; public static void main(String args[]){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); long arr[]=new long[n]; long sum=0; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); sum+=arr[i]; } long ans=helper(0,n-1,...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; import java.io.*; class Main { static long[][] dp1; static long recurse1(long[] a, int i, int j) { if(i>j) return 0; if(i==j) return a[i]; if(dp1[i][j]!=-1) return dp1[i][j]; long max = Math.max(a[i]-recurse1(a, i+1, j), a[j...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n,a=int(input()),list(map(int,input().split()));dp=[[None]*n for _ in range(n)] for i in range(n):dp[i][i]=a[i] for j in range(1,n): for i in range(n-j):dp[i][j+i]=max(a[i]-dp[i+1][j+i],a[j+i]-dp[i][j+i-1]) print(dp[0][-1])
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std; long long n, a[3005], b[3005]; int main(){ scanf("%lld", &n); for(int i = 0; i < n; i++){ scanf("%lld", a+i); b[i] = (n%2*2-1)*a[i]; for(int j = i-1; j >= 0; j--){ if((n-i+j)&1) b[j] = max(b[j+1] + a[j], b[j] + a[i]); ...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
N = int(input()) A = list(map(int, input().split())) dp = [[0]*(N+1) for _ in range(N+1)] for n in range(1, N+1): for l in range(0, N-n+1): r = l + n if (N-n)%2==0: dp[l][r] = max(dp[l+1][r] + A[l], dp[l][r-1] + A[r-1]) else: dp[l][r] = min(dp[l+1][r] - A[l], dp[l][...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> #define int long long #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; const int N = 3005; int dp[N][N]; signed main(){ fastio int n; cin >> n; int a[n]; for(auto &x : a) cin >> x; for(int i = n-1;i >= 0;i--){ for(int j = i;j < n;j++){ if...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
N = int(input()) A = list(map(int, input().split())) def solve(N,A): s,e = 0, N-1 dp = [[0]*N for i in range(N)] for k in range(N): for i in range(N-k): j = i+k sign = (-1)**(N-1-k) if k==0: dp[i][j] = sign*A[i] continue ...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import sys input = sys.stdin.readline ''' allinputs = iter(input().splitlines()) input = lambda : next(allinputs) #''' N = int(input()) a = list(map(int, input().split())) dp = [[0] * N for _ in range(N)] #dp[n][i]...n+1:残った要素の個数、i:左端の要素が初期状態のi番目の要素、dp:X-Y for n in range(N): for i in range(N - n): if n == 0: dp[...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Main { static long dp[][][]; public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine(...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static int lenbuf = 0...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> #define ll long long #include <iostream> using namespace std; int main() { ll int n; cin>>n; vector<ll int> a(n); for(ll int i=0;i<n;i++){ cin>>a[i]; } ll int dp[n+1][n+1]; memset(dp,0,sizeof(dp)); for(ll int i=1;i<=n;i++){ dp[i][i]=a[i-1]; } for(ll int i=n-1;i>=0;i--){ for(ll in...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
def kukan(n, a): dp = [[0] * n for _ in range(n)] for i in range(n): dp[i][i] = a[i] for i in range(n - 2, -1, -1): for j in range(i + 1, n): L = a[i] - dp[i + 1][j] R = a[j] - dp[i][j - 1] dp[i][j] = L if L > R else R return dp[0][n - 1] n = int(inp...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<iostream> using namespace::std; long long int t,ar[3005],dp[3005][3005]; int main(){ int n; cin>>n; for(int i=0;i<n;i++) cin>>ar[i]; for(int i=0;i<n;i++) dp[i][0]=ar[i]; for(int j=1;j<n;j++) for(int i=0;i<n-j;i++) dp[i][j]=max(ar[i+j]-dp[i][j-1],ar[i]-...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { static int MAXN= 3005; static long dp[][]; static long as []; static long min(long a, long b) { return Math.min(a, b); } static long max(long a, long b) { return Math.max(a, b); } static long calc(int inicio, int fin, i...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
def fun(i,j): # print(i,j) if i>j or i>=n or j<0: return 0 elif i==j: dp[i][j]=arr[i] return dp[i][j] else: if dp[i][j]==0: dp[i][j]=max(arr[i]+min(fun(i+1,j-1),fun(i+2,j)),arr[j]+min(fun(i+1,j-1),fun(i,j-2))) return dp[i][j] n=int(input()) dp=[0]*n for i in range(n): dp[i]=[0]*n arr=list(map(int,inpu...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; public class Main{ static FastScanner sc = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); //static String S; //static String T; static int n; static long dp [][]...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { init(); int n = nextInt(); int[] values = new int[n]; for (int i = 0; i < values.length; i++) values[i] = nextInt(); ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.*; import java.io.IOException; import java.lang.Thread.State; import java.util.*; //import javafx.util.Pair; //import java.util.concurrent.LinkedBlockingDeque; public class Main { public static long mod = (long)Math.pow(10, 9)+7 ; public static double epsilon=0.000000...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; for (int i=0; i<n; i++) { a[i] = sc.nextInt(); } long[] dp = new long[n * n]; for ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
N = int(input()) A = list(map(int,input().split())) A = A dp = [[0] * (N+1) for _ in range(N+1)] for i in range(N+1): dp[i][i] = 0 for k in range(1,N+1):#区間の残り数 for i in range(N):#左端 x = i+k#右端 if x > N: continue elif (N-k) % 2 == 0:#太郎 #print(i,x) d...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; int a[3000]; vector<vector<ll>> dp(3000, vector<ll>(3000, -1)); ll fun(int l, int r){ if(dp[l][r] != -1) return dp[l][r]; if(l == r){ return dp[l][l] = a[l]; } return dp[l][r] = max(a[l] - fun(l+1, r),a[r] - fun(l, r-1)); } int main(){ int...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
N = int(input()) A = list(map(int,input().split())) dp = [[None for j in range(N-i)] for i in range(N)] for i,a in enumerate(A): dp[i][N-i-1] = a * (1 if N%2 else -1) for k in range(N-2,-1,-1): for l in range(k+1): r = k-l if k%2: dp[l][r] = min(dp[l+1][r] - A[l], dp[l][r+1] - A[-...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; public class Main { private static long solve(int N, int[] a) { long[][] score = new long[N][N]; int parity = N % 2; for (int i = N-1; i >=0; i--) score[i][i] = parity == 1 ? a[i]: -a[i]; for (int e = 1; e < N; e++) for (int s = e - 1, t...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; using ll=long long; int n; ll dp[3005][3005],a[3005]; int main(){ cin>>n; for(int i=1;i<=n;++i){ cin>>a[i]; } for(int L=n;L>0;--L){ for(int R=L;R<=n;++R){ dp[L][R]=max(a[L]-dp[L+1][R],a[R]-dp[L][R-1]); } } cout<<dp[1][n]<<'\n'; return 0; }
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> #define FOR(i,a,b) for(int i = (a); i < (b); i++) #define ll long long #define N 3010 using namespace std; ll n,a[N],dp[N][N]; int main() { cin>>n; FOR(i,0,n) cin>>a[i],dp[i][i]=a[i]; FOR(i,1,n) FOR(j,0,n-i+1) dp[j][j+i]=max(a[j]-dp[j+1][j+i],a[j+i]-dp[j][j+i-1]); cout<<dp[0][n-1]...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.StringJoiner; import java.util.StringTokenizer; import java.util.function.IntUnaryOperator; import java.util.function.LongUnaryOper...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <cstdio> #include <algorithm> #include <stdint.h> int as[3005]; int64_t dp[3005][3005]; int main(){ int N; scanf("%d",&N); for(int i=0;i<N;i++){ scanf("%d",&as[i]); } for(int i=0;i<=N;i++){ dp[i][i]=0; } for(int i=N-1;i>=0;i--){ for(int j=i+1;j<=N;j++){ dp[i][j]=std::max(as[i]...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; public class Main { private final static InputReader inputReader = new InputReader(System.in); private final s...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); long[][] dp = new long[n][n]; for(int i=0;i<n;i++)dp[i][i]=a[i]; for(int i=n-2;i>=0;i--){...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) ns = list(map(int, input().split())) dp = [[0] * n for i in range(n+1)] for i in range(n+1, -1, -1): for j in range(i, n): if n % 2 == (i + j+1) % 2: dp[i][j] = max(dp[i + 1][j] + ns[i], dp[i][j - 1] + ns[j]) else: dp[i][j] = min(dp[i + 1][j] - ns[i], dp[i][j...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; //dp[i] first player winns if its true stones left long long int dp[3005][3005]; int main(){ int n; cin>>n; int arr[n]; for(int i=0;i<n;i++) cin>>arr[i]; int r=0,l=n-1; for(l=n-1;l>=0;--l){ for(r=l;r<n;++r){ if(l==r){ dp[l][r]=arr[l]; } else{ d...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
N=int(input()) a=list(map(int,input().split())) dp=[[0]*(N+1) for i in range(N+1)] for W in range(N): for l in range(N-W): r=l+W if (N-W+1)%2==0: dp[l][r]=max(a[l]+dp[l+1][r],dp[l][r-1]+a[r]) else: dp[l][r]=min(dp[l+1][r]-a[l],dp[l][r-1]-a[r]) print(dp[0][N-1])
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) a = list(map(int,input().split())) dp = [[0 for i in range(n+1)] for j in range(n+1)] for i in range(1,n+1): dp[i][i] = a[i-1] for w in range(1,n): for i in range(1,n-w+1): j = i+w dp[i][j] = max(a[i-1]-dp[i+1][j],a[j-1]-dp[i][j-1]) print(dp[1][n])
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; const int N = 3005; long long dp[N][N],v[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i<=n; i++) { cin >> v[i]; dp[i][i] = v[i]; } for (int i = n-1; i>=1; i--) ...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
def main(): N = int(input()) A = list(map(int, input().split())) c = [0] * N for r in reversed(range(N)): ar = A[r] c[r] = t = ar for l in range(r + 1, N): c[l] = t = max(ar - c[l], A[l] - t) print(c[-1]) main()
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = input() a = map(int, raw_input().split()) oo = sum(a) + 1 dp_max = [[-oo] * n for _ in range(n)] dp_min = [[oo] * n for _ in range(n)] for i in range(n): dp_max[i][i] = a[i] dp_min[i][i] = -a[i] for i in range(1, n): for j in range(n - i): k = i + j dp_max[j][k] = max(dp_min[j + 1][...
PYTHON