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
n=int(input()) a=list(map(int,input().split())) dp=[[0]*(n) for i in range(n)] if n%2!=0: for i in range(n): dp[i][i]=a[i] else: for i in range(n): dp[i][i]=-a[i] for i in range(n-1)[::-1]: for g in range(i,n): if (i+g)%2!=n%2: dp[i][g]=max(dp[i][g-1]+a[g],dp[i+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
def solve(l, r, A, dp): if r - l < 2: return max(A[l:r + 1]) x1 = dp[l + 2][r] if dp[l + 2][r] != -1 else solve(l + 2, r, A, dp) x2 = dp[l + 1][r - 1] if dp[l + 1][r - 1] != -1 else solve(l + 1, r - 1, A, dp) x3 = dp[l][r - 2] if dp[l][r - 2] != -1 else solve(l, r - 2, A, dp) dp[l][r] = ma...
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=[] 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 - 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 { private static long solve(int N, int[] a) { long[] score = new long[N+1]; for (int e = 0; e < N; e++) for (int s = e; s >= 0; s--) score[s] = Math.max(a[s] - score[s+1], a[e] - score[s]); return score[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
#if 1 #include<bits/stdc++.h> using namespace std; long long a[3001]; long long d[3001]; int main() { long long n; cin >> n; for(int i = 1;i <= n;i++) { scanf("%lld",&a[i]); } for(int i = 1;i <= n;i++) { for(int j = 1;j+i-1 <= n;j++) { d[j] = max(a[j]-d[j+1],a[j+i-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.*; import java.util.*; class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputS...
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())) 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
import java.util.*; import java.io.*; /* AtCoder contest code coder : yoichidon */ public class Main { public static String longer(String x, String y){ if(x.length() > y.length()) return x; else return y; } public static void main(String[] args) { Scanner sc = new Scanner(Sy...
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]*N for i in range(N)] for i,a in enumerate(A) : dp[i][i] = a for i in range(N-2, -1, -1) : for j in range(i+1, N) : left = A[i] - dp[i+1][j] right = A[j] - dp[i][j-1] dp[i][j] = max(left, right) ans = dp[0][N-1] print(ans)
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 mxN=3000; int n, a[mxN]; long long dp[mxN+1][mxN+1]; int main() { ios::sync_with_stdio(0); cin.tie(0); 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...
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 = 3000 + 10; int n; ll a[MAXN], f[MAXN][MAXN]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]); for (int l = n; l >= 1; --l) { for (int r = l; r <= n; ++r) { f[l][r] = ma...
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.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 void solve() { int 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
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int arr[n]; for(int i=0;i<n;i++)scanf("%d",&arr[i]); long long dp[n][n]; for(int i=0;i<n;i++)dp[i][i]=arr[i]; for(int i=1;i<n;i++){ for(int j=0;j<n-i;j++){ dp[j][j+i] = max(arr[j+i]-dp[j][j+i-1],arr[...
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 L in range(n - 1, -1, -1): for R in range(L, n): if L == R: dp[L][R] = a[L] else: dp[L][R] = max(a[L] - dp[L + 1][R], a[R] - dp[L][R - 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 <cstdio> #include <algorithm> int n; int A[3000]; long long int dp[3001][3001]; int main(){ scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d", A+i); for(int i = n-1; i >= 0; i--){ for(int j = i + 1; j <= n; j++){ dp[i][j] = std::max(A[j-1] - dp[i][j-1], A[i] - dp[i+1][j]); } } p...
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.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(sc.next()); } l...
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.Arrays; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author silviase */ public class Main { public static void main(String[] arg...
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.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; public class Main { public static class pair implements Comparable<pair> { int id; int ans; pair(int x, int y) { id = x; ans = y; } public pair() { } public int compareTo(pair o) { ...
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
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<int>A(n, 0); for(int i = 0; i < n; i++){ cin >> A[i]; } vector< vector<long long int> > dp(n, vector<long long int> (n, 0) ); for(int i = 0; i < n; i++){ dp[i][i] = A[i]; } for(int k = 1; k < n; k++){ for(int i = 0; (i + k)...
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; #define int long long #define rep(i,n) for(int i=0;i<n;i++) int n,a[3334],dp[3334][3334]; signed main(){ cin>>n; rep(i,n)cin>>a[i]; for(int i=n-1;i>=0;i--){ for(int j=i;j<n;j++){ if((n-i-j-1)%2==0){//taro dp[i][j]=max(dp[i+1][j]+a[i],dp[i][j-1]+a[j]); } 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
import java.util.*; 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+1][N+1]; for(int i=0;i<N;i++) dp[i][i]=0; for(int len = 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; long long int a[3001]; long long int dp[3001]; int main(){ long long int n,i,j; cin>>n; for(i=1;i<=n;i++)scanf("%d",&a[i]); for(i=1;i<=n;i++) for(j=1;j+i-1<=n;j++) dp[j]=max(a[j]-dp[j+1],a[j+i-1]-dp[j]); cout<<dp[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.Scanner; import java.util.Vector; public class Main { static int a[]; static long dp[][]; static int g[][]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); dp = new long [n][n]; g = new int[n][n]; a = new int[n]; for (int i=0;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; #define ll long long #define fo(i, n) for(i=0; i<n; i++) #define Fo(i, k, n) for(i=k; i<n; i++) int main() { int n, i, j; cin >> n; ll dp[n], a[n]; fo(i, n) cin >> a[i], dp[i] = a[i]; Fo(i, 1, n) fo(j, (n - i)) dp[j] = max(a[i + j] - dp[j], a[j] - dp[j + 1]); cout <...
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.Arrays; import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { 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 + 1][N + 1]; 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 <bits/stdc++.h> using namespace std; const int N = 3e3 + 10; int a[N]; long long f[N][N]; int main() { int n; cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; for(int st = n; st >= 1; st--) for(int en = st; en <= n; en++) f[st][en] = max(a[st] - f[st + 1][en], a[en] -...
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 coun(a,i,j): if dp[i][j]!=-1: return dp[i][j] if i==j: return a[i] elif abs(i-j)==1: return max(a[i],a[j]) dp[i][j]= max(min(coun(a,i+2,j),coun(a,i+1,j-1))+a[i],min(coun(a,i+1,j-1),coun(a,i,j-2))+a[j]) return dp[i][j] n= int(input()) a =[int(X ) for X in input().spli...
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 MOD=1e9+7; const int INF=1e9; int main(){ int n; cin>>n; ll a[n]; ll dp[n][n]; for(int i=0;i<n;i++){ cin>>a[i]; dp[i][i]=a[i]; } for(int i=2;i<=n;i++){ for(int j=0;j<=n-i;j++){ 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())) def calc(): 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]) return dp dp = calc() 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
#include<bits/stdc++.h> #define long long long using namespace std; const int N=3e3; int aa[N+2],n;long dp[N+2][N+2]; long dfs(int i,int j) { if(i>j)return 0; if(dp[i][j]!=-1)return dp[i][j]; return dp[i][j]=max(aa[i]-dfs(i+1,j),aa[j]-dfs(i,j-1)); } int main() { ios_base::sync_with_stdio(0);cin.tie(0); cin>>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
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.io.IOException; import java.io.UncheckedIOException; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /**...
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 read = sys.stdin.read sys.setrecursionlimit(10 ** 7) def solve(): n, *a = map(int, read().split()) a = tuple(a) dp = [0] * n ** 2 for i in range(n): dp[i * n + i] = a[i] for i in range(n - 2, -1, -1): for j in range(i + 1, n): L = a[i] - dp[(i + 1) * n + 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; typedef long long ll; int main(){ ll n; cin>>n; vector<ll> a(n); for(ll i=0;i<n;i++) cin>>a[i]; vector<vector<ll>> dp(n,vector<ll>(n)); for(ll i=0;i<n;i++) dp[i][i]=a[i]; for(ll i=n-1;i>=0;i--){ for(ll j=i+1;j<n;j++){ 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
import java.util.Arrays; import java.util.Scanner; public class Main { private static long max(long dp[][], int i, int j,int n, int arr[]) { if(i==j) { dp[i][j] = arr[i]; return dp[i][j]; } if( dp[i][j]!=-1) return dp[i][j]; dp[i][j]= Math.max(arr[i]-max( dp, i+1,j,n, arr), arr[j]- max(dp, i, j-1,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
#include <iostream> #include <vector> using namespace std; #define ll long long ll dp[3005]; int main() { ll n; cin >> n; vector<ll> num(n); for (int i = 0; i < n; ++i) { cin >> num[i]; } for (int i = 0; i < n; ++i) { for (int j = i; j >= 0; --j) { if (j == i) { dp[j] = num[i]; } else { ...
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.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; public class Main{ // static final long mod=1000000007; // static int dx[]={-1,0,1,0}; // static int dy[]={0,-1,0,1}; public static void main(String[] args) ...
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]*(n+1) for j in range(n+1)] ## dp[j][i]:iは始点、jは列の長さを表す for i in range(n): dp[0][i] = 0 for j in range(1,n+1): for i in range(n): if i+j <= n: dp[j][i] = max(a[i]-dp[j-1][i+1],a[i+j-1]-dp[j-1][i]) print(dp[n][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
#include <bits/stdc++.h> #define ll long long #define fop(i,m,n) for(ll i=m;i<n;i++) #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front using namespace std; int main(){ ll n; cin >> n; ll dp[n][n]; ll a[n]; fop(i,0,n){ cin >> a[i]; dp[i][i] = a[i]; } fop(i,1,n){ for(ll l=...
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 f(a,i,j,dp): if dp[i][j]==-1: if i>j: return 0 dp[i][j]=max(a[i]+min(f(a,i+2,j,dp),f(a,i+1,j-1,dp)), a[j]+min(f(a,i+1,j-1,dp),f(a,i,j-2,dp))) return dp[i][j] n=int(input()) dp=[[-1 for i in range(n)] for j in range(n)] a=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
N = int(input()) a = list(map(int, input().split())) M = 10 ** 15 DP = [[0] * N for _ in range(N)] if N % 2 == 0: t = -1 else: t = 1 for i in range(N): DP[0][i] = a[i] * t t *= -1 for i in range(1, N): for j in range(N - i): if t > 0: DP[i][j] = max(DP[i - 1][j] + a[i + j], DP[i - 1][j + 1] + a[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 <algorithm> #include <iostream> using namespace std; int n; long long dp[3100][3100]; int main() { cin >> n; for (int l = 0; l < n; l++) { cin >> dp[l][l+1]; } for (int l = n-2; l >= 0; l--) { for (int r = l+2; r <= n; r++) { dp[l][r] = max(dp[l][l+1]-dp[l+1][r], dp[r-1][r]-dp[l][r-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; int main() { int n; cin>>n; int a[n]; for(int i=0;i<n;i++) cin>>a[i]; long long int dp[n][n]; for(int i=0;i<n;i++) dp[i][i]=a[i]; for(int i=1;i<n;i++) for(int j=0;j+i<n;j++) dp[j][j+i]=max(a[j]-dp[j+1][j+i],a[j+i]-dp[j][j+i-1]); cout<<dp[0][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
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int n = sc.nextInt(); int ar[] = new int[n]; for(int 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; #define ll long long ll a[3005]; ll mo[3005][3005]; int n; ll dp(int l,int r); int main() { cin >> n; for(int i=0; i<n; i++) cin >> a[i]; memset(mo, -1, sizeof(mo)); cout << dp(0,n-1) << endl; return 0; } ll dp(int l, int r){ if(l == r) return a[l]; 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())) n = N % 2 DP = [[0] * (N+1) for _ in range(N+1)] for w in range(1, N+1): for i in range(N): j = i + w if j > N: continue if (w+n) % 2 == 1: DP[i][j] = min(DP[i+1][j] - A[i], DP[i][j-1] - A[j-1]) else: ...
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.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long dp[][] = new long[n + 1][n + 1]; for ( int i = 1; i <= n; i++ ) { dp[i][i] = in.nextInt(); } in.close(); for ( int col = 2; col <= n; col++ ) { 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.*; import java.util.*; //import javafx.util.*; import java.math.*; //import java.lang.*; public class Main { // static int n;static ArrayList<Integer> arr[]; // int ans[]; public static void main(String[] args) throws IOException { // Scanner sc=new Scanner(System.in); Pr...
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]*(N+1) for _ in range(N+1)] for i in range(1, N+1): for j in range(N-i+1): dp[i][j] = max(-dp[i-1][j] + A[j+i-1], -dp[i-1][j+1] + A[j]) print(dp[-1][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.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author NMouad21 */ public class Main...
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[3007][3007]; int main(){ int n; cin >> n; vector<ll> a(n,0); ll sum = (ll)0; 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(L==R){ dp[L][R] = a[L]; } 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
import java.io.*; import java.util.*; class Solver { final int n; final long[] as; long[][] table; Solver(int n, long[] as) { this.n = n; this.as = as; } private long solve(int rangeMin, int rangeMax) { if (rangeMin == rangeMax) { return 0; } if (table[rangeMin][rangeMax] !=...
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.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper...
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 int n; ll f[3010][3010],a[3010]; int main() { scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%lld",&a[i]); f[i][i]=a[i]; } for (int l=1;l<n;l++) { for (int i=1;i+l<=n;i++) { int j=i+l; f[i][j]=max(a[i]-f[i+1][j],a[j]-f[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
def main(): n = int(input()) *a, = map(int, input().split()) memo = [[-1] * n for _ in range(n)] for i, x in enumerate(a): memo[i][i] = x for l in range(n - 1, -1, -1): # ここの感覚がつかめない for r in range(l + 1, n): memo[l][r] = max( a[l] - memo[l + 1][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
#include <bits/stdc++.h> using namespace std; const int N = 3005; int n; long long a[N], f[N][N]; int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int l = n; l > 0; --l) { f[l][l] = a[l]; for (int r = l + 1; r <= n; ++r) { f[l][r] = max(a[l] - f[l + 1][r], a[r] - f[l][r - 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.text.*; public class Main { static long MOD = (long)1e9+7; public static void main(String args[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long[] a = new long[n]; for(int i = 0; i< n; i++)a[i] = in.nextLong(); ...
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 n; cin >> n; vector<long long> a(n); vector<vector<long long> > dp(n,vector<long long>(n)); for(int i = 0; i < n; i++){ cin >> a[i]; dp[i][i] = a[i]; } for(int i = 1; i < n; i++){ for(int j = 0; j < n-i; j++){ int l = j, r = j + i; dp[l]...
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.Arrays; import java.util.Scanner; public class Main { static int n; static int[] a; static long[][][] dp; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); n = sc.nextInt(); a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.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
n = int(input()) *A, = map(int, input().split()) DP = [[0 for j in range(n+1)] for i in range(n+1)] for l in range(1, n+1): for i in range(n-l+1): j = i+l DP[i][j] = max(A[i]-DP[i+1][j], A[j-1]-DP[i][j-1]) print(DP[0][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
import java.io.*; import java.math.*; import java.util.*; public class Main { static MyScanner in = new MyScanner(); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int N; static int max = 200001; static int [] dr = {1,-1,0,0}; static int [] dc={0,0,-1,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
n=int(input()) a=list(map(int,input().split())) dp=[[0]*(n+1) for _ in range(n+1)] for i in range(1,n+1): for l in range(n+1): r=i+l if r>n: continue if (n-i)%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],...
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 a[30010],dp[30010]; int main() { ll 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+i-1<=n; j++) { dp[j]=max(a[j]-dp[j+1],a[j+i-1]-dp[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> #define int long long using namespace std; const int N = 3001; int dp[N][N]; int arr[N]; int32_t main(){ int n ; 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;r++){ if( l == r ){ dp[l][r] = arr[l]; }else { dp[l][r] = max(arr[l...
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; #define int long long int n,a[3005]; int f[3005][3005]; signed main() { cin>>n; for(int i=1;i<=n;cin>>a[i++]); for(int i=1;i<=n;f[i][i]=a[i],i++); for(int i=2;i<=n;i++) for(int l=1;l+i-1<=n;l++) { int r=l+i-1; f[l][r] = max(a[l]-f[l+1][r],-f[l][r-1]+a[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
#include <iostream> using namespace std; long long i,n,a[3009],d[3009][3009]; bool b[3009][3009]; long long dp(int l,int r){ if(l==r) return d[l][r]=a[l]; if(b[l][r]) return d[l][r]; b[l][r]=1; d[l][r]=max(a[l]-dp(l+1,r),a[r]-dp(l,r-1)); return d[l][r]; } int main() { cin>>n; for(i=0;i<n;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.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main{ static class FastReader { BufferedReader br; StringTokenizer st; public FastR...
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=[int(i) for i in input().split()] dp=[[0 for _ in range(n+1)] for _ in range(n+1)] for w in range(n): for l in range(n-w): r=w+l+1 dp[l][r] = max(-dp[l+1][r]+A[l],-dp[l][r-1]+A[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
# n=int(input()) n=int(input()) a=list(map(int,input().split(" "))) mat=[[(0,0) for i in range(n)] for j in range(n)] for l in range(1,n+1): for i in range(n-l+1): j=i+l-1 # print(i,j) # if(i<=j): if(i==j): x=a[i] y=-a[i] mat[i][j]=(x,y) else: # print(i+1) x=max(a[i]+mat[i+1][j][1],a[j]+mat[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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(reader.readLine()); St...
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.*; public class Main { static int N; static int[] A; public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); N = sc.nextInt(); A = sc.nextIntArray(N); System.out.println(solve()); } 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.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.io.Reader; import java.io.PrintWriter; import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.ArrayDeque; import java.util.List; import java.util.Queue; import java.util....
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]*n for _ in range(n)] # dp[i][j]:i<=j,i番目からj番目までの数列(j-i+1個)を前にした時、ゲーム終了までに自分が取れる最大得点。i,j=0,..,n-1 sa=[0] s=0 for ai in a: s+=ai sa.append(s) for len in range(1,n+1): for l in range(n-len+1): r=l+len-1 s=sa[r+1]-sa[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> using namespace std; int n; int arr[5000]; long long dp[3005][3005]; long long rec(int l, int r){ if(l == r)return arr[l]; if(dp[l][r] != -1)return dp[l][r]; return dp[l][r] = max(arr[l] - rec(l+1, r), arr[r] - rec(l, r-1)); } int main(){ cin >> n; for(int i = 0; i <= n; i++) for(int 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> #define ll long long using namespace std; const int N = 3004; ll dp[N][N]; // dp(i,j) the optimal value of x-y in interval (i->j) ll a[N]; int n; int main() { cin>>n; for(int i=1;i<=n;i++) { scanf("%I64d",&a[i]); dp[i][i] = a[i]; } for(int len=2;len<=n;len++) { for(int l=1;l+len<...
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<cstdio> #define int long long #define max(a,b)((a)>(b)?(a):(b)) const int N=3003; int f[N][N],n,a[N]; signed main(){ scanf("%lld",&n); for(int i=1;i<=n;i++)scanf("%lld",a+i),f[i][0]=a[i]; for(int k=1;k<n;k++) for(int i=1;i+k<=n;i++) f[i][k]=max(a[i]-f[i+1][k-1],a[i+k]-f[i][k-1]); printf("%lld",f[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
def main(): n = int(input()) a = tuple(map(int, input().split())) dp = [[0] * (n + 1) for _ in range(n+1)] for len in range(1, n + 1): for i in range(0, n - len + 1): j = i + len if (n - len) % 2 == 0: dp[i][j] = max(dp[i+1][j] + a[i], dp[i][j-1] + a[j-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]*(n+1) for _ in range(n+1)] for l in range(1,n+1): for i in range(n-l+1): j=i+l-1 if (n-l)%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
import java.util.*; import java.io.*; public class Main { public static void main(String abc[]) { 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]; 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
N = int(input()) A = list(map(int, input().split())) dp = [[0 for _ in range(N)] for _ in range(N)] for index in range(N): dp[index][index] = A[index] 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] = max(l,r) print(dp[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
n=int(input()) *a, =map(int,input().split()) dp=[[0]*(n+1) for _ in range(n+1)] for length in range(1, n+1): for l in range(n-length+1): r=l+length if (n-length)%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][r-1]-a[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.util.Arrays; import java.util.Scanner; public class Main { static final Scanner sc = new Scanner(System.in); static final int MOD = (int) 1E9 + 7; static final long INF_L = (long) 4E18; public static void main(String[] args) { int N = nint(); int[] a = nints(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
/* Author: Anthony Ngene Created: 13/08/2020 - 18:33 */ import java.io.*; import java.util.*; public class Main { // Service to others is the rent you pay for your room here on earth. - Muhammad Ali static Long[][][] dp; static long[] arr; public static void main(String[] args) throws IOExcept...
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 n, dp[3004][3004], a[3004]; vector<long long> stones; int main() { cin>>n; for(long long i = 0; i < n; i++){ cin>>a[i]; } for(long long L = n-1; L >= 0; --L){ for(long long R = L; R < n; ++R){ dp[L][R] = L == R ? a[L] ...
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.Arrays; import java.util.Scanner; public class Main { void run() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } // dp[i][j] ... 区間[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
#include <bits/stdc++.h> using namespace std; using LL = long long; const int MAXN = 3010; int A[MAXN]; LL dp[MAXN][MAXN]; int N; int main() { cin >> N; for(int i=1; i<=N; i++) cin >> A[i]; memset(dp, 0, sizeof(dp)); for(int i=N; i>0; i--) { for(int j=i; j<=N; j++) { if(i==j) dp[i][j] = A[i]; dp[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
#!/mnt/c/Users/moiki/bash/env/bin/python # FROM : https://atcoder.jp/contests/dp/tasks/dp_l # N,M = map(int, input().split()) N = int(input()) # A = list(map(int, input().split())) A = tuple(map(int, input().split())) dp = [ [0]*(N+1) for _ in range(N+1)] for d in range(1,N+1): for l in range(N+1-d): r = 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
n=int(input()) a=list(map(int,input().split())) dp=[[0 for i in range(n+1)] for j in range(n+1)] for len in range(1,n+1): for i in range(n+1): if i+len>n: break j=i+len if (n - len) % 2 == 0: dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j - 1]) #print(len,dp) else: 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
#include<bits/stdc++.h> using namespace std; long long n, k, a[3005], dp[3005][3005]; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 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++){ dp[l][r] = l ==...
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 int ll; int main() { ll i,j,n; cin>>n; ll a[n]; for(i=0;i<n;i++) { cin>>a[i]; } vector<vector<ll> > dp(n+1, vector<ll>(n+1, 0)); for (i = 1; i <= n;i++){ for (j = i; j > 0 ;j--){ dp[i][j] = max(a[i-1] - dp[i - 1][j], a[j-1] - 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
#include <iostream> using namespace std; long long n,i,a[3005],d[3005][3005],k; int main() { cin>>n; for(i=1;i<=n;i++) { cin>>a[i]; d[i][i]=a[i]; } for(k=2;k<=n;k++) { for(i=1;i<=n-k+1;i++) { d[i][i+k-1] = max(a[i]-d[i+1][i+k-1],a[i+k-1]-d[i][i+k-2]); } ...
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.PrintWriter; import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import java.util.StringTokenizer; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args) throws Exception { int n = sc.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 sys sys.setrecursionlimit(10**6) N = int(input()) ali = list(map(int,input().split())) inf = 10**15 dp = [[inf]*(N+1) for _ in range(N+1)] #i-j番目までが残っているときの利益の最大値 for i in range(N+1): dp[i][i] = 0 for j in range(1,N+1): for i in range(N+1): if i+j > N: break dp[i][i+j] = max(-dp[i+1][i+j]+ali...
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.*; public class Main { public static final long mod = (long)1e9+7; public static final long INF = Long.MAX_VALUE/10; public static final int inf = Integer.MAX_VALUE/10; public static boolean[][] use1, use2; public static long[][] dp1, dp2; public static 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
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]) # 下記提出と全く同じ内容で提出します # https://atcoder.jp/conte...
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]*(N+1) for i in range(N+1)] #dp[a][b]でa:残っている数列の長さ b:左端 for w in range(1,N+1): for l in range(N-w+1): r=l+w dpl=-dp[l+1][r]+a[l] dpr=-dp[l][r-1]+a[r-1] dp[l][r]=max(dpl,dpr) print(dp[0][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> #define LL long long using namespace std; LL a[30010],dp[30010]; int main(){ LL 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+i-1<=n;j++) dp[j]=max(a[j]-dp[j+1],a[j+i-1]-dp[j]); cout<<dp[1]<<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
#include <bits/stdc++.h> using namespace std; int a[3010]; long dp[3010][3010]; int main(){ int n; scanf("%d", &n); for(int i=0; i < n; i++) scanf("%d", &a[i]); for(int i=n-1; i >= 0; i--) { for(int j=i; j < n; j++) { if(i == j) { dp[i][j] = a[i]; }else{ dp[i][j] = max(a[i] - dp[i+1][j], a[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 <stdio.h> #include <vector> using namespace std; typedef long long ll; const int C=3001; int n; ll a[C], dp[C][C]; int main(){ int i, j; scanf ("%d", &n); for (i=0;i<n;i++) scanf ("%lld", &a[i]); for (i=0;i<n;i++){ for (j=0;j<n;j++){ if (j+i>=n) break; if (i==0) dp[j][j]=a[j]; else dp[j][j+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()) li=list(map(int,input().split())) dp=[[0]*n for _ in range(n)] for l in range(n-1,-1,-1): for r in range(l,n): if l==r: dp[l][r]=li[l] else: dp[l][r]=max(li[l]-dp[l+1][r],li[r]-dp[l][r-1]) print(dp[0][-1])
PYTHON3