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())
arr = list(map(int, input().split()))
dp = [[0]*(N+1) for _ in range(N+1)]
for i in range(1, N+1):
for k in range(N-i+1):
j = k+i
if (N-i)%2 == 0:
dp[k][j] = max(dp[k+1][j]+arr[k], dp[k][j-1]+arr[j-1])
else:
dp[k][j] = min(dp[k+1][j]-arr[k], dp[k][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 | # input
N = int(input())
a = list(map(int, input().split()))
# dp[i][j]: a[i], a[j] の選択肢があるとき、その後に蓄積されるX-Yの最大値
dp = [[0] * N for i in range(N)]
for i in range(N - 1, -1, -1):
for j in range(i, N):
if i == j:
dp[i][j] = a[i]
else:
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1])
print(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.*;
import java.util.*;
import java.lang.*;
public class Main implements Runnable {
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
p... | 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.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.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;
int n;
int a[3010];
long long f[3010][3010];
char v[3010][3010];
inline long long dp(int l,int r){
if(l==r)return f[l][r]=a[l];
if(v[l][r])return f[l][r];
v[l][r]=1;
f[l][r]=max(a[l]-dp(l+1,r),a[r]-dp(l,r-1));
return f[l][r];
}
int main(){
scanf("%d",&n);
for(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 | import sys
input = sys.stdin.readline
n = int(input())
a = tuple(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(1,n):
for j in range(n-i):
if (n-i)%2 == 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[3003],dp[3003][3003];
int main(){
cin>>n;
for(int k=1;k<=n;k++)
cin>>a[k];
for(int k=1;k<=n;k++)
dp[k][k]=a[k];
for(int len=2;len<=n;len++){
for(int i=len;i<=n;i++){
int k=i-len+1;
dp[k][i]=max(-dp[k+1][i]+a[k],-dp[k][i-1]+a[i]);
}
}
cou... | 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;
class Main{
static int N;
static int[] a;
static long[][][] memo;
static boolean[][][] flag;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
a = new int[N];
memo = new long[2][N][N];
flag = new boolean[... | 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)]
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 sys
input = sys.stdin.readline
MOD = 10**9 +7
INF = 10**9
def main():
n = int(input())
a = list(map(int,input().split()))
dp = [[0] * (n + 2) for _ in range(n + 2)]
for l in range(1,n + 1):
for i in range(n - l + 1):
j = i + l
if (n - l)%2 == 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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public long getBalance(int[] A, int L, int R, long[][] memo) {
// System.out.println(String.format("L: %d, R: %d", L, R));
if (L == R) {
return A[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 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; ++i) {
cin >> a[i];
}
vector<vector<ll>> dp(N, vector<ll>(N));
for (int L = N - 1; L >= 0; --L) {
for (int R = L; R < N; ++R) {
if (L == R)
dp[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 sys
read = sys.stdin.read
sys.setrecursionlimit(10 ** 7)
def solve():
n, *a = map(int, read().split())
a = tuple(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][... | 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+2) for _ in range(n+2)]
# aiからajのX−Yの最大値
# [左端,右端)=[l,r)からの遷移
# 左をとる[l+1,r)、右をとる[l,r−1)の2通り
for len in range(1,n+1):
for i in range(n+1-len):
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])
#後手の番
e... | 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 for _ in range(N)]
if N%2 == 0:
for i in range(N):
dp[0][i] = -a[i]
else:
for i in range(N):
dp[0][i] = a[i]
for i in range(1, N):
for j in range(N-i):
if (N-i)%2 == 1:
dp[i][j] = max(dp[i-1][j]+a[i+j], 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;
#define N 3005
#define ll long long
ll dp[N][N], n, arr[N];
ll cal(int i, int j)
{
if(j < i) return 0;
if(dp[i][j] != -1) return -dp[i][j];
dp[i][j] = max(arr[i] + cal(i + 1, j), arr[j] + cal(i, j - 1));
return -dp[i][j];
}
int main()
{
memset(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 | #include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll dp[3002][3002];
int main()
{ //dp[i][j] answer of segment [i,j]
int n;cin>>n;
ll a[n];
for(int i=0;i<n;i++)
cin>>a[i];
for(int l=n-1;~l;l--)
for(int r=l;r<n;r++)
{
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]... | 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 db double
#define ll long long
const int MX=3000;
ll dp[MX+5][MX+5];
ll a[MX+5];
int n,k;
ll solve(int i, int j)
{
if(i>j)return 0;
if(~dp[i][j])return dp[i][j];
ll mx;
mx=max(a[i]-solve(i+1,j),a[j]-solve(i,j-1));
return dp[i][j]=mx;
}
int main()
{
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 | #include<bits/stdc++.h>
using namespace std;
void solve(){
int n,K,x,y,p,q;
cin>>n;
vector<long long>A(n);
for(int i=0;i<n;i++) cin>>A[i];
long long dp[n][n];
for(int i=0;i<n;i++){
dp[i][i]=A[i];
}
for(int k=1;k<n;k++){
for(int i=0;i<n-k;i++){
int j=i+k;
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 | /*
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
public static void main(String[] args) throws IOException {
in = new FastScanner();
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 | n = int(input())
arr = list(map(int, input().strip().split()))
mem = [[0 for i in range(n)] for i in range(n)]
for i in range(n):
mem[i][i] = arr[i]
for diff in range(1, n):
for L in range(n-diff):
H = L+diff
mem[L][H] = max(arr[L]-mem[L+1][H], arr[H]-mem[L][H-1])
print(mem[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 | 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):
g0 = dp[i]
g1 = dp[i + 1]
for j in range(i + 1, n):
L = a[i] - g1[j]
R = a[j] - g0[j - 1]
dp[i][j] = L if L > R else R
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 maxn = 1000050;
typedef long long ll;
ll a[maxn];
ll dp[maxn];
int main(){
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+i-1<=n;j++){
dp[j]=max(a[j]-dp[j+1],a[j+i-1]-dp[j]);
}
}
cout<<dp[1]<<endl;
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 | import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.*;
class Main{
public static FastReader fs = new F... | 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 d[3030][3030];
int main()
{
int n;
cin >> n;
for (int i=1 ; i<=n ; i++)
cin >> d[i][i];
for (int i=1 ; i<=n ; i++)
for (int j=i-1 ; j>0 ; j--)
d[i][j] = max(d[i][i] - d[i-1][j] , d[j][j] - d[i][j+1]);
cout << d[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 | N = int(input())
a = list(map(int, input().split()))
dp = [[-1 for i in range(3010)] for j in range(3010)]
for i in range(3010):
dp[i][i] = 0
for len in range(1, N+1):
for i in range(N - len + 1):
j = i + len
if (N - len) % 2 == 0: # 前が誰の手番であったか
# 先手番
dp[i][j] = max(dp[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;
static Long[][][] memo;
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
n = in.nextInt();
a = new long[n];
for (int i = 0; i < n; i++)
a[i] = in.nextLong();
memo = new Long[2][n][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 <cstdio>
#include <vector>
using namespace std;
typedef long long ll;
ll opt[3010][3010];
int main() {
int n; scanf("%d", &n);
vector<int> a(n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
for (int i = n - 1; i >= 0; --i) {
opt[i][i] = a[i];
for (int j = i + 1; j < n; ++j) {
opt[i][j] = max(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 | #include<bits/stdc++.h>
using namespace std;
#define ll long long int
const int N=3005;
ll a[N],dp[N][N];
int main() {
int n;
cin>>n;
for(int i=0;i<n;i++) {cin>>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]-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 | import collections
from collections import deque
n=int(input())
a=deque(map(int, input().split()))
dp=[[0]*(n+1) for i in range(n+1)]
x=0
y=0
for i in range(n-1,-1,-1):
for j in range(i+1,n+1):
if (n-(j-i))%2==0:
dp[i][j]=max(dp[i][j-1]+a[j-1],dp[i+1][j]+a[i])
else:
dp[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 | N = int(input())
A = list(map(int, input().split()))
dp = [[0] * (N + 1) for _ in range(N + 1)]
for d in range(1, N + 1):
for i in range(N - d + 1):
j = i + d
if (N-d) % 2 == 1:
dp[i][j] = min(dp[i+1][j]-A[i], dp[i][j-1]-A[j-1])
else:
dp[i][j] = max(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 | import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
dp = [[0] * N for _ in range(N)]
for l in range(N):
for i in range(N-l):
if l == 0:
dp[i][i] = a[i]
else:
j = i + l
dp[i][j] = max(a[i] - dp[i+1][j], a[j] - dp[i][j-1])
pr... | 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 maxn 3002
using namespace std;
typedef long long LL;
int n;
int ar[maxn];
LL dp[maxn][maxn];
int main() {
scanf("%d",&n);
for( int i = 1 ; i <= n ; i++ )
scanf("%d",&ar[i]);
for( int len = 1 ; len <= n ; len++ )
for( int i = 1 ; i <= n-len+1 ; i++ ) {
int j = i+len-1;
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 | import sys
input=sys.stdin.readline
n=int(input())
A=tuple(map(int,input().split()))
DP=[[0]*(n+1) for _ in range(n+1)]
for j in range(1,n+1):
for i in range(n-j+1):
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>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
constexpr int MAXN = 3000;
ll dp[MAXN + 2][MAXN + 2];
int main() {
int N;
cin >> N;
for(int i = 0; i < N; i++)
cin >> dp[1][i];
for(int i = 2; i <= N; i++)
for(int j = 0; i + j <= 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>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define MAXC 100000
ll dp[3005][3005];
int main()
{
int n;cin>>n;
int a[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(l==r) dp[l][r] = a[l];
else dp[l][r] = max(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 | 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 = list(map(int, input().split()))
dp = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(n)[::-1]:
for j in range(n + 1):
if i >= j:
continue
dp[i][j] = max(a[j - 1] - dp[i][j - 1], a[i] - dp[i + 1][j])
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>
using namespace std;
int solve(vector<int> vec, int p1, int p2,int curr){
}
int main(){
int n;
cin>>n;
long long arr[n];
int input;
for(int i=0;i<n;i++){
cin>>arr[i];
}
long long dp[n][n];
for(int i=n-1;i>=0;i--){
for(int j=i;j<n;j++){
if(i==j){
dp[i][j]=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 | #include <bits/stdc++.h>
using namespace std;
long long n,i,j,v[3010],d[3010][3010];
int main()
{
cin>>n;
for(i=1; i<=n; i++)
{
cin>>v[i];
d[i][i]=v[i];
}
for(i=n; i>=1; i--)
{
for(j=i+1; j<=n; j++)
{
if(i+1<=n)d[i][j]=max(-d[i+1][j]+v[i],-d[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 = [[None]*n for _ in range(n)]
for d in range(n):
for l in range(n-d):
r = l + d
if d:
dp[l][r] = max(a[l] - dp[l+1][r], a[r] - dp[l][r-1])
else:
dp[l][r] = a[l]
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.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
long[][] memo = new long[n][n];
boolean[][] memoed = new boolean[n][n];
long[] a = new long[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.util.Arrays;
import java.util.Scanner;
public class Main {
private static Scanner in;
private static int n;
private static int[] a;
private static long[][] dp;
private static final int MOD = (int)1e9+7;
public static void main(String[] args) {
in = new Scanner(System.in);
n = 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 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Input... | 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 const MAX=3002;
long long dp[MAX][MAX];
void funk()
{long long i,j;
for(i=0;i<MAX;i++)
{for(j=0;j<MAX;j++)
{dp[0][0]=0;}}}
int main()
{long long i,j,n,r,k,p,w,y,sum,m,x;
cin>>n;
long long a[n];
for(i=0;i<n;i++)
{cin>>a[i];}
for(i=0;i<n;i++)
{dp[i][i]=a[i];}
for(i=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.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] A = new int[N+1];
for(int i = 1; i <= N; i++) A[i] = sc.nextInt();
// dp[i][j]:先頭がi番目、末尾がj番目から始まる時のX-Y(後手の時はY-X)
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
br.readLine();
Main app = ne... | 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 s = new Scanner(System.in);
int n = s.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextLong();
}
long[][] dp = new long[n][n];
for (int i = 0; i < n; i++)
Arrays.fill(dp[i], 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 | #include <bits/stdc++.h>
using namespace std;
int main(){
int n;cin>>n;
long long A[n];
for(int i=0; i<n; i++)cin>>A[i];
long long dp[n][n];
for(int l=n-1; l>=0; l--){
for(int r = l; r<n; r++){
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]);
}
}
}
cou... | 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 findans(i,j,li):
if i>=len(li) or j<0:
return 0
if i==j:
dp[i][j]=li[i]
if i+1==j:
dp[i][j]=max(li[i],li[j])
if dp[i][j]!=-1:
return dp[i][j]
ans1=li[i]+min(findans(i+2,j,li),findans(i+1,j-1,li))
ans2=li[j]+min(findans(i,j-2,li),findans(i+1,j-1,li))
dp[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.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;
public class Main{
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while ... | 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 pb push_back
#define mp make_pair
const int N=3050;
int a[N];
ll dp[N][N];
bool was[N][N];
ll DP(int l, int r)
{
if(was[l][r] || l>r) return dp[l][r];
was[l][r]=1;
dp[l][r]=max(a[l]-DP(l+1,r),a[r]-DP(l,r-1));
return dp[l][r];
}
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, = 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(j-1, -1, -1):
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 | N = int(input())
a = list(map(int,input().split()))
dp = [[0]*(N+1) for i in range(N+1)]
#数列の長さ
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>
#include <iostream>
using namespace std;
#define ll long long
int main() {
ll n;
cin>>n;
ll arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
ll dp[n][n];
for(int i=0;i<n;i++)
dp[i][i]=arr[i];
for(int d=1;d<n;d++){
for(int i=0;i<n-d;i++){
dp[i][i+d]=max(arr[i]-dp[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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Input... | 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 |
import java.util.Scanner;
public class Main {
static long[][] aa;
static long maxx(int ind1,int ind2,int[] arr,int flag){
if(ind1>=arr.length || ind2<0 || ind1>ind2){
return 0;
}
if(ind1==ind2){
return arr[ind1];
}
if(aa[ind1][ind2]!=-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>
#define ll long long
using namespace std;
ll a[5000];
ll dp[5000][5000]={0};
ll n;
ll solve(ll s=0,ll l=n-1){
if(s>l){
return 0;
}
if(dp[s][l]){
return dp[s][l];
}
dp[s][l]=max(a[s]-solve(s+1,l),a[l]-solve(s,l-1));
return dp[s][l];
}
int main(){
cin>> n;
for(int 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 | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e12;
int main() {
int n;
cin >> n;
vector<vector<long long>> dp(n, vector<long long> (n));
vector<int> v(n);
for (int &x : v)
cin >> x;
for (int i=0; i<n; ++i)
dp[i][i] = v[i];
for (int l=n-1; l>=0; --l)
for (int r=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 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n; cin >> n;
vector<vector<ll>> dp(n, vector<ll> (n));
vector<ll> a(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+i < n; j++)
dp[j][i+j] = max(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 | #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
int a[n];
long long dp[n+1][n+1];
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
dp[i][i] = a[i];
}
for(int i = 1; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i + j >= n) continue;
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 | MAX_INT = 100
N = int(input())
a = list(map(int,input().split()))
dp = [[0]*(N) for i in range(N+1)]
if N%2 == 0:
hand = 0
else:
hand = 1
for i in range(N):
if hand == 0:
dp[i][i] = -a[i]
else:
dp[i][i] = a[i]
for i in range(1,N+1):
for j in range(N+1):
if i+j < N:
if (hand+i)%2 == 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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class Main {
static long dp[][][] = new long[3001][3001][2];
public static void main(String[] args) {
FastScanner in = 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 | def solve():
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[... | 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.*;
public class Main {
static final long MOD=1000000007;//998244353;
static double[][][] memo;
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out);
InputReader sc=new InputRead... | 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())
l=[int(x) for x in input().split()]
dp=[[0 for i in range(n+1)] for j in range(n+1)]
for i in range(n-1,-1,-1):
for j in range(0,n,1):
if i==j:
dp[i][j]=l[i]
else:
dp[i][j]=max(l[i]-dp[i+1][j],l[j]-dp[i][j-1])
#print(i,j)
#print(dp)
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;
const int N=3005;
ll dp[N][N];
int n,x[N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>x[i];
for(int i=1;i<=n;i++)dp[i][i]=x[i];
for(int i=n;i>=1;--i)
{
for(int j=i+1;j<=n;j++)
{
dp[i][j]=max(x[i]-dp[i+1][j],x[j]-dp[i][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 | #include <iostream>
using namespace std;
int N, a[3009];
long long dp[3009][3009];
int main() {
cin >> N;
for(int i = 0; i < N; i++)
cin >> a[i];
for(int bit = 0; bit < N; bit++) {
for(int l = 0; l + bit < N; l++) {
int r = l + bit;
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 | #include<bits/stdc++.h>
using namespace std;
int N;
long long f[3005][3005],a[3005];
int main()
{
scanf("%d",&N);
for(int i=1;i<=N;++i)
scanf("%lld",&a[i]);
for(int i=1;i<=N;++i)
{
int s=N-i+1;
for(int j=1;j<=s;++j)
{
if(s%2==1) f[j][j+i-1]=max(f[j+1][j+i-1]+a[j],f[j][j+i-2]+a[j+i-1]);
else f[j][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 | import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Queue;
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 | 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(1,n):
for j in range(n-i):
if (n-i)%2==1:
dp[j][i+j] = max(dp[j][i+j-1]+a[i+j],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.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int size = scanner.nextInt();
int arr[] = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = scanner.nextInt();
}
long ans[][... | 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=3000+100;
long long dp[MAXN][MAXN];
int 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;
dp[l][r]=max(a[l]-dp[l+1][r],a[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;
typedef long long ll;
ll dp[3000][3000];
ll a[3000];
ll dfs(ll l,ll r){
if(dp[l][r]>=0)return dp[l][r];
if(l==r)return dp[l][r]=a[l];
ll ret=max(-dfs(l+1,r)+a[l],-dfs(l,r-1)+a[r]);
return dp[l][r]=ret;
}
int main(){
ll n;cin>>n;
for(ll i=0;i<n;i++)for(ll j=0;j<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 sys
readline = sys.stdin.readline
N = int(readline())
A = list(map(int,readline().split()))
# dp[i][j] = 元の数列の中の連続する区間i~jに対して、最終局面に行った時のX-Yの最大値
dp = [[0] * (N + 1) for i in range(N + 1)]
for length in range(1, N + 1):
for i in range(N - length + 1):
j = i + length
if (N - length) % 2 == 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())
lista = list(map(int,input().split()))
dp = [[0] * n for i in range(n)]
for i in range(n):
dp[i][i] = lista[i]
for i in range(1,n):
for j in range(i-1,-1,-1):
dp[j][i] = max(lista[j] - dp[j+1][i], lista[i] - dp[j][i-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 | n = int(input())
a = list(map(int, input().split()))
dp = [[0]*(n+1) for i in range(n+1) ]
for cnt in range(1, n+1):
i = 0
while True:
j = i + cnt
if j-1 >= n:
break
dp[i][j] = max(a[i] - dp[i+1][j], a[j-1] - dp[i][j-1])
i += 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 | N = int(input())
a_list = list(map(int, input().split()))
dp = [[0]*(N+10) for _ in range(N+10)]
dp[0][0] = 0
for width in range(1, N+1):
for l in range(1, N+1):
r = l + width
if r >= N+2:
break
if width % 2 == N % 2:
dp[l][r] = max(dp[l][r-1]+a_list[r-2], dp[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 <iostream>
#define nmax 3005
using namespace std;
int n, v[nmax];
long long dp[nmax][nmax];
int main()
{
int i, j;
cin >> n;
for(i = 1; i <= n ;i++)
cin>>v[i];
for(i = 1; i <= n; i++)
dp[i][i] = v[i];
for(i = n; i >= 1; i--)
for(j = i + 1; j <= n; 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 | import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Main main = new Main();
main.solve();
}
public void solve() throws IOException {
// Scanner... | 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 long stoneGame(int[] piles) {
long[][] dp = new long[piles.length + 1][piles.length];
for (int gap = 0; gap < piles.length; gap++) {
for (int i = 0, j = i + gap; j < piles.length; j++, i++) {
if (gap == 0) {
dp[i][j] = piles[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 | from sys import stdin
n = int(stdin.readline())
arr = list(map(int,stdin.readline().split()))
dp = [[0]*(n+1) for _ in range(n+1)]
i=j=n-1
for i in range(n-1,-1,-1):
for j in range(1,n+1):
if i>j-1:
continue
if ((j-i)%2)^(n%2):
dp[i][j] = min(-arr[i]+dp[i+1][j],-arr[j-1]+dp[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.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = scanner.nextInt();
int[] arr = new int[count];
for (int i = 0; i < count; ++i) {
arr[i] = scanner.nextInt();
}
lo... | 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.LinkedList;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
FastScanner scanner=new FastScanner();
int n=scanner.nextInt();
int a[]=scanner.readArray(... | 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 n;
int a[3100];
long long dp[3100][3100];
bool vis[3100][3100];
long long solve (int s, int e) {
if (s == e) return a[s];
if (vis[s][e]) return dp[s][e];
vis[s][e] = true;
dp[s][e] = max(a[s]-solve(s+1, e), a[e]-solve(s, e-1));
return dp[s][e];
}
int main () {
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.util.*;
public class Main {
static int N;
static long[] a;
static long[][] dp;
static long INF = Long.MAX_VALUE;
static long dp(int left, int right) {
if (dp[left][right]!=INF) return dp[left][right];
int flag = 1;
if ((N-left-right)%2==0) flag*=-1;
if... | 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;
#define N 3010
long long dp[N][N];
int main ()
{
int n;
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> dp[i][i];
for (int j = i - 1; j > 0; j --) {
dp[i][j] = max(dp[i][i] - dp[i - 1][j], dp[j][j] - dp[i][j + 1]);
}
}
cou... | 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());r=range;S=[0]*-~N
for l in r(N):S=[max(S[i+1]+A[i],S[i]+A[i+l])if(l^N)&1else min(S[i+1]-A[i],S[i]-A[i+l])for i in r(N-l)]
print(S[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.OutputStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
impor... | 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().strip().split(' ')))
MAx=max(A)
dp=[[-3*max(A)]*N for i in range(N)]
for i in range(N):
dp[i][i]=A[i]
for length in range(2,len(A)+1):
for start in range(0,len(A)-length+1):
left=start
right=start+length-1
temp1=0
temp2=0
dp[left][ri... | 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.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
static final int INF = (int) (1e9 + 10);
static final int MOD = (int) (1e9 + 7);
// static final int N = (int) (4e5 + 5);
// static ArrayList<Integer>[] graph;
// static boolean visited... | 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
def input():
return sys.stdin.readline()[:-1]
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 -... | 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(x) for x in input().split()]
dp = [[0 for _ in range(n+1)] for _ in range(n+1)]
for i in range(1, n+1):
for l in range(n):
if l+i > n:
continue
else:
if i % 2 == n % 2:
dp[l][l+i] = max(a[l]+dp[l+1][l+i], a[l+i-1]+dp[l][l+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 | #include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxm=3e3+2;
ll dp[maxm][maxm];
ll arr[maxm];
ll cal(int start,int end)
{
if(start==end)return arr[start];
if(dp[start][end]!=-1)return dp[start][end];
return dp[start][end]=max(arr[start]-cal(start+1,end),arr[end]-cal(start,end-1));
}
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 | import sys
input = sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))
dp=[0]*3000
for i in range(n):
dp[i]=a[i]
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]) | 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 int
ll dp[3010][3030];
using namespace std;
int main()
{
ll n;
cin>>n;
ll a[n];
for(ll i=0;i<n;i++)
cin>>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... | 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;
int a[3000];
ll sum[3001];
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));
}
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 | import java.io.*;
import java.util.*;
public class Main{
final public static int MOD = (int)1e9+7;
public static long dp1[][];
public static int arr[];
public static long tot[];
public static void main(String[] args) throws IOException{
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWr... | 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, nums = int(input()), list(map(int, input().split()))
score = [[(-1 if N % 2 == 0 else 1)*x for x in nums]]
for i in range(1, N):
temp = []
for j in range(N - i):
func, sign = (min, -1) if i % 2 == N % 2 else (max, 1)
temp.append(func(sign*nums[j] + score[-1][j + 1], sign*nums[j + i] + score[... | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.