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 | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3005;
ll a[maxn], dp[maxn][maxn];
int main()
{
ll n;
cin >> n;
for(ll i=0;i<n;i++)cin >> a[i];
for(ll l=n-1;l>=0;l--){
for(ll r=l;r<n;r++){
if(r == l)dp[l][r]=a[l];
else dp[l][r] = max(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 | #include<iostream>
#define inf 1e18
using namespace std;
long long dp[3005][3005];
long long a[3005];
long long int rec(int i,int j){
if(i>j)return 0;
if(dp[i][j]!=-inf)return dp[i][j];
return dp[i][j]=max(a[i]-rec(i+1,j),a[j]-rec(i,j-1));
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
for(int i=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.util.Scanner;
public class Main {
public static void main(String[] args) {
Main main = new Main();
main.solve();
}
public void solve() {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
long[] as = new long[N+1];
for(int i=1; i<=N; ++i) {
... | JAVA |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
int a[3100];
long long dp[3100][3100] = {0};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int d = 1; d <= n; ++d) {
for (int i = 0; i + d <= n; ++i) {
dp[i][i + d] = max(a[i] - ... | CPP |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | N = int(input())
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+d
dp[l][r] = max(A[l]-dp[l+1][r], A[r-1]-dp[l][r-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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.InputStream;
import java.io... | 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][(n-1)-i] = a[i]
for i in range(n-2, -1, -1):
for j in range(i+1):
dp[j][i-j] = max( a[j] - dp[j+1][i-j],
a[-(1+i-j)] - dp[j][i-j+1])
print(dp[0][0]) | PYTHON3 |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import java.io.*;
import java.util.*;
import java.lang.*;
public class Main
{
static long[] A;
static long[][] dp;
static long fn(int i, int j, int c) {
if (i > j) { return 0; }
if (dp[i][j] != -1) { return dp[i][j]; }
if (c == 0) {
dp[i][j] = Math.max(A[i] + 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 | 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 + 1 - l):
j = i + l
if (N - l) % 2 == 0:
dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j - 1])
else:
dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - ... | PYTHON3 |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import java.util.Arrays;
import java.util.Scanner;
public class Main {
static long rec(int i, int j, int n, int[] a, long[][] dp) {
if (dp[i][j] != Long.MAX_VALUE) {
return dp[i][j];
}
int turn = n - (j - i);
long d;
if (turn % 2 == 0) {
d = Math.ma... | JAVA |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import 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 = [int(i) for i in 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+1-l):
j = i+l
if (N-l) % 2 == 0:
dp[i][j] = max(dp[i+1][j]+A[i], dp[i][j-1]+A[j-1])
else:
dp[i][j] = min(dp[i+1][j]-A[... | 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 | # @author
import sys
class LDeque:
def solve(self):
n = int(input())
a = [int(_) for _ in input().split()]
dp = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
for i in range(n - 1, -1, -1):
dp[i][i] = a[i]
for j in range(i + 1, n):
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 = [int(i) for i in input().split()]
dp = [[0] * (n+1) for _ in range(n+1)]
for i in range(n):
dp[i][i] = 0
for j in range(1, n + 1):
for i in range(n+1):
if i + j <= n:
if j % 2 == n % 2:
dp[i][i+j] = max(dp[i][i+j-1]+a[i+j-1], a[i]+dp[i+1][i+j])
... | PYTHON3 |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 |
import java.io.*;
import java.util.*;
public class Main {
static BufferedReader br;
static int cin() throws Exception
{
return Integer.valueOf(br.readLine());
}
static int[] split() throws Exception
{
String[] cmd=br.readLine().split(" ");
int[] ans=new int[cmd.length];
for(int i=0;i<cmd.length;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>
#define ll long long
using namespace std;
const int MAXN = 3007;
ll dp[MAXN][MAXN], a[MAXN];
int main()
{
int n; scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), dp[i][1] = a[i];
for (int j = 2; j <= n; j++)
for (int i = 1; i + j - 1 <= 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>
#define ll long long
using namespace std;
const int NM=1e5+5;
ll dp[3005][3005];
int main()
{ int n;
cin>>n;
for(int i=1; i<=n; i++)
cin>>dp[i][i];
for(int k=1; k<n; k++)
for(int i=1,j; i+k<=n; i++)
{ j=i+k;
dp[i][j]=max(dp[i][i]-dp[i+1][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.util.Scanner;
/**
* Deque
*/
public class Main {
public static void main(String[] args) throws Exception {
try (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.nex... | 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())
lst = list(map(int, input().split()))
ans = 0
dp = [[0] * (n+1) for _ in range(n+1)] #dp[i]は石の数が残りi個のときに手番プレイヤーが勝利できるか
for i in range(n+1):
for j in range(n - i+1):
l = j
r = j+i
if l-r== 0:
dp[l][r] == 0
elif (n - (l - r-1)) % 2 ==1:
dp[l... | PYTHON3 |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | /**
* @author derrick20
*/
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) throws Exception {
FastScanner sc = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int N = sc.nextInt();
long[] points = new lon... | 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 = [[float('inf')]*N for _ in range(N)]
for i in range(N):
if (N-1)%2==0:
dp[i][i] = A[i]
else:
dp[i][i] = -A[i]
for k in range(1,N):
for i in range(N):
j = i+k
if j>N-1:
break
if (N-(k+1))%2==0:
dp[i][j] = max(dp[i][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 <bits/stdc++.h>
using namespace std;
long long dp[3333][3333];
int main(){
int N;
cin >> N;
vector<long long> a(N);
for(auto &i:a)cin >> i;
for(int w=1;w<=N;w++){
for(int l=0;l<=N-w;l++){
int r=l+w-1;
dp[l][r]=max(a[l]-dp[l+1][r],a[r]-dp[l][r-1]);
}
}
cout << dp[0][N-1] << endl;
return ... | 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 for _ in range(n)] for _ in range(n)]
m = n % 2
if m == 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 x in range(n-i):
y = x + i
if i % 2 == m:
... | 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 d in range(1, n+1):
for i in range(n+1-d):
j = i+d
if (n-d)%2 == 0:
dp[i][j] = max(dp[i+1][j]+A[i], dp[i][j-1]+A[j-1])
else:
dp[i][j] = min(dp[i+1][j]-A[i], dp[i][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<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll dp[3001][3001];
int main()
{
ll n,i,j;
cin >> n;
ll a[n];
deque<ll>dq;
for(i=0;i<n;i++)
{
cin >> a[i];
dq.push_back(a[i]);
}
ll start,end;
for(start=n-1;start>=0;start--)
{
for(end=start;end<n;end++)
dp[start][end]=max(a[start]... | 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 = [[0] * (N + 1) for i in range(N + 1)]
# [i,j)
for l in range(1, N + 1):
for i in range(N - l + 1):
j = i + l
if (N - l) % 2 == 0: # 先手
dp[i][j] = max(dp[i + 1][j] + A[i], dp[i][j - 1] + A[j - ... | 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 |
// Problem : L - Deque
// Contest : Educational DP Contest
// URL : https://atcoder.jp/contests/dp/tasks/dp_l
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] ... | 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 N = 3589;
ll a[N], dp[N][N];
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i=1; i<=n; i++) {
cin >> a[i];
}
for (int len=1; len<=n; len++) {
for (int i=1, j=i+len-1; j<=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;
typedef long long ll;
ll a[3000];
ll dp[3000][3000];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int l = 1; l <= n; l++) {
for (int i = 0; i+l <= n; i++) {
dp[l][i] = max(a[i]-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 | n = int(input())
l = list(map(int, input().split()))
tl = sum(l)
dp=[]
for _ in range(n):
dp.append([0]*n)
for i in range(n):
dp[i][i]=l[i]
for i in range(n-1):
dp[i][i+1]=max(l[i],l[i+1])
for i in range(n-1,-1,-1):
for j in range(i+2,n):
ans1=l[i]+min(dp[i+2][j],dp[i+1][j-1])
ans2=l[j]+... | PYTHON3 |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import java.util.*;
import java.io.*;
public class Main {
public static long dp[][][];
public static long solve(long arr[] , int i , int j , int turn){
if(i > j){
return 0;
}
if(dp[i][j][turn] != -1){
return dp[i][j][turn];
}
if(turn == 1){
... | JAVA |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import java.io.*;
import java.util.*;
public class Main{
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
tr... | 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 | #!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**9)
#import
#import math
#import numpy as np
N = int(input())
A = list(map(int, input().split()))
dp = [[0] * (N + 1) for _ in range(N + 1)]
for w in range(1, N + 1):
for l in range(N - w + 1):
r = w + l
dp[l][r] = A[l] - 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 | import java.io.*;
import java.util.*;
public class Main{
static HashSet<Integer> set;
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
// Start writing your solution here. ---------------------------... | JAVA |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import java.util.Arrays;
import java.util.Scanner;
public class Main{
static int N;
static long[] a;
static long[][] dp;
static long f(int i, int j) {
if(i == j) return ((N + i - j - 1) % 2 == 0) ? a[i] : -a[i];
if(dp[i][j] != Long.MIN_VALUE) return dp[i][j];
if((N + i - j - 1) % 2 == 0) {
return dp[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 | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
// Your code here!
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int N = in.nextInt();
long[] a = new long[N];
long... | 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 width in range(1, N+1):
for i in range(N-width+1):
j = i + width
if (N - width) % 2 == 0:
dp[i][j] = max(dp[i+1][j] + A[i], dp[i][j-1] + A[j-1])
else:
dp[i][j] = min(dp[i... | 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 i in range(n)[::-1]:
for j in range(n + 1):
if i >= j:
continue
minus = (n - i + j) % 2 * (-2) + 1
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 | n = int(input())
INF = 987456325585
arr = [int(x) for x in input().split()]
# dp[i][j] -> array is left with elements from index i to j, contains max of x-y if player x starts from here
# dp[0][n-1] -> res
dp = []
for i in range(n):
dp.append([-INF]*n)
for i in range(n-1,-1,-1):
for j in range(i,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(map(int, input().split()))
DP = [[0] * (N+1) for _ in range(N+1)]
for num in range(1, N + 1):
for start in range(N):
end = start + num
if end > N:
break
if num == 1:
DP[start][end] = A[start]
else:
DP[start][end] = 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 | n=int(input())
arr=list(map(int,input().split()))
dp=[[0]*n for _ in range(n)]
for i in range(n):
if n%2==0:
dp[i][i]=-arr[i]
else:
dp[i][i]=arr[i]
for i in range(1,n):
for j in range(n-i):
if (n-i)%2==0:
dp[j][j+i]=min(dp[j][j+i-1]-arr[j+i],dp[j+1][j+i]-arr[j])
else:
dp[j][j+i]=max(dp... | PYTHON3 |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | n=int(input())
a=list(map(int,input().split()))
dp=[[float("inf")]*(n+1) for _ in range(n)]
z=0
for l in range(n):
dp[l][l]=0
for k in range(1,n+1):
for i in range(n-k+1):
j=i+k
if 0<=i<=n-2 and 1<=j<=n:
if (n-(i+j))%2==0: #先手
dp[i][j]=max(dp[i+1][j]+a[i],dp[i][j-1]+a... | PYTHON3 |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import sys,queue,math,copy,itertools,bisect,collections,heapq
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
N = NI()
a = LI()
dp = [[0] * (N+1) for _ in range(N+1)]
for i in range(N-1,-1,-1):
for j in range(i+1):
dp[i][j] = max(a[i-j]-dp[i+1][j],a[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 | import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main {
FastScanner in;
PrintWriter out;
boolean systemIO = true;
public long pow(long x, long p, long mod) {
if (p <= 0) {
return 1;
}
long th = pow(x, p / 2, mod);
th *= th;
th %= mod;
if (p % 2 == 1) {
th *= x;
... | JAVA |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import java.util.*;
import java.io.*;
class Main{
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
thi... | JAVA |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import java.util.*;
import java.io.*;
class Main{
static int n;
static long[] a;
static long[][] dp;
static long inf = -1000000000000000L;
public static void main(String[] args){
FastScanner fs = new FastScanner();
n = fs.nextInt();
a = new long[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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] A = new 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 | def deque(a):
n = len(a)
cur = [(x, 0) for x in a]
nxt = [(0, 0)] * n
for l in range(2, n + 1):
for i in range(n - l + 1):
j = i + l - 1
x, y = cur[i]
z, t = cur[i + 1]
if a[i] + t - z > a[j] + y - x:
nxt[i] = (a[i] + t, z)
... | 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 = [[-1]*(N+1-i) for i in range(N+1)]
for i in range(N+1):
dp[0][i] = 0
t = (N & 1)
for l in range(1, N+1):
S = dp[l-1]; T = dp[l]
t ^= 1
if t:
T[:] = (min(S[i+1] - A[i], S[i] - A[i+l-1]) for i in range(N-l+1))
else:
T[:] = (max(S[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()))
ans=[[0 for e in range(N+1)] for f in range(N+1)]
for j in range(1,N+1):
ans[j][1]=a[j-1]
if N>1:
for i in range(2,N+1):
for j in range(1,N+2-i):
ans[j][i]=max(a[j-1]-ans[j+1][i-1],a[j+i-2]-ans[j][i-1])
print(ans[1][N])
| PYTHON3 |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
/*
* L - Deque
*/
public class Main
{
long[][] cache;
public static void main( String[] args ) throws Exception
{
new Main().run();
}
public void run() throws Exception
{
final Scanner sc = new Scanner( g... | 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 | /**
* Created at 10:39 on 2019-06-23
*/
import java.io.*;
import java.util.*;
public class Main {
static FastScanner sc = new FastScanner();
static int[] dx = {0, 1, 0, -1};
static int[] dy = {-1, 0, 1, 0};
static long MOD = (long) (1e9 + 7);
public static void main(String[] args) {
int N = sc.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 | N = int(input())
A = list(map(int,input().split()))
dp = [[0] * (N+1) for _ in range(N+1)]
#半開区間で考える⇒[i,j)での最適解
for w in range(1,N+1):#区間の残り数
for l in range(N):# 左端
r = l + w#右端
if r > N:
continue
elif (N-w) % 2 == 0:#太郎
dp[l][r] = max(dp[l+1][r] + A[l], dp[l][r-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.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;
const int nax=3005;
long long int dp[nax][nax];
int main(){
int n;
scanf("%d", &n);
vector<int> a(n);
for(int &x:a)
scanf("%d", &x);
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... | CPP |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | n,*a=map(int,open(0).read().split())
dp=[[0]*(n+1)for _ in range(n+1)]
for k in range(1,n+1):
for i in range(n-k+1):
j=i+k
dp[i][j]=min(dp[i+1][j]-a[i],dp[i][j-1]-a[j-1])if(n-k)%2else max(dp[i+1][j]+a[i],dp[i][j-1]+a[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.util.*;
import java.io.*;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int N = in.nextInt();
long[] a = new long[N];
for (int i = 0; i < N; i++) {
a[i] = in.nextInt();
}
... | JAVA |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
... | 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;
typedef long double ld;
ll a[3001][3001]={0};
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[0][i];
}
for(int i=1;i<n;i++){
for(int j=0;j<n;j++){
if( (i+j) >= n){continue;}
a[i][j]=max... | 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 = [[None]*(N+1) for _ in range(N+1)]
for i in range(N+1):
dp[i][i]=0
for len in range(1,N+1):
for i in range(N-len+1):
j=i+len
#print(i,j)
if (N-len)%2==0:
dp[i][j] = max(dp[i][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 | import java.util.*;
import java.io.*;
public class Main {
private static void solve() {
int n = nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
long[] sum = new long[n];
sum[0] = a[0];
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] +... | JAVA |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import java.util.*;
import java.io.*;
public class Main {
static long MOD=(long) (1e9+7);
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for(int i =0; i< n; i++)
arr[i] = in.nextInt();
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.util.Arrays;
import java.util.Scanner;
public class Main {
static int count;
static long[][] dp;
static long[] numberArray;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
count = scan.nextInt();
scan.nextLine();
numberArray = Arrays.stream(scan.nextLine().s... | JAVA |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ll n; cin >> n;
vector<ll> a(n); for(auto &b : a) cin >> b;
vector<vector<ll> > dp(n, vector<ll>(n));
for(ll i = 0; i < n; i++) dp[i][i] = a[i];
for(ll len = 2; len <= n; len++) for(ll lb = 0; lb < n; lb++){
ll rb = lb + len - 1;
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 | #include <iostream>
#define NMAX 3000
using namespace std;
int n;
long long v[NMAX+10], dp[NMAX+10][NMAX+10];
int main()
{
cin >> n;
for(int i=1; i<=n; i++) cin >> v[i];
for(int len=0; len<n; len++)
for(int st=1; st<=n-len+1; st++)
{ int dr = st + len;
dp[st][dr] = m... | 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.math.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] a = new int[N];
for (int i = 0; i < N; i++)
a[i] = sc.nextInt();
sc.close();
long[][] dp = new long[N][N];
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 | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static FastScanner sc = new FastScanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void ok() throws Exception{
int n=sc.nextInt();
long arr[] =new long[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;
const int N = 3e3 + 3;
int n,a[N];
long long dp[N][N];
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int l = n; l >= 1; l--){
for(int r = l; r <= n; r++){
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;
const int xp=1e9+7;
long long d[2][3002][3002],n,v[3002];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>v[i];
for(int i=n;i>=1;i--)
for(int j=i;j<=n;j++)
{
d[1][i][j]=min(d[0][i+1][j]-v[i],d[0][i][j-1]-v[j]);
d[0][i][j]=max(d[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, = map(int, input().split())
dp = [[0] * 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 | #https://ikatakos.com/pot/programming_algorithm/contest_history/atcoder/2019/0106_educational_dp#%E8%A7%A3%E6%B3%953
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 = a[l] - dp[l + 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.*;
import java.util.*;
public class Main implements Runnable {
FastReader scn;
PrintWriter out;
String INPUT = "";
void solve() {
int n = scn.nextInt();
int[] arr = scn.nextIntArray(n);
dp = new long[n][n];
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
dp[i][j] = val;
... | 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):
DP[i][i+l] = max(A[i]-DP[i+1][i+l], A[i+l-1]-DP[i][i+l-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 sys
n=int(sys.stdin.readline())
a=list(map(int,sys.stdin.readline().split()))
dp=[[0 for i in range(n)] for j in range(n)]
for i in range(n):
dp[i][i]=a[i]
for i in range(2,n+1):
for j in range(n-i+1):
k=j+i-1
dp[j][k]=max(a[k]-dp[j][k-1],a[j]-dp[j+1][k])
print(dp[0][len(a)-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;
typedef long long ll;
int main(){
ll n;cin >> n;
vector<ll> v(n);
vector<vector<ll>> dp(n+1,vector<ll>(n+1));
for(ll i=0;i<n;i++) cin >> v[i];
for(int i=n-1;i>=0;i--){
for(int j = i;j<n;j++){
if(i == j) dp[i][j] = v[i];
else{
dp[i][j] = 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 | #!/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()))
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+d
dp[l][r] = \
max(
... | 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;
using ll=long long;
const int maxn=3001;
int n;ll arr[maxn];
ll dp[maxn][maxn];
int main()
{
cin>>n;
for(int i=0;i<n;i++)cin>>arr[i];
for(int k=0;k<n;k++)
{
int i=0,j=k;
while(j<n)
{
if(i==j)dp[i][j]=arr[i];
else dp[i][j]=max(arr[i]-dp[i+1][j],arr[j]-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 | n = int(input())
a = list(map(int, input().split( )))
dp = [[0]*n for i in range(n)]
for i in range(n):#奇数偶数で最終手番は変わる
dp[i][i] = a[i]
for d in range(1,n):
for j in range(n-d):
if d%2 == 1:#最後打たない方
dp[j][j+d] = min(dp[j][j+d-1]-a[j+d],dp[j+1][j+d]-a[j])
else:#最後打つ方
dp[j... | PYTHON3 |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | import java.io.*;
import java.util.*;
public class Main {
final boolean isDebug = false;
String fileName = "input.txt";
FastScanner sc;
//PrintWriter out;
final int MOD = (int)1e9+7;
final int INF = Integer.MAX_VALUE / 2;
int N;
int[] a;
long[][] dp;
/* solve */
void solve(){
N = sc.nextInt();
a =... | 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, -1, -1):
for j in range(i, N):
dp[i][j] = max(a[i]-dp[i+1][j],a[j]-dp[i][j-1])
print(dp[0][N-1]) | PYTHON3 |
p03171 Educational DP Contest - Deque | Taro and Jiro will play the following game against each other.
Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:
* Remove the element at the beginning or the end of a. The player earns x points, whe... | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
#define lli long long int
int main(){
lli n;
cin>>n;
vector<lli> a(n);
for(lli i=0;i<n;i++){
cin>>a[i];
}
vector<vector<lli> > dp(n,vector<lli>(n));
for(lli i=0;i<n;i++){
dp[i][i]=a[i];
}
for(lli l=2;l<=n;l++){
for(lli i=0;i<n-l+1;i++){
lli j=i+l-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 java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* Built using CHelper plug-in Actual solution is at the top
*/
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 | 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(1, N):
for j in range(i-1, -1, -1):
dp[i][j] = max(A[i]-dp[i-1][j], A[j]-dp[i][j+1])
print(dp[N-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 | #include <bits/stdc++.h>
#define ll long long
using namespace std;
long long sum ;
int n;
long long dp[3005][3005],a[3005];
int main(){
cin>>n;
for(int i=1;n>=i;i++)
{
cin>>a[i];
sum+=a[i];
}
for(int i=1;n>=i;i++)
dp[i][i]=a[i];
for(int i=2;n>=i;i++)
{
for(int j=1;j+i-1<=n;j++)
{
dp[j][j+i-1]=m... | 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 N =3e3+20;
ll a[N],f[N][N];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) f[i][i]=a[i];
for(int i=2;i<=n;i++)
{
for(int j=1;j+i-1<=n;j++)
{
f[j][j+i-1]=max(a[j]-f[j+1][j+i-1],a[j+i-1]-f[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.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
*
* @author vdewansa
*/
public class Main {
public static void main(String[] args) {
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 | /*input
6
4 2 9 7 1 5
*/
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.... | 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 Max = 3000;
long dp[Max][Max];
int main(){
int n;
scanf("%d", &n);
vector<int>a(n);
for(int i=0; i<n; i++){
scanf("%d", &a[i]);
}
for(int l=n-1; l>=0; l--){
for(int r=l; r<n; r++){
dp[l][r] = max(a[l]-(l+1<=r ? 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
readline = sys.stdin.readline
N = int(readline())
A = list(map(int,readline().split()))
# dp[l][r] = [l:r)の区間から起こり得るX - Yの最大値
# [N - 区間の長さ] = 偶数のとき、次に取るのは先手
dp = [[0] * (N + 1) for i in range(N + 1)]
for k in range(1, N + 1): # 区間の長さ
for i in range(N + 1 - k):
if (N - k) % 2 == 0: # 先手番
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 | #include <bits/stdc++.h>
using namespace std;
const int MM=3013;
long long dp[MM][MM];
long long s=0;
int main()
{
ios_base::sync_with_stdio(0);
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>dp[i][i];
for(int i=1;i<=n;i++)
{
s+=dp[i][i];
for(int j=1;j+i<=n;j++)
{
... | CPP |
p03317 AtCoder Beginner Contest 101 - Minimization | There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.
On this sequence, Snuke can perform the following operation:
* Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.... | 6 | 0 | import math
n, k = map(int, input().split())
print(math.ceil((n-k) / (k - 1) + 1))
| PYTHON3 |
p03317 AtCoder Beginner Contest 101 - Minimization | There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.
On this sequence, Snuke can perform the following operation:
* Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.... | 6 | 0 | n, k = [int(x) for x in input().split()]
print((n - 2) // (k - 1) + 1)
| PYTHON3 |
p03317 AtCoder Beginner Contest 101 - Minimization | There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.
On this sequence, Snuke can perform the following operation:
* Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.... | 6 | 0 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
try {
// 標準入力
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
... | JAVA |
p03317 AtCoder Beginner Contest 101 - Minimization | There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.
On this sequence, Snuke can perform the following operation:
* Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.... | 6 | 0 | n,k=map(int,input().split());print((n+k-3)//~-k) | PYTHON3 |
p03317 AtCoder Beginner Contest 101 - Minimization | There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.
On this sequence, Snuke can perform the following operation:
* Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.... | 6 | 0 | #include<iostream>
#include<cmath>
using namespace std;
int main()
{
double n,k;
cin >> n >> k;
cout << (int)ceil((n-1)/(k-1)) << endl;
return 0;
} | CPP |
p03317 AtCoder Beginner Contest 101 - Minimization | There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.
On this sequence, Snuke can perform the following operation:
* Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.... | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k; cin >> n >> k;
cout << 1 + int(ceil((n - k + 0.0) / (k - 1))) << "\n";
} | CPP |
p03317 AtCoder Beginner Contest 101 - Minimization | There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.
On this sequence, Snuke can perform the following operation:
* Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.... | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int main(){
float N, K;
cin >> N >> K;
cout << ceil((N - K)/(K - 1)) + 1 << endl;
} | CPP |
p03317 AtCoder Beginner Contest 101 - Minimization | There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.
On this sequence, Snuke can perform the following operation:
* Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.... | 6 | 0 | n, k = map(int, input().split());print(1 + (n - k) // (k - 1) + ((n - k) % (k - 1) != 0))
| PYTHON3 |
p03317 AtCoder Beginner Contest 101 - Minimization | There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.
On this sequence, Snuke can perform the following operation:
* Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.... | 6 | 0 | #include <iostream>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
cout << (N-2) / (K-1) + 1 << endl;
return 0;
}
| CPP |
p03317 AtCoder Beginner Contest 101 - Minimization | There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.
On this sequence, Snuke can perform the following operation:
* Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.... | 6 | 0 | #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
int main(){
int n,k; cin>>n>>k;
cout<<1+(n-2)/(k-1)<<endl;
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.