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
p03030 AtCoder Beginner Contest 128 - Guidebook
You have decided to write a book introducing good restaurants. There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i. No two restaurants have the same score. You want to in...
6
0
#include <bits/stdc++.h> using namespace std; int main(){ int N, P; string S; cin >> N; vector<tuple<string, int, int>> vec; for(int i=0;i<N;i++){ cin >> S >> P; vec.push_back(make_tuple(S,-P,i+1)); } sort(vec.begin(), vec.end()); for(int i=0;i<N;i++){ cout << get<2>(vec[i]) << endl; }...
CPP
p03030 AtCoder Beginner Contest 128 - Guidebook
You have decided to write a book introducing good restaurants. There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i. No two restaurants have the same score. You want to in...
6
0
#include <bits/stdc++.h> using namespace std; int main(){ int i; int N;cin>>N; vector<tuple<string,int,int>> rest(N); for(i=0;i<N;i++){cin>>get<0>(rest[i])>>get<1>(rest[i]);get<1>(rest[i])*=-1;get<2>(rest[i])=i+1;} sort(rest.begin(),rest.end()); for(i=0;i<N;i++)cout<<get<2>(rest[i])<<endl; }
CPP
p03030 AtCoder Beginner Contest 128 - Guidebook
You have decided to write a book introducing good restaurants. There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i. No two restaurants have the same score. You want to in...
6
0
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int(i)=0;i<(n);i++) int main(){ int n;cin>>n; pair<pair<string,int>,int> p[100]; REP(i,n){ string store; int score; cin>>store>>score; p[i]=make_pair(make_pair(store,-score),i+1); } sort(p,p+n); REP(i,n) cout<<p[i].second<<endl;...
CPP
p03030 AtCoder Beginner Contest 128 - Guidebook
You have decided to write a book introducing good restaurants. There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i. No two restaurants have the same score. You want to in...
6
0
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String[] s = new String[n]; for(int i = 0; i < n; i++){ String city = sc.next(); int point = sc.nextInt(); int num = i + 1; s[i] = city +...
JAVA
p03030 AtCoder Beginner Contest 128 - Guidebook
You have decided to write a book introducing good restaurants. There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i. No two restaurants have the same score. You want to in...
6
0
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; string s; int p; vector<tuple<string,int,int>> r; for(int i=0; i<n; i++){ cin>>s>>p; r.emplace_back(s,100-p,i); } sort(r.begin(),r.end()); for(int i=0; i<n; i++){ cout<<get<2>(r[i])+1...
CPP
p03030 AtCoder Beginner Contest 128 - Guidebook
You have decided to write a book introducing good restaurants. There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i. No two restaurants have the same score. You want to in...
6
0
#include <bits/stdc++.h> using namespace std; //解き方をすっかり忘れていました int main(){ int n; cin>>n; vector<tuple<string,int,int>>a; for(int i=0;i<n;i++){ string s; int b; cin>>s>>b; a.emplace_back( s, -1*b, i+1 ); } sort(a.begin(),a.end()); for(int i=0;i<n;i++){ cout<<get<2>(a[i])<<endl; }...
CPP
p03030 AtCoder Beginner Contest 128 - Guidebook
You have decided to write a book introducing good restaurants. There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i. No two restaurants have the same score. You want to in...
6
0
n = int(input()) a = (input().split() for _ in range(n)) for x in sorted((s, -int(p), i) for i, (s, p) in enumerate(a, 1)): print(x[2])
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(raw_input()) A=[int(x) for x in raw_input().split()] B=[[0 for x in range(N+1)] for y in range(N)] for i in range(N): B[i][i+1]=A[i] for l in range(1,N): for i in range( N-l): B[i][i+l+1]=max(A[i]-B[i+1][i+l+1], A[i+l] - B[i][i+l] ) print B[0][N] #for ll in B: # print ll
PYTHON
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following 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.buffer.readline def main(): N = int(input()) a = list(map(int,input().split())) dp = [[-1 for _ in range(N)] for _ in range(N)] for i in range(N): dp[i][i] = a[i] for i in range(N-1,-1,-1): for j in range(i,N): if 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
N=int(input()) A=list(map(int,input().split())) dpx=[[0 for i in range(0,N)] for i in range(0,N+1)] dpy=[[0 for i in range(0,N)] for i in range(0,N+1)] for i in range(1,N+1): for j in range(0,N): if N>j+i-1 and i!=1: dpx[i][j]=max(dpy[i-1][j+1]+A[j],dpy[i-1][j]+A[j+i-1]) dpy[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_list = list(map(int, input().split())) dp = [[0] * (N + 1) for _ in range(N + 1)] for i in range(1, N + 1): for l in range(N + 1 - i): r = l + i dp[l][r] = max(a_list[l] - dp[l+1][r], a_list[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
n = int(input()) a = list(map(int, input().split())) dp = [[0] * n for _ in range(n)] # dp[i][j] stores max value of (x-y) in segment from index i to j 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])...
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 N=3005; ll dp[N][N]; int main(){ int n; cin>>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][...
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) break; dp[j][...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std; #define pb push_back #define in long long #define inf INT_MAX int main() { in n; cin>>n; in a[n]; for(in i=0;i<n;i++) {cin>>a[i];} in dp[n][n]; for(in i=n-1;i>=0;i--) { for(in j=i;j<n;j++) { if(i==j) dp[i][j]=a[i]; else { dp...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std ; using ll = long long ; int main(){ int n,k ; cin >> n ; ll dp[n][n]; ll a[n] ; for(auto 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[i][j-1])...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std; #define int long long int max(int a,int b){ return (a>b?a:b); } const int N = 3005; int dp[N][N]; int n, arr[N]; int32_t main(){ cin >> n; for(int i=1;i<=n;i++){ cin >> arr[i]; } for(int i = n;i>0;i--){ for(int j=i;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 javax.swing.*; import java.io.*; import java.sql.Struct; import java.text.DecimalFormat; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(rd...
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 { public static void main(String args[]) { int n = 0; int[] arr = null; try (Scanner scanner = new Scanner(System.in)) { n = scanner.nextInt(); arr = new int[n]; for(int i = 0; i < n ;i++) { ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
N = int(input()) As = list(map(int, input().split())) d = [0]*(N**2) for i in range(N): for j in range(N-i): span = i*N+j if i == 0: d[span] = As[j] else: d[span] = max(As[j]-d[span-N+1], As[j+i]-d[span-N]) print(d[span])
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.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(new BufferedReader(new InputStreamReade...
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-t+1) for t in range(1, n+1)] dp[0] = a for t in range(1, n): for i in range(n-t): dp[t][i] = max(-dp[t-1][i+1] + a[i], -dp[t-1][i] + a[i+t]) 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
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; int n, a[3010]; long long dp[3010][3010]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = n - 1; i >= 0; i--) { for (int j = i + 1; j <= n; j++) { dp[i][j] = max(a[i] - dp[i + 1][j], a[j - 1] - dp[i][j - 1]); } } cout << dp[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
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
#include <algorithm> #include <iostream> using namespace std; int n, a[100]; long long dp[3100][3100]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> dp[i][1]; } for (int len = 2; len <= n; len++) { for (int i = 0; i+len <= n; i++) { dp[i][len] = max(dp[i][1]-dp[i+1][len-1], 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
import sys input = sys.stdin.readline def func(): N = int(input()) A = [int(i) for i in input().split()] dp = [[0] * (N+1) for i in range(N+1)] for i in range(N): dp[i][i+1] = A[i] for s in range(2, N+1): for l in range(N-s+1): r = l + s dp[l][r] = max(A[l]...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { static PrintWriter out; static StringBuilder sb; static final double EPS = 1e-9; static long mod = (int) 1e9 + 7; static int inf = (int) 1e9 + 2; static long[] fac; static int[] si; static ArrayList<Integer> primes; static...
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 = 3087; int a[N]; long long dp[N][N]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; dp[i][i] = a[i]; } for (int w = 2; w <= n; ++w) for (int l = 0, r; (r = l + w - 1) < n; ++l) d...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; int n, v[3002]; long long dp[3002][3002]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for(int i = 1; i <= n; ++i) cin >> v[i]; for(int i = n; i >= 1; --i) for(int j = i+1; j <= n+1; ++...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual soluti...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import sys input = sys.stdin.readline N = int(input()) lst_a = list(map(int, input().split())) dp = [[0 for _ in range(N+1)] for _ in range(N+1)] 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] + lst_a[i], dp[i][j-1] + lst_a[j-1]) else : ...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Map; import java.util.Objects; public class Main { static long[][] taro; static long[][] jiro; public static void main(String[] args) { InputReader ir = new ...
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
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package here; /** * * @author sokumar */ import java.util.*; import java.io.*; public class Main { public static void mai...
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 { public static final long mod = (long)1e9+7; public static final long INF = Long.MAX_VALUE/10; public static final int inf = Integer.MAX_VALUE/10; public static boolean[][] use1, use2; public static long[][] dp1, dp2; public static int[]...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
N = int(input()) A = list(map(int,input().split())) dp = [[0]*3100 for _ in range(3100)] for length in range(1,N+1): for i in range(N-length+1): j = i + length # 先手番 if (N-length)%2 == 0: dp[i][j] = max(dp[i+1][j] + A[i], dp[i][j-1] + A[j-1]) # 後手番 else: ...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; for (int i=0; i<n; i++) { a[i] = sc.nextInt(); } long[][] dp = new long[n][n]; for...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
N=int(input()) A=list(map(int,input().split())) d=[[0]*(N+1) for i in range(N+1)] for w in range(1,N+1): if w%2==N%2: for l in range(N+1-w): d[l][l+w]=max(d[l][l+w-1]+A[l+w-1],d[l+1][l+w]+A[l]) else: for l in range(N+1-w): d[l][l+w]=min(d[l][l+w-1]-A[l+w-1],d[l+1][l+w]-A[...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long[] a = new long[N + 1]; for (int i = 1; i <= N; i++) { a[i] = a[i - 1] + (long) sc.nextInt(); } ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import 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
n = int(input()) a = list(map(int,input().split())) dp = [[0]*n for _ in range(n)] # dp[i][j] : 残っている石がi からj の時の手番から見た終了得点の最大値 for i in range(n): dp[i][i] = a[i] for j in range(1,n): for i in range(n-j): dp[i][i+j] = max(a[i]-dp[i+1][i+j], a[i+j]-dp[i][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; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int N; cin>>N; vector<long long>a(N); vector<vector<long long> >dp(N,vector<long long>(N)); for(int i=0;i<N;i++) cin>>a[i]; for(int i=0;i<N;i++) dp[i][i]=a[i]; for(int j=1;j<N;j++) for(int i=0;i+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; const int N=100005; const long long INF=0x3f3f3f3f3f3f3f3f; int n,k; long long a[3005],dp[3005][3005]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) dp[i][i]=a[i]; for(int len=2;len<=n;len++) { for(int i=1;i<=n-len+1;i++...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) a = list(map(int,input().split())) dp = [[0 for _ in range(n)] for j in range(n)] for i in range(n): dp[i][i] = a[i] for dist in range(1,n): for i in range(n-dist): j = i + dist 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 = [int(x) for x in input().split()] dp = [[[] for _ in range(N)] for _ in range(N)] for i in range(N): dp[i][i] = a[i] for l in range(1, N): for i in range(N-l): dp[i][i+l] = max(a[i] - dp[i+1][i+l], a[i+l] - dp[i][i+l-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 = tuple(map(int, input().split())) dp = [[0] * (N + 1) for _ in range(N + 1)] for length in range(1, N + 1): for l in range(N - length + 1): r = length + l if (N - length) % 2: dp[l][r] = min(dp[l + 1][r] - A[l], dp[l][r - 1] - A[r - 1]) else: 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
//package Dynamic_Programming; import java.util.*; import java.lang.*; import java.math.*; import java.io.*; public class Main{ static FastReader scn = new FastReader(); static OutputStream out = new BufferedOutputStream ( System.out ); public static void main(String[] args) throws IOException { // TODO Auto-gener...
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 lli long long int lli dp[3001][3001]; lli solve(int i, int j, int arr[]){ if(dp[i][j]!=-1) return dp[i][j]; if(i==j) return dp[i][j]=arr[i]; return dp[i][j]=max(arr[i]-solve(i+1,j,arr),(arr[j]-solve(i,j-1,arr))); } int main(){ int n; cin>>n; int 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
def deque(n, arr): dp = [[0] * n for _ in range(n)] for i in range(n): dp[i][i] = arr[i] for l in range(n-2, -1, -1): for r in range(l+1, n): L = arr[l] - dp[l+1][r] R = arr[r] - dp[l][r-1] dp[l][r] = L if L > R else R return dp[0][n-1] n = int(input...
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; int main(){ int n; cin>>n; int a[n]; for(int i=0;i<n;i++) cin>>a[i]; ll d[n][n]; int i,j; for(i=0,j=0;i<n;i++,j++) d[i][j]=a[i]; for(int k=1;k<n;k++){ for(i=0,j=k;j<n;i++,j++){ ...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; int b, n[3005]; long long r[3005][3005]; int main() { cin >> b; for(int i = 0; i < b; i++) { cin >> n[i]; } for(int i = 0; i < b; i++) { r[i][i] = n[i]; } for(int i = 1; i < b; i++) { int l = 0; for(int d = i; d < b; d++) { r[l][d] = max(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
N=int(input()) A=[int(i) for i in input().split()] S=[0]*(N+1) for i in range(1,N+1): S[i]=S[i-1]+A[i-1] dp=[[0]*N for i in range(N)] for i in range(N): dp[i][i]=A[i] for k in range(1,N): for i in range(N-k): j=i+k dp[i][j]=max(S[j]-S[i]-dp[i][j-1]+A[j],S[j+1]-S[i+1]-dp[i+1][j]+A[i]) print(2...
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()) aaa = 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 = l + w dpl = -dp[l + 1][r] + aaa[l] dpr = -dp[l][r - 1] + aaa[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
import java.util.Scanner; public class Main { static long[][] sol; static boolean[][] flag; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); sol = new long[N][N]; flag = new boolean[N][N]; int[] arr = new int[N]; for (int i = 0; i < N;...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<cstdio> #include<algorithm> using namespace std; int n, w[3010], K; long long D[3010][3010]; int main() { int i, j, k; scanf("%d", &n); for (i = 1; i <= n; i++)scanf("%d", &w[i]); for (int L = 1; L <= n; L++) { for (i = 1; i <= n - L + 1; i++) { j = i + L - 1; if (L == 1) { D[i][j] = w[i]; ...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) a = list(map(int,input().split())) dp = [[0] * (n+1) for i in range(n+1)] dp[0][0] = 0 for j in range(1,n+1): for i in range(n+1-j): if (n-j) %2 == 0: dp[i][i+j] = max(dp[i+1][i+j] + a[i],dp[i][i+j-1]+a[i+j-1]) else: dp[i][i+j] = min(dp[i+1][i+j] - a[i],dp[i][i+j-1]-a[i+j-1]) prin...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import sys input=sys.stdin.readline def chmax(a,b): if a>=b: return a return b def chmin(a,b): if a<=b: return a return b def main(): N=int(input()) a=list(map(int,input().split())) dp=[[0]*(N+2) for _ in range(N+2)] for len in range(1,N+1): for i in range(N+1-len...
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 sun.misc.Unsafe; import java.lang.reflect.*; public class Main { public static void main(String[] args) throws Exception { Field f = Unsafe.class.getDeclaredField("theUnsafe"); f.setAccessible(true); Unsafe unsafe = (Unsafe) f.get(null); Scanner sc = new ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; #define int int64_t int a[3005],dp[3005][3005]; int dpgame(int l,int r) { if(dp[l][r]!=-1) return dp[l][r]; if(l==r) return a[l]; dp[l][r]=max(a[l]-dpgame(l+1,r),a[r]-dpgame(l,r-1)); return dp[l][r]; } signed main() { int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); long S = 0; int[] a = new int[N]; for (int i = 0; i < N; ++i) { a[i] = scanner.nextInt(); S += ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
N = int(input()) a = list(map(int, input().split())) dp = [[0] * (N + 1) for i in range(N + 1)] for k in range(1, N + 1): for l in range(N - k + 1): r = l + k if (N - k) % 2 == 0: dp[l][r] = max(dp[l + 1][r] + a[l], dp[l][r - 1] + a[r - 1]) else: dp[l][r] = min(dp[l +...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n=int(input()) a=list(map(int,input().split())) dp = [[0 for _ in range(n)] for __ in range(n)] for i in range(n): if n%2 == 1: dp[i][0] = a[i] else: dp[i][0] = -a[i] for le in range(1,n): for i in range(n): if i + le >= n: continue if (n-le)%2 == 1: dp[i][le] = ...
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 main(): N = int(input()) a = list(map(int, input().split())) dp = [0 for i in range(N)] for i in range(N-1, -1, -1): dp[i] = a[i] prev = dp[i] for j in range(i+1, N): right = a[i] - dp[j] left = a[j] - prev if right > left: dp[j] = right prev = right else: dp[j] = left prev = l...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> #define ll long long using namespace std; int n; int a[3005]; ll dp[3005][3005]; int main(){ cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } for(int i=n-1;i>=0;i--){ for(int j=i;j<n;j++){ if(dp[i]==dp[j]) dp[i][j]=a[i]; else dp[i][j]=max(a[i]-dp[i+1][j],a[j]-dp[i][j-1]); } ...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll f[3005][3005],a[3005],n; int main() { scanf("%lld",&n); for(int i=1;i<=n;i++) scanf("%lld",a+i); for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) { int l=j-i+1,r=j; f[l][r]=max(a[l]-f[l+1][r],a[r]-f[l][r-1]...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) a = list(map(int, input().split())) dp = [[0 for _ in range(n+1)] for _ in range(n+1)] #dp[l][r]: 左l個右r個取ったところからの前-後max for i in range(n): dp[i][n-1-i] = a[i] for x in range(n-2, -1, -1): for y in range(x+1): l, r = y, x-y dp[l][r] = max(-dp[l+1][r] + a[l], -dp[l][r+1] + a[-(r+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 using namespace std; ll dp[3010][3010]; ll a[3010]; ll solve(int i, int j){ if(i > j) return 0; if(dp[i][j] != -1) return dp[i][j]; return dp[i][j] = max(a[i] - solve(i + 1, j), a[j] - solve(i, j - 1)); } int main(){ int n; cin >> n; memset(dp, -1, sizeof(dp))...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <stdio.h> #include <algorithm> #include <string> #include <vector> using namespace std; int N,S[3030]; long long D[3030][3030]; int main() { scanf ("%d",&N); for (int i=0;i<N;i++) scanf ("%d",&S[i]), D[i][i] = S[i]; for (int l=1;l<N;l++){ for (int i=0,j=l;j<N;i++,j++){ D[i][j] = S[i] - D[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
#include <bits/stdc++.h> using namespace std; typedef long long int in; in dp[3005][3005]; int main() { in n; cin>>n; in a[n],i,j,k=0; for(i=0;i<n;++i) { cin>>a[i]; dp[i][i]=a[i]; } for(i=0;i<n;++i) { ++k; for(j=0;j<n;++j) if(j+...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> using namespace std; #define int long long int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin>>n; int dp[n+5][n+5]; int a[n+5]; 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++) ...
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())) # 元の数列から連続する区間[i,j) を抜き出した状態から、 # 最善を尽くした場合のX-Yの最大値 dp=[[0]*(n+1) for i in range(n+1)] for i in range(n + 1): dp[i][i] = 0 for length in range(1,n+1): for i in range(n+1): j=length+i if j<=i or j>n:continue if (n-length)%2==0: # 先手 dp[i][j]...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; typedef long long int ll; int main() { int n; cin >> n; vector<ll> v(n); for (int i = 0; i < n; i++) cin >> v[i]; ll dp[n + 1][n + 1]; // この区間で自分に回ってきたとき、自分がとれる最大値 memset(dp, 0, sizeof(dp)); for (int w = 1; w <= n; w++) for (int i = 0; i + w <= 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
n = int(input()) A = list(map(int,input().split())) dp = [[0]*(n+1) for _ 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][i+k] = max(dp[i+1][i+k] + A[i], dp[i][i+k-1] + A[i+k-1]) else: dp[i][i+k] = min(dp[i+1][i+k] - A[i], dp[i][i+k-1] - A[i+...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include<bits/stdc++.h> #define maxn 3200 using namespace std; typedef long long LL; LL f[maxn][maxn]; LL a[maxn]; int main(){ ios::sync_with_stdio(false); int N; cin >> N; for(int i = 1; i <= N; i += 1) cin >> a[i]; for(int L = 0; L < N; L += 1) for(int i = 1; i + L <= N; i += 1){ f[i][L] = L ? max(- f[i + 1...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.*; import java.util.*; public class Main { static class Solver { int[] arr; long[][] mem; int mxN = 3001; public void solve(MyReader in, PrintWriter out) { int n = in.nextInt(); arr= in.nextIntArray(n); mem = new long[mxN][mxN]; ...
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; class Main { public static void playGame(long[] sequence) { long[][] dp = new long[sequence.length + 1][sequence.length + 1]; for(int i=1; i<=sequence.length; i++) dp[i][i] = sequence[i-1]; for(int gap= 1; gap<sequence.length...
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 ll long long int n; ll a[3007],dp[3007][3007],pre[3007]; ll dfs(int l,int r){ if(l==r){ return a[l]; } if(dp[l][r]!=0)return dp[l][r]; return dp[l][r]=(pre[r]-pre[l-1])-min(dfs(l+1,r),dfs(l,r-1)); } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%lld"...
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 int main() { int n; cin>>n; int a[n]; for(int i=0; i<n; i++) cin>>a[i]; ll dp[n+1][n+1]; for(int i=0; i<=n; i++) dp[i][0] = 0; for(int l=1; l<=n; l++) for(int i=0; i+l<=n; i++) dp[i][l] = max(a[i] -...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long dp[] = new long[n * n]; for ( int i = 0; i < n; i++ ) { dp[ i*n + i] = in.nextInt(); } in.close(); for ( int col = 1; col < n; col++ ) { int r = 0,...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } long dp[][]=new long[n+1][n+1]; for(int i=n-1;i>=0;i--) { for(int j=i;j<n;j++) { 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
N = int(input()) *A, = map(int, input().split()) dp = [[0] * (N+1) for _ in range(N+1)] for n in range(1, N+1): for i in range(N-n+1): j = i + n if (N - n) % 2 == 0: # first player dp[i][j] = max(dp[i+1][j] + A[i], dp[i][j-1] + A[j-1]) else: # second player 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 = list(map(int, input().split())) # dp[i][j] = a[i:j] を考えたときの X-Y の最大値 dp = [[0]*(N+1) for _ in range(N+1)] for l in range(1, N+1): for i in range(N-l+1): j = i+l if (N-l)%2 == 0: dp[i][j] = max(dp[i+1][j]+a[i], dp[i][j-1]+a[j-1]) else: dp[i][j] = ...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.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
import java.util.Scanner; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int []a = IntStream.range(0, n).map(x -> in.nextInt()).toArray(); long [][]dp = new long[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 <bits/stdc++.h> using namespace std; typedef long long ll; ll a[5005]; ll fun(ll l,ll r,vector<vector<ll>>&dp) { if(l>r)return 0; if(l==r)return dp[l][r]=a[l]; if(dp[l][r]!=-1)return dp[l][r]; return dp[l][r]=max(a[l]-fun(l+1,r,dp),a[r]-fun(l,r-1,dp)); } int main() { ll n,i,j,k; cin>>n; fo...
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 static java.util.Comparator.comparing; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Scanner; public class Main { public sta...
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; long long a[3010], dp[3010][3010]; long long f(int x, int y) { if (x>y) return 0; if (dp[x][y] != -1) return dp[x][y]; dp[x][y] = max(a[x] - f(x+1, y), a[y] - f(x, y-1)); return dp[x][y]; } int main () { cin >> N; for(int i=1; i<=N; i++) ci...
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 main() { ll n; cin>>n; ll a[n]; for(ll i=0;i<n;i++)cin>>a[i]; ll dp[n][n]; for(ll i=0;i<n;i++)dp[i][i]=a[i]; for(ll l=2;l<=n;l++) { for(ll i=0;i<n-l+1;i++) { ll j=i+l-1; dp[i][j]=max(a[i]-dp[i+1][j],a[j]-dp[i][j-1]); } } cout<<d...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#dp[i][j] denotes subarray from i to j left n=int(input()) l=input().split() li=[int(i) for i in l] dp=[[0 for i in range(n)]for i in range(n)] for i in range(n): dp[i][i]=li[i] for gap in range(1,n): for start in range(n-gap): end=start+gap dp[start][end]=max(li[start]-dp[start+1][end],li[end]-dp[start][end-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.*; import java.math.*; public class Main{ static class Pair implements Comparable<Pair>{ int a; int b; public Pair(int x,int y){a=x;b=y;} public Pair(){} public int compareTo(Pair p1){ if(a == p1.a) return b - p1.b; return a - p1.a; } } s...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) a = [0]+list(map(int,input().split())) dp = [[0]*(n+2) for _ in range(n+2)] for l in range(1,n+2): for i in range(1,n-l+2): j = l+i if (n-l)%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],dp[i][j-1]+a[j-1]) print(dp[1][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 rep(i, n) for (int i = 0; i < n; ++i) int main() { int n; cin >> n; vector<long long> a(n); rep(i, n) cin >> a[i]; long long dp[n][n]; rep(i, n) dp[i][i] = a[i]; for (int i = n - 2; i > -1; --i) { for (int j = i + 1; j < n; ++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
#include<bits/stdc++.h> using namespace std; long long int dp[3010][3010]; int main(){ int n; cin>>n; long long 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[L]-dp[L+1][R],a[R]-dp[L][R-1]); } cout<<dp[0][n-1]; }
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
N = int(input()) *A, = map(int, input().split()) t = (N & 1) S = [0]*(N+1) for l in range(1, N+1): S = [min(S[i+1] - A[i], S[i] - A[i+l-1]) for i in range(N-l+1)] if (l ^ N) & 1 else [max(S[i+1] + A[i], S[i] + A[i+l-1]) for i in range(N-l+1)] 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.util.*; import java.io.*; // Template for atcoder public class Main { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; final long MOD = 1000L * 1000L * 1000L + 7; private static final int[] dx = {0, -1, 0, 1}; private static final int[] dy = {1, 0, -1, 0}; ...
JAVA
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
#include <bits/stdc++.h> using namespace std; long long int dp[3010][3010]; int main(){ long long int n,l,r,i,a[3010]; cin>>n; for(i=1;i<n+1;i++){ cin>>a[i]; } for(l=n;l>0;l--){ for(r=1;r<n+1;r++){ if(l>r) dp[l][r]=0; else if(l==r) dp[l][r]=a[l]; e...
CPP
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
n = int(input()) a = list(map(int, input().split())) dp = [[0] * n for _ in range(n)] for i in range(n): dp[i][i] = a[i] for l in range(1, n): for s in range(n - l): dp[s][s + l] = max(a[s] - dp[s + 1][s + l], a[s + l] - dp[s][s + l - 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, = map(int, input().split()) dp = [[0]*(N+1) for _ in range(N+1)] for l in range(1, N+1): for i in range(N-l+1): j = i+l if (N-l) % 2 == 0: # first dp[i][j] = max([dp[i+1][j]+a[i], dp[i][j-1]+a[j-1]]) else: # second 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 = list(map(int, input().split())) dp = [[0] * (N) for _ in range(N+1)] ans = int(0) for i in range(N): dp[i][i] = a[i] #for l in range(1,N) for l in range(1,N): for i in range(N-l): j = i + l dp[i][j] = max(dp[i][i]-dp[i+1][j], dp[j][j]-dp[i][j-1]) ans = dp[0][N-1] print...
PYTHON3
p03171 Educational DP Contest - Deque
Taro and Jiro will play the following game against each other. Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro: * Remove the element at the beginning or the end of a. The player earns x points, whe...
6
0
import java.io.*; import java.util.*; import java.math.*; public class Main { public static void solve() { int n = in.nextInt(); int[] stones = new int[n]; for(int i = 0; i < n; i ++) stones[i] = in.nextInt(); long[][] store = new long[n][n]; for(int i = 0; ...
JAVA