exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
0e992edec6cb38d411942ab78bea83ac
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; public class A { static class fast { BufferedReader br; StringTokenizer st; public fast() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } public static int maxSum(int arr[], int n, int k) { // k must be smaller than n if (n < k) { System.out.println("Invalid"); return -1; } if (k == 0)return 0; // Compute sum of first window of size k int res = 0; for (int i = 0; i < k; i++) res += arr[i]; // Compute sums of remaining windows by // removing first element of previous // window and adding last element of // current window. int curr_sum = res; for (int i = k; i < n; i++) { curr_sum += arr[i] - arr[i - k]; res = Math.max(res, curr_sum); } return res; } public static void main(String[] args)throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { try { System.setIn(new FileInputStream(new File("i.txt"))); System.setOut(new PrintStream(new File("o.txt"))); } catch (Exception e) {} } fast f = new fast(); int t = f.nextInt(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int n = f.nextInt(); int x = f.nextInt(); int[]arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = f.nextInt(); } int[]l = new int[n + 1]; for (int i = 0; i <= n; i++) { l[i] = (maxSum(arr, n, i)); } for (int k = 0; k <= n; k++) { int maxi = 0; for (int i = 0; i <= n; i++) { maxi = Math.max(maxi, Math.min(i, k) * x + l[i]); } sb.append(maxi + " "); } sb.append("\n"); } System.out.println(sb); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 11
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
c9df27fad1249dac1419649185ba5aee
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; public class Question4 { static boolean multipleTC = true; final static int Mod = 1000000007; final static int Mod2 = 998244353; final double PI = 3.14159265358979323846; int MAX = 1000000007; void pre() throws Exception { } long[] calMax(List<Long> list, int strtIn, int endIn, int n){ long total = 1; long ans[] = new long[3]; for(int i=1;i<(list.size()-1);i++) total *= list.get(i); pn(total); if(total > 0) { ans[0] = total; ans[1] = strtIn - 1; ans[2] = n - endIn; return ans; } int i = strtIn, j = endIn; long prefixProd = 1, suffixProd = 1; boolean flag1 = false, flag2 = false; while(!flag1 || !flag2) { if(!flag1) { prefixProd *= list.get(i); if(list.get(i) < 0) { flag1 = true; } else i++; } if(!flag2) { suffixProd *= list.get(j); if(list.get(j) < 0) { flag2 = false; } else j--; } } if(total/prefixProd >= total/suffixProd) { total /= prefixProd; ans[0] = total; ans[1] = i; ans[2] = n - endIn; return ans; } total /= suffixProd; ans[0] = total; ans[1] = strtIn - 1; ans[2] = (n - (j-1)); return ans; } void solve(int t) throws Exception { int n = ni(); long x = nl(); StringBuilder ans = new StringBuilder(); long arr[] = new long[n+1]; for(int i=1;i<=n;i++) arr[i] = nl(); // Calculating for each sub array of length k long sumlen[] = new long[n+1]; sumlen[0] = 0; for(int i=1;i<=n;i++) { //Sub array of length i long max_sum = 0; for (int j=1;j<=i;j++) max_sum += arr[j]; long window_sum = max_sum; for (int j=(i+1);j<=n;j++) { window_sum += arr[j] - arr[j-i]; max_sum = Math.max(max_sum, window_sum); } sumlen[i] = max_sum; } for(int i=0;i<=n;i++) { long max = 0; for(int j=0;j<=n;j++) { long curr_sum = sumlen[j] + (Math.min(j, i)*x); max = Math.max(max, curr_sum); } ans.append(max + " "); } pn(ans); } double dist(int x1, int y1, int x2, int y2) { double a = x1 - x2, b = y1 - y2; return Math.sqrt((a * a) + (b * b)); } int[] readArr(int n) throws Exception { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } void sort(int arr[], int left, int right) { ArrayList<Integer> list = new ArrayList<>(); for (int i = left; i <= right; i++) list.add(arr[i]); Collections.sort(list); for (int i = left; i <= right; i++) arr[i] = list.get(i - left); } void sort(int arr[]) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < arr.length; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < arr.length; i++) arr[i] = list.get(i); } public long max(long... arr) { long max = arr[0]; for (long itr : arr) max = Math.max(max, itr); return max; } public int max(int... arr) { int max = arr[0]; for (int itr : arr) max = Math.max(max, itr); return max; } public long min(long... arr) { long min = arr[0]; for (long itr : arr) min = Math.min(min, itr); return min; } public int min(int... arr) { int min = arr[0]; for (int itr : arr) min = Math.min(min, itr); return min; } public long sum(long... arr) { long sum = 0; for (long itr : arr) sum += itr; return sum; } public long sum(int... arr) { long sum = 0; for (int itr : arr) sum += itr; return sum; } String bin(long n) { return Long.toBinaryString(n); } String bin(int n) { return Integer.toBinaryString(n); } static int bitCount(int x) { return x == 0 ? 0 : (1 + bitCount(x & (x - 1))); } static void dbg(Object... o) { System.err.println(Arrays.deepToString(o)); } int bit(long n) { return (n == 0) ? 0 : (1 + bit(n & (n - 1))); } int abs(int a) { return (a < 0) ? -a : a; } long abs(long a) { return (a < 0) ? -a : a; } void p(Object o) { out.print(o); } void pn(Object o) { out.println(o); } void pni(Object o) { out.println(o); out.flush(); } void pn(int[] arr) { int n = arr.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(arr[i] + " "); } pn(sb); } void pn(long[] arr) { int n = arr.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(arr[i] + " "); } pn(sb); } String n() throws Exception { return in.next(); } String nln() throws Exception { return in.nextLine(); } int ni() throws Exception { return Integer.parseInt(in.next()); } long nl() throws Exception { return Long.parseLong(in.next()); } double nd() throws Exception { return Double.parseDouble(in.next()); } public static void main(String[] args) throws Exception { new Question4().run(); } FastReader in; PrintWriter out; void run() throws Exception { in = new FastReader(); out = new PrintWriter(System.out); int T = (multipleTC) ? ni() : 1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception { br = new BufferedReader(new FileReader(s)); } String next() throws Exception { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception { String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } return str; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 11
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
74e5501faefb674ba3f51bca9acbf4a0
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; public class SubarraySums { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-- > 0) { int n = s.nextInt(); long x = s.nextLong(); int arr[] = new int[n]; for(int i = 0 ; i < n ; i++) { arr[i] = s.nextInt(); } long dp[] = new long[n + 1]; for(int i = 0 ; i <= n ; i++) { dp[i] = Long.MIN_VALUE; } long p[] = new long[n + 1]; for(int i = 0; i < n; i++) { long sum = 0; for(int j = i ; j < n ; j++) { sum += arr[j]; dp[j-i+1] = Math.max(dp[j-i+1],sum); } } for(int i = 0 ; i <= n ; i++) { p[i] = dp[i]; } for(int i = n-1 ; i >= 0 ; i--) { dp[i] = Math.max(dp[i], dp[i+1]); } long mx = 0; long ans[] = new long[n + 1]; for(int i = 0 ; i <= n ; i++) { long ass = 0; ass = dp[i]+(long)x * (long)i; ans[i] = Math.max(ass,mx); mx = Math.max(mx,(long)i*(long)x+p[i]); } for(int i = 0; i <= n; i++) { System.out.print(ans[i] + " "); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 11
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
fbef829a71f40368ae69ac50c7d6c029
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
//1644C - subarrays import java.util.*; import java.io.*; public class Solution { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void solve() throws IOException { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); long[] arr = new long[n]; st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(st.nextToken()); } long[] ps = new long[n + 1]; for (int i = 0; i < n; i++) { ps[i + 1] = ps[i] + arr[i]; } LinkedList<long[]> q = new LinkedList<>(); q.add(new long[] {0,0}); //length of subarray, max value for (int i = 1; i <= n; i++) { long maxSub = Integer.MIN_VALUE; for (int j = 0; j + i < ps.length; j++) { maxSub = Math.max(maxSub, ps[j + i] - ps[j]); } while(!q.isEmpty() && q.peekLast()[0] <= maxSub) q.removeLast(); if(!q.isEmpty() && q.peekLast()[0] + q.peekLast()[1] * (long)x < maxSub + i * (long)x || q.isEmpty()) { q.add(new long[] {maxSub, i}); } } long[] ans = new long[n + 1]; for (int k = 0; k <= n; k++) { if(q.size() > 1) { long[] o1 = q.remove(); long[] o2 = q.remove(); long calcO1 = o1[0] + (long)x * Math.min(o1[1], k); long calcO2 = o2[0] + (long)x * Math.min(o2[1], k); if(calcO2 >= calcO1) { ans[k] = calcO2; q.addFirst(o2); } else { ans[k] = calcO1; q.addFirst(o2); q.addFirst(o1); } } else { ans[k] = q.peek()[0] + (long)x * Math.min(q.peek()[1], k); } } System.out.print(ans[0]); for (int i = 1; i < ans.length; i++) { System.out.print(" " + ans[i]); } System.out.println(); } public static void main(String[] args) throws IOException { int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++) solve(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 11
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
f46b506c1dada076b568a2c4f9cc169e
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; public class Solution{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); static StringTokenizer st; public static void main(String[] YDSV) throws IOException{ //int t=1; int t=Integer.parseInt(br.readLine()); while(t-->0) Solve(); bw.flush(); } public static void Solve() throws IOException{ st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()),x=Integer.parseInt(st.nextToken()); st=new StringTokenizer(br.readLine()); int[] ar=new int[n]; for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken()); int[][] dp=new int[n+1][n+1]; int ans=0; for(int i=1;i<=n;i++){ dp[0][i]=Math.max(0,dp[0][i-1]+ar[i-1]); ans=Math.max(ans,dp[0][i]); } bw.write(ans+" "); for(int i=1;i<=n;i++){ ans=0; for(int j=1;j<=n;j++){ dp[i][j]=Math.max(0,dp[i-1][j-1]+ar[j-1]+x); ans=Math.max(ans,dp[i][j]); } bw.write(ans+" "); } bw.newLine(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 11
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
d09b8ddb54da95a6961c5fde1962ecda
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class Main { static class JoinSet { int[] fa; JoinSet(int n) { fa = new int[n]; for (int i = 0; i < n; i++) fa[i] = i; } int find(int t) { if (t != fa[t]) fa[t] = find(fa[t]); return fa[t]; } void join(int x, int y) { x = find(x); y = find(y); fa[x] = y; } } static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static int mod = (int)1e9+7; static int[][] dir1 = new int[][]{{0,1},{0,-1},{1,0},{-1,0}}; static int[][] dir2 = new int[][]{{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; static boolean[] prime = new boolean[10]; static { for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i < prime.length; i++) { if (prime[i]) { for (int k = 2; i * k < prime.length; k++) { prime[i * k] = false; } } } } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return a * b / gcd(a, b); } static int get() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Integer.parseInt(ss); } static long getx() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Long.parseLong(ss); } static int[] getint() throws Exception { String[] s = bf.readLine().split(" "); int[] a = new int[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(s[i]); } return a; } static long[] getlong() throws Exception { String[] s = bf.readLine().split(" "); long[] a = new long[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Long.parseLong(s[i]); } return a; } static String getstr() throws Exception { return bf.readLine(); } static void println() throws Exception { bw.write("\n"); } static void print(int a) throws Exception { bw.write(a + "\n"); } static void print(long a) throws Exception { bw.write(a + "\n"); } static void print(String a) throws Exception { bw.write(a + "\n"); } static void print(int[] a) throws Exception { for (int i : a) { bw.write(i + " "); } println(); } static void print(long[] a) throws Exception { for (long i : a) { bw.write(i + " "); } println(); } static void print(int[][] a) throws Exception { for (int i[] : a) print(i); } static void print(long[][] a) throws Exception { for (long[] i : a) print(i); } static void print(char[] a) throws Exception { for (char i : a) { bw.write(i +""); } println(); } static long pow(long a, long b) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans *= a; } a *= a; b >>= 1; } return ans; } static int powmod(long a, long b, int mod) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans = ans * a % mod; } a = a * a % mod; b >>= 1; } return (int) ans; } static void sort(int[] a) { int n = a.length; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static void sort(long[] a) { int n = a.length; Long[] b = new Long[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static int max(int a, int b) { return Math.max(a,b); } static int min(int a, int b) { return Math.min(a,b); } static long max(long a, long b) { return Math.max(a,b); } static long min(long a, long b) { return Math.min(a,b); } static int max(int[] a) { int max = a[0]; for(int i : a) max = max(max,i); return max; } static int min(int[] a) { int min = a[0]; for(int i : a) min = min(min,i); return min; } static long max(long[] a) { long max = a[0]; for(long i : a) max = max(max,i); return max; } static long min(long[] a) { long min = a[0]; for(long i : a) min = min(min,i); return min; } static int abs(int a) { return Math.abs(a); } static long abs(long a) { return Math.abs(a); } static long sum(int[] a) { long s = 0; for(int i : a) s += i; return s; } public static void main(String[] args) throws Exception { int t = get(); while (t-- > 0){ int p[] = getint(); int n = p[0], x = p[1]; int[] a = getint(); int dp[] = new int[n+1]; Arrays.fill(dp,-mod); long f[] = new long[n+1]; dp[0] = 0; for(int i = 0;i < n;i++){ int s = 0; for(int j = i;j < n;j++){ s += a[j]; dp[j-i+1] = max(dp[j-i+1],s); } } for(int i = 0;i <= n;i++){ for(int j = 0;j <= n;j++){ f[i] = max(f[i],dp[j]+min(i,j)*x); } } print(f); } bw.flush(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 11
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
9a19de1503158da346c592ef26facc4e
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.0000000"); final static int mod = (int) (1e9 + 7); final static int MAX = Integer.MAX_VALUE; final static int MIN = Integer.MIN_VALUE; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { // ==== start ==== int t = readInt(); // int t = 1; preprocess(); while (t-- > 0) { solve(); } out.flush(); } // 10 10+101 = 110 private static void solve() throws IOException { int n = readInt(); int k = readInt(); int[] num = readIntArray(n); deal( n , k , num); } private static void deal(int n , int k , int[] num){ // -2 -7 -1 -2 -7 4 int[] dp = new int[n+1]; int max = 0; for(int i = 0 ; i < n ; i++){ dp[i+1] = Math.max(0 , dp[i]+num[i]); max = Math.max(max , dp[i+1]); } out.print(max+" "); for(int i = 0 ; i < n ; i++){ for(int j = n-1 ; j >= i ; j--){ dp[j+1] = dp[j]+k+num[j]; max = Math.max(max , dp[j+1]); } out.print(max+" "); } out.println(); } private static void preprocess() { } // ======================= FOR INPUT ================================== static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(readLine()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return br.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray(int n) throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(m); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static long power(long a, long b) { if (b == 0) return 1L; long ans = power(a, b >> 1); ans *= ans; if ((b & 1) == 1) ans *= a; return ans; } private static int mod_power(int a, int b) { if (b == 0) return 1; int temp = mod_power(a, b / 2); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static int multiply(int a, int b) { return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } // ==================== Primes using Seive ===================== private static List<Integer> getPrime(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) prime[j] = false; } } // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime() { int n = (int) 1e6 + 1; int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) { if (primes[j] == j) primes[j] = i; } } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return true; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return false; } // ==================== SEGMENT TREE (RANGE SUM) ===================== public static class SegmentTree { int n; int[] arr, tree, lazy; SegmentTree(int arr[]) { this.arr = arr; this.n = arr.length; this.tree = new int[(n << 2)]; this.lazy = new int[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, int val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, int val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } int query(int l, int r) { return query(1, 0, n - 1, l, r); } int query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } public void insert(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; } curr.isEnd = true; } public boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ==================== FENWICK TREE ================================ static class FT { long[] tree; int n; FT(int[] arr, int n) { this.n = n; this.tree = new long[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } long query(int l, int r) { return getSum(r) - getSum(l - 1); } long getSum(int idx) { long ans = 0L; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return ans; } } // ==================== OTHER CLASSES ================================ static class Pair implements Comparable<Pair> { int first, second; Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair o) { return this.first - o.first; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 11
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
1c3e660cf0328bb8f7c94b66f1f8915f
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
/* بسم الله الرحمن الرحيم /$$$$$ /$$$$$$ /$$ /$$ /$$$$$$ |__ $$ /$$__ $$ |$$ |$$ /$$__ $$ | $$| $$ \ $$| $$|$$| $$ \ $$ | $$| $$$$$$$$| $$ / $$/| $$$$$$$$ / $$ | $$| $$__ $$ \ $$ $$/ | $$__ $$ | $$ | $$| $$ | $$ \ $$$/ | $$ | $$ | $$$$$$/| $$ | $$ \ $/ | $$ | $$ \______/ |__/ |__/ \_/ |__/ |__/ /$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$ | $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$ | $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$ | $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/ | $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$ | $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$ | $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$ |__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/ */ import java.util.*; import java.lang.*; import java.io.*; public class IncreaseSubarraySum { public static void main(String[] args) throws java.lang.Exception { // your code goes here try { // Scanner sc=new Scanner(System.in); FastReader sc = new FastReader(); int t =sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); long x=sc.nextLong(); int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } long[] sums=new long[n+1]; Arrays.fill(sums,Long.MIN_VALUE); sums[0]=0; for(int i=0;i<n;i++){ long temp=0; for(int j=i;j<n;j++){ temp+=Long.valueOf(arr[j]); sums[j-i+1]=Math.max(temp,sums[j-i+1]); } } for(int i=0;i<=n;i++){ long curr=0; for(int j=0;j<=n;j++){ curr=Math.max(curr,sums[j]+Long.valueOf(Math.min(i,j))*x); } System.out.print(curr+" "); } System.out.println(); /* * long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } */ } } catch (Exception e) { return; } } public static class pair { int ff; int ss; pair(int ff, int ss) { this.ff = ff; this.ss = ss; } } public static void rsa(int[] arr2){ Arrays.sort(arr2); int size = arr2.length; for (int left = 0; left < size / 2; left++) { int right = size - left - 1; int temp = arr2[right]; arr2[right] = arr2[left]; arr2[left] = temp; } } public static boolean checkBothTheArraysSame(int[] a, int[] b, int i, int j) { int n = a.length; int[] a1 = a; int[] b1 = b; if (i >= 0 && j >= 0) { for (int k = i; k <= j; k++) { a1[k] += 1; } } Arrays.sort(a1); Arrays.sort(b1); for (int ii = 0; ii < n; ii++) { if (a1[ii] != b1[ii]) return false; } return true; } static int BS(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] < element) { low = mid + 1; } else { high = mid; } } if (arr[low] == element) { return low; } else if (arr[high] == element) { return high; } return -1; } static int lower_bound(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] < element) { low = mid + 1; } else { high = mid; } } if (arr[low] >= element) { return low; } else if (arr[high] >= element) { return high; } return -1; } static int upper_bound(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] <= element) { low = mid + 1; } else { high = mid; } } if (arr[low] > element) { return low; } else if (arr[high] > element) { return high; } return -1; } public static long gcd_long(long a, long b) { // a/b,a-> dividant b-> divisor if (b == 0) return a; return gcd_long(b, a % b); } public static int gcd_int(int a, int b) { // a/b,a-> dividant b-> divisor if (b == 0) return a; return gcd_int(b, a % b); } public static int lcm(int a, int b) { int gcd = gcd_int(a, b); return (a * b) / gcd; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.valueOf(Integer.parseInt(next())); } String nextLine() { String s = ""; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } Long nextLong() { return Long.parseLong(next()); } } /* * JAVA STRING CONTAINS METHOD--------->str1.contains(str2)-->if(str2 is present * in str1){ return true; } else return false; */ public static boolean contains(String main, String Substring) { boolean flag = false; if (main == null && main.trim().equals("")) { return flag; } if (Substring == null) { return flag; } char fullstring[] = main.toCharArray(); char sub[] = Substring.toCharArray(); int counter = 0; if (sub.length == 0) { flag = true; return flag; } for (int i = 0; i < fullstring.length; i++) { if (fullstring[i] == sub[counter]) { counter++; } else { counter = 0; } if (counter == sub.length) { flag = true; return flag; } } return flag; } //FACTORISATION OF A NUMBER ---> OPTIMISED CODE //TC -->O(sqrt(N)); public static void factorize(int n){ for(int i=2;i*i<=n;i++){ if(n%i==0){ int cnt=0; while(n%i==0){ n/=i; cnt++; } System.out.println("DIVISOR is "+i+" "+"COUNT is "+cnt); } } if(n!=1){ //n is prime number System.out.println("DIVISOR is "+n+" "+"COUNT is "+1); } } //DISJOINT SET UNINION OPTIMALLY IMPLEMENTED USING PATH COMPRESSION AND RANK ARRAY------>>>>>>>>>>>>>>>>>>>>> //parent[i].ff==parent of i and parent[i].ss gives the rant of the set in which i belongs to public static int find_set(int a,pair[] parent){ if(parent[a].ff==a)return a; return parent[a].ff=find_set(parent[a].ff,parent); } public static void union_set(int a,int b,pair[] parent){ int a_root=find_set(a, parent); int b_root=find_set(b, parent); if(a_root==b_root)return; if(parent[a_root].ss<parent[b_root].ss){ parent[a_root].ff=b_root; } else if(parent[a_root].ss>parent[b_root].ss){ parent[b_root].ff=a_root; } else{ parent[b_root].ff=a_root; parent[a_root].ss++; } } } /* * public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){ * return true; } if(n<1 || m<1 || k<0){ return false; } boolean * tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; } * return false; } */
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 11
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
361db669209fc08dc46d8f9a41e71e7b
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class Main { public static int mod = (int) 1e9 + 7; // **** -----> Disjoint Set Union(DSU) Start ********** public static int findPar(int node, int[] parent) { if (parent[node] == node) return node; return parent[node] = findPar(parent[node], parent); } public static boolean union(int u, int v, int[] rank, int[] parent) { u = findPar(u, parent); v = findPar(v, parent); if(u == v) return false; if (rank[u] < rank[v]) parent[u] = v; else if (rank[u] > rank[v]) parent[v] = u; else { parent[u] = v; rank[v]++; } return true; } // **** DSU Ends *********** //Pair with int int public static class Pair{ public int a; public int b; Pair(int a , int b){ this.a = a; this.b = b; } @Override public String toString(){ return a + " -> " + b; } } public static String toBinary(int decimal) {StringBuilder sb = new StringBuilder();while (decimal > 0) {sb.append(decimal % 2);decimal = decimal / 2;}return sb.reverse().toString();} public static boolean isPalindrome(String s) {int i = 0, j = s.length() - 1;while (i < j) {if (s.charAt(i) != s.charAt(j))return false;i++;j--;}return true;} public static boolean isPalindrome(int[] arr) {int i = 0, j = arr.length - 1;while (i < j) {if (arr[i] != arr[j])return false;}return true;} public static int pow(int x, int y) {int res = 1;x = x % mod;if (x == 0)return 0;while (y > 0) {if ((y & 1) != 0)res = (res * x) % mod;y = y >> 1;x = (x * x) % mod;}return res;} public static int gcd(int a, int b) {if (b == 0)return a;return gcd(b, a % b);} public static long gcd(long a, long b) {if (b == 0)return a;return gcd(b, a % b);} public static void sort(long[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);long temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);} public static void reverse(int a[]) {int i, k, t, n = a.length;for (i = 0; i < n / 2; i++) {t = a[i];a[i] = a[n - i - 1];a[n - i - 1] = t;}} public static void sort(int[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);int temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);} public static void revSort(int[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);int temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);reverse(a);} public static long LCM(long a, long b) {if (a > b) {long t = a;a = b;b = t;}a /= gcd(a, b);return (a * b);} public static int findMax(int[] a, int left, int right) {int res = left;int max = a[left];for (int i = left + 1; i <= right; i++) {if (a[i] > max) {max = a[i];res = i;}}return res;} public static long findClosest(long arr[], long target) {int n = arr.length;if (target <= arr[0])return arr[0];if (target >= arr[n - 1])return arr[n - 1];int i = 0, j = n, mid = 0;while (i < j) {mid = (i + j) / 2;if (arr[mid] == target)return arr[mid];if (target < arr[mid]) {if (mid > 0 && target > arr[mid - 1])return getClosest(arr[mid - 1], arr[mid], target);j = mid;} else {if (mid < n - 1 && target < arr[mid + 1])return getClosest(arr[mid], arr[mid + 1], target);i = mid + 1;}}return arr[mid];} public static long getClosest(long val1, long val2, long target) {if (target - val1 >= val2 - target)return val2;else return val1;} public static int findClosest(int arr[], int target) { int n = arr.length; if (target <= arr[0]) return arr[0]; if (target >= arr[n - 1]) return arr[n - 1]; int i = 0, j = n, mid = 0; while (i < j) { mid = (i + j) / 2; if (arr[mid] == target) return arr[mid];if (target < arr[mid]) {if (mid > 0 && target > arr[mid - 1])return getClosest(arr[mid - 1], arr[mid], target);j = mid;} else {if (mid < n - 1 && target < arr[mid + 1])return getClosest(arr[mid], arr[mid + 1], target);i = mid + 1;}}return arr[mid];} public static int getClosest(int val1, int val2, int target) {if (target - val1 >= val2 - target)return val2;else return val1;} public static String reverse(String str) {String nstr = "";char ch;for (int i = 0; i < str.length(); i++) {ch = str.charAt(i);nstr = ch + nstr;}return nstr;} public static boolean isPrime(int n){if (n <= 1)return false;if (n <= 3)return true;if (n % 2 == 0 || n % 3 == 0)return false;for (int i = 5; i * i <= n; i = i + 6)if (n % i == 0 || n % (i + 2) == 0)return false;return true;} public static int xorSum(int arr[], int n){int bits = 0;for (int i = 0; i < n; ++i)bits |= arr[i];int ans = bits * (int)Math.pow(2, n-1);return ans;} public static ArrayList<Integer> primeFactors(int n) { ArrayList<Integer> res = new ArrayList<>();while (n%2 == 0) { res.add(2); n = n/2; } for (int i = 3; i <= Math.sqrt(n); i = i+2) { while (n%i == 0) { res.add(i); n = n/i; } } if (n > 2) res.add(n);return res;} public static ArrayList<Long> primeFactors(long n) { ArrayList<Long> res = new ArrayList<>();while (n%2 == 0) { res.add(2L); n = n/2; } for (long i = 3; i <= Math.sqrt(n); i = i+2) { while (n%i == 0) { res.add(i); n = n/i; } } if (n > 2) res.add(n);return res;} static int lower_bound(int array[], int low, int high, int key){ int mid; while (low < high) { mid = low + (high - low) / 2; if (key <= array[mid]) high = mid; else low = mid + 1; } if (low < array.length && array[low] < key) low++; return low; } /********************************* Start Here ***********************************/ // int mod = 1000000007; // static HashMap<Integer, HashMap<Long, Integer>> map; public static void main(String[] args) throws java.lang.Exception { if (System.getProperty("ONLINE_JUDGE") == null) { PrintStream ps = new PrintStream(new File("output.txt")); System.setOut(ps); } FastScanner sc = new FastScanner("input.txt"); int T = sc.nextInt(); // int T = 1; while (T-- > 0) { int n = sc.nextInt(); int x = sc.nextInt(); int[] arr = sc.readArray(n); int[] maxForEveryLen = new int[n+1]; for(int i = 1; i <= n; i++){ int max = 0; for(int j = 0; j < i; j++) max += arr[j]; maxForEveryLen[i] = max; for(int j = i; j < n; j++){ max += (arr[j] - arr[j-i]); maxForEveryLen[i] = Math.max(maxForEveryLen[i], max); } // System.out.print(maxForEveryLen[i]+" "); } // System.out.println(); for(int k = 0; k <= n; k++){ int res = 0; for(int i = 0; i <= n; i++){ res = Math.max(res, maxForEveryLen[i]+x*Math.min(k, i)); } System.out.print(res+" "); } System.out.println(); } System.out.close(); } public static int bs(ArrayList<Integer> arr, int tar){ int n = arr.size(); int l = 0, r = n-1; int res = n; while(l <= r){ int mid = (l+r)/2; if(arr.get(mid) > tar) { res = mid; r = mid-1; }else l = mid+1; } return res; } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { br = new BufferedReader(new InputStreamReader(System.in)); } } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } public String nextLine() { if (st == null || !st.hasMoreTokens()) { try { return br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken("\n"); } public String[] readStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } int nextInt() { return Integer.parseInt(nextToken()); } String next() { return nextToken(); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] read2dArray(int n, int m) { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextInt(); } } return a; } long[][] read2dlongArray(int n, int m) { long[][] a = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextLong(); } } return a; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 11
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
33935d0f3ae454569cfb3ba6aa7e2d5b
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; public class EDU123C { FastScanner sc; PrintWriter out; int n; int[] s; public void solve() throws Exception { Locale.setDefault(Locale.US); sc = new FastScanner(System.in); out = new PrintWriter(System.out); int t = sc.nextInt(); for (; t > 0; t--) { n = sc.nextInt(); int x = sc.nextInt(); var a = new int[n]; s = new int[n+1]; int max = Integer.MIN_VALUE / 2; int maxLength = 0; int minSum = 0; int minPos = 0; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); s[i+1] = s[i] + a[i]; if (max < s[i+1] - minSum) { max = s[i+1] - minSum; maxLength = i+1 - minPos; } if (minSum > s[i+1]) { minSum = s[i+1]; minPos = i+1; } } var maxSum = new int[n+1]; for (int i = 1; i <= n; i++) maxSum[i] = maxSubarray(i); for (int k = 0; k <= n; k++) { int ans = 0; for (int l = 0; l <= n; l++) ans = Math.max(ans, maxSum[l] + Math.min(l, k) * x); out.print(ans + " "); } out.print('\n'); } out.close(); sc.close(); } int maxSubarray(int k) { int max = Integer.MIN_VALUE / 2; for (int i = k; i <= n; i++) max = Math.max(max, s[i] - s[i-k]); return max; } static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream in) throws Exception { br = new BufferedReader(new InputStreamReader(in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String line = ""; try { line = br.readLine(); } catch (Exception e) { e.printStackTrace(); } return line; } void close() throws Exception { br.close(); } } public static void main(String[] arg) throws Exception { new EDU123C().solve(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 11
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
c33e054bf5462ed978f26ef5242fba58
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for(int ttt = 1; ttt <= T; ttt++) { int n = in.nextInt(); int x = in.nextInt(); int[] a = new int[n+1]; for(int i = 1; i <= n; i++) { a[i] = in.nextInt(); } int[] ps = new int[n+1]; for(int i = 1; i <= n; i++) { ps[i] = ps[i-1]+a[i]; } int[] ms = new int[n+1]; for(int len = 1; len <= n; len++) { int max = Integer.MIN_VALUE; for(int end = len; end <= n; end++) { max = Math.max(max, ps[end]-ps[end-len]); } ms[len] = max; } for(int k = 0; k <= n; k++) { int ans = 0; for(int len = 1; len <= n; len++) { ans = Math.max(ans, ms[len]+Math.min(k, len)*x); } out.print(ans + " "); } out.println(); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int[] readInt(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } long[] readLong(int size) { long[] arr = new long[size]; for(int i = 0; i < size; i++) arr[i] = Long.parseLong(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } return arr; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
3cbc0de86ac37c2a26e8b391257c10cf
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; public class CodeForces { class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader(new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } void shuffle(int a[], int n) { for (int i = 0; i < n; i++) { int t = (int) Math.random() * a.length; int x = a[t]; a[t] = a[i]; a[i] = x; } } long lcm(long a, long b) { long val = a * b; return val / gcd(a, b); } void swap(long a[], long[] b, int i) { long temp = a[i]; a[i] = b[i]; b[i] = temp; } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } long mod = (long) 1e9 + 7, c; FastReader in; PrintWriter o; public static void main(String[] args) throws IOException { new CodeForces().run(); } void run() throws IOException { in = new FastReader(); OutputStream op = System.out; o = new PrintWriter(op); int t; t = 1; t = in.nextInt(); for (int i = 1; i <= t; i++) { helper(); o.println(); } o.flush(); o.close(); } public void helper() { int n=in.nextInt(),x=in.nextInt(); int a[]=new int[n]; for(int j = 0; j <n; j++) a[j]=in.nextInt(); Map<Integer,Long>hm=new HashMap<>();hm.put(0,0l); for(int i=0;i<n;i++) { int len=0;long sum=0; for(int j=i;j<n;j++) { sum+=a[j];len++; if(hm.containsKey(len)) hm.put(len,Math.max(hm.get(len),sum)); else hm.put(len,sum); } } for(int i=0;i<=n;i++) { long ans=0; for(int j=0;j<=n;j++) ans=Math.max(ans,(j<i?hm.get(j)+j*x:hm.get(j)+i*x)); o.print(ans+" "); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
eabc7f73241e268a49d772937ecb351c
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
/*========================================================================== * AUTHOR: RonWonWon * CREATED: 22.02.2022 19:59:12 /*==========================================================================*/ import java.io.*; import java.util.*; public class C { public static void main(String[] args) throws IOException { /* in.ini(); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); */ long startTime = System.nanoTime(); int t = in.nextInt(), T = 1; while(t-->0) { int n = in.nextInt(), x = in.nextInt(); int a[] = in.readArray(n); int idxLeft[] = new int[n]; int idxRight[] = new int[n]; int pre[] = new int[n]; pre[0] = a[0]; for(int i=1;i<n;i++) pre[i] = pre[i-1] + a[i]; Arrays.fill(idxLeft,-1); Arrays.fill(idxRight,-1); for(int i=0;i<n;i++){ int max = 0; for(int j=0;j<i;j++){ if(i==0) break; int cur = pre[i-1]; if(j-1>-1) cur -= pre[j-1]; if(cur>=max){ max = cur; idxLeft[i] = j; } } max = 0; for(int j=i+1;j<n;j++){ if(i==n-1) break; int cur = pre[j] - pre[i]; if(cur>=max){ max = cur; idxRight[i] = j; } } } int ans = 0; for(int i=0;i<n;i++){ int cur = a[i]; int p = i, q = i; if(idxLeft[p]!=-1){ cur += pre[p-1]; if(idxLeft[p]-1>-1) cur -= pre[idxLeft[p]-1]; } if(idxRight[q]!=-1){ cur += pre[idxRight[q]]; cur -= pre[q]; } ans = Math.max(ans,cur); } out.print(ans+" "); for(int i=1;i<=n;i++){ for(int j=i-1;j<n;j++){ int cur = pre[j] + i*x; int p = j-i+1, q = j; if(p-1>-1) cur -= pre[p-1]; if(idxLeft[p]!=-1){ cur += pre[p-1]; if(idxLeft[p]-1>-1) cur -= pre[idxLeft[p]-1]; } if(idxRight[q]!=-1){ cur += pre[idxRight[q]]; cur -= pre[q]; } ans = Math.max(ans,cur); } out.print(ans+" "); } out.println(); //out.println("Case #"+T+": "+ans); T++; } /* startTime = System.nanoTime() - startTime; System.err.println("Time: "+(startTime/1000000)); */ out.flush(); } static FastScanner in = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); static int oo = Integer.MAX_VALUE; static long ooo = Long.MAX_VALUE; static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); void ini() throws FileNotFoundException { br = new BufferedReader(new FileReader("input.txt")); } String next() { while(!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch(IOException e) {} return st.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } long[] readArrayL(int n) { long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = nextLong(); return a; } double nextDouble() { return Double.parseDouble(next()); } double[] readArrayD(int n) { double a[] = new double[n]; for(int i=0;i<n;i++) a[i] = nextDouble(); return a; } } static final Random random = new Random(); static void ruffleSort(int[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n), temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } static void ruffleSortL(long[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n); long temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
4211bde971dbdea802a7293e58be38f6
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
/* I am dead inside Do you like NCT, sKz, BTS? 5 4 3 2 1 Moonwalk Imma knock it down like domino Is this what you want? Is this what you want? Let's ttalkbocky about that :() */ import static java.lang.Math.*; import java.util.*; import java.io.*; public class x1644C { public static void main(String omkar[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int X = Integer.parseInt(st.nextToken()); int[] arr = readArr(N, infile, st); long[] dp = new long[N+1]; Arrays.fill(dp, arr[0]); dp[1] = arr[0]+X; long[] res = new long[N+1]; for(int k=0; k <= N; k++) res[k] = max(res[k], dp[k]); for(int t=1; t < N; t++) { long[] next = new long[N+1]; next[0] = max(0, dp[0])+arr[t]; for(int k=1; k <= N; k++) { next[k] = max(0, dp[k])+arr[t]; next[k] = max(next[k], max(0, dp[k-1])+arr[t]+X); } for(int k=0; k <= N; k++) res[k] = max(res[k], next[k]); dp = next; } for(int i=1; i <= N; i++) res[i] = max(res[i], res[i-1]); for(long x: res) sb.append(x+" "); sb.append("\n"); } System.out.print(sb); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } } /* dp[t][k] = max subarray sum that ends EXACTLY AT t with k additions made dp[t+1][k] -> max(dp[t][k], 0)+arr[t] dp[t+1][k+1] -> max(dp[t][k], 0)+arr[t]+X */
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
d917793a6b77413554bd9febd0d57e41
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
/* I am dead inside Do you like NCT, sKz, BTS? 5 4 3 2 1 Moonwalk Imma knock it down like domino Is this what you want? Is this what you want? Let's ttalkbocky about that :() */ import static java.lang.Math.*; import java.util.*; import java.io.*; public class x1644C { public static void main(String omkar[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int X = Integer.parseInt(st.nextToken()); int[] arr = readArr(N, infile, st); long[] dp = new long[N+1]; Arrays.fill(dp, arr[0]); dp[1] = arr[0]+X; long[] res = new long[N+1]; for(int k=0; k <= N; k++) res[k] = max(res[k], dp[k]); for(int t=1; t < N; t++) { long[] next = new long[N+1]; next[0] = max(0, dp[0])+arr[t]; for(int k=1; k <= N; k++) { next[k] = max(0, dp[k])+arr[t]; next[k] = max(next[k], max(0, dp[k-1])+arr[t]+X); } for(int k=0; k <= t+1; k++) res[k] = max(res[k], next[k]); dp = next; } for(int i=1; i <= N; i++) res[i] = max(res[i], res[i-1]); for(long x: res) sb.append(x+" "); sb.append("\n"); } System.out.print(sb); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } } /* dp[t][k] = max subarray sum that ends EXACTLY AT t with k additions made dp[t+1][k] -> max(dp[t][k], 0)+arr[t] dp[t+1][k+1] -> max(dp[t][k], 0)+arr[t]+X */
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
232f7dbf8dd383b0a3936a7bca883640
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class Main { private static void solve(long[] prefix, int x){ int n = prefix.length - 1; long max = Long.MIN_VALUE; Map<Integer, Long> map = new HashMap<>(); map.put(0, 0L); for(int i = 1; i <= n; i++){ for(int j = i; j <= n; j++){ int len = j - i + 1; if(map.containsKey(len)){ if(prefix[j] - prefix[i-1] > map.get(len)){ map.put(len, prefix[j] - prefix[i-1]); } } else{ map.put(len, prefix[j] - prefix[i-1]); } max = Math.max(prefix[j] - prefix[i-1], max); } } long[] best = new long[n + 1]; best[n] = map.get(n) + (long) x * n; long currMax = map.get(n); for(int i = n - 1; i >= 0; i--){ currMax = Math.max(currMax, map.get(i)); best[i] = currMax + (long) x * i; } for(int i = 1; i <= n; i++){ best[i] = Math.max(best[i-1], Math.max(0, best[i])); } StringBuilder sb = new StringBuilder(); for(int i = 0; i < best.length; i++){ sb.append(best[i]); if(i < best.length - 1){ sb.append(" "); } } System.out.println(sb); } public static void main(String[] args){ MyScanner scanner = new MyScanner(); int num = scanner.nextInt(); for(int i = 0; i < num; i++){ int n = scanner.nextInt(); int x = scanner.nextInt(); int[] arr = new int[n]; long[] prefix = new long[n+1]; for(int j = 0; j < n; j++){ arr[j] = scanner.nextInt(); prefix[j+1] = arr[j] + prefix[j]; } solve(prefix, x); } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
bcf4decd1be1ee709a0c2676ae076464
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; // import java.lang.*; // import java.math.*; public class Main { static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=1000000007; // static long mod=998244353; static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; static long MAXL=Long.MAX_VALUE; static long MINL=Long.MIN_VALUE; static ArrayList<Integer> graph[]; static long fact[]; static int seg[]; static int lazy[];//lazy propagation is used in case of range updates. // static int dp[][]; static long dp[]; public static void main (String[] args) throws java.lang.Exception { // code goes here int t=I(); outer:while(t-->0) { int n=I(),x=I(); long a[]=IL(n); long pre[]=prefix(a); long temp[]=new long[n+1]; temp[0]=kadane(a,n); for(int i=1;i<=n;i++){ long sum=MINL; long tmp=0; for(int j=0;j<n;j++){ int k=j+i-1; if(k>=n)break; if(j==0){ tmp=pre[k]; }else{ tmp=pre[k]-pre[j-1]; } sum=max(sum,tmp); } // if(i==0){ // temp[i]=sum; // }else{ // temp[i]=max(temp[i-1],sum); // } temp[i]=sum; } // printArray(temp); long ans[]=new long[n+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=n;j++){ ans[i]=max(ans[i],temp[j]+1L*min(i,j)*x); } } printArray(ans); } out.close(); } public static long kadane(long a[],int n) //largest sum subarray { long max_sum=Long.MIN_VALUE,max_end=0; for(int i=0;i<n;i++){ max_end+=a[i]; if(max_sum<max_end){max_sum=max_end;} if(max_end<0){max_end=0;} } return max_sum; } public static class pair { int a; int b; public pair(int aa,int bb) { a=aa; b=bb; } } public static class myComp implements Comparator<pair> { //sort in ascending order. public int compare(pair p1,pair p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return -1; else return 1; } // sort in descending order. // public int compare(pair p1,pair p2) // { // if(p1.a==p2.a) // return 0; // else if(p1.a<p2.a) // return 1; // else // return -1; // } } public static void DFS(int s,boolean visited[],int dis) { visited[s]=true; for(int i:graph[s]){ if(!visited[i]){ DFS(i,visited,dis); } } } public static void setGraph(int n,int m) { graph=new ArrayList[n+1]; for(int i=0;i<=n;i++){ graph[i]=new ArrayList<>(); } for(int i=0;i<m;i++){ int u=I(),v=I(); graph[u].add(v); graph[v].add(u); } } //LOWER_BOUND and UPPER_BOUND functions //It returns answer according to zero based indexing. public static int lower_bound(long arr[],long X,int start, int end) //start=0,end=n-1 { if(start>end)return -1; if(arr[arr.length-1]<X)return end; if(arr[0]>X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; // if(arr[mid]==X){ //Returns last index of lower bound value. // if(mid<end && arr[mid+1]==X){ // left=mid+1; // }else{ // return mid; // } // } if(arr[mid]==X){ //Returns first index of lower bound value. if(mid>start && arr[mid-1]==X){ right=mid-1; }else{ return mid; } } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid-1; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid; }else{ left=mid+1; } } } return left; } //It returns answer according to zero based indexing. public static int upper_bound(long arr[],long X,int start,int end) //start=0,end=n-1 { if(arr[0]>=X)return start; if(arr[arr.length-1]<X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; if(arr[mid]==X){ //returns first index of upper bound value. if(mid>start && arr[mid-1]==X){ right=mid-1; }else{ return mid; } } // if(arr[mid]==X){ //returns last index of upper bound value. // if(mid<end && arr[mid+1]==X){ // left=mid+1; // }else{ // return mid; // } // } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid+1; }else{ left=mid+1; } } } return left; } //END // File file = new File("C:\\Users\\Dell\\Desktop\\JAVA\\testcase.txt"); // FileWriter fw = new FileWriter("output.txt"); // BufferedReader br= new BufferedReader(new FileReader(file)); //Segment Tree Code public static void buildTree(int si,int ss,int se) { if(ss==se){ seg[si]=0; return; } int mid=(ss+se)/2; buildTree(2*si+1,ss,mid); buildTree(2*si+2,mid+1,se); seg[si]=max(seg[2*si+1],seg[2*si+2]); } public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b) { int i=0,j=0; while(i<a.size() && j<b.size()){ if(a.get(i)<=b.get(j)){ f.add(a.get(i)); i++; }else{ f.add(b.get(j)); j++; } } while(i<a.size()){ f.add(a.get(i)); i++; } while(j<b.size()){ f.add(b.get(j)); j++; } } public static void update(int si,int ss,int se,int pos,int val) { if(ss==se){ seg[si]=(int)add(seg[si],val); return; } int mid=(ss+se)/2; if(pos<=mid){ update(2*si+1,ss,mid,pos,val); }else{ update(2*si+2,mid+1,se,pos,val); } seg[si]=(int)add(seg[2*si+1],seg[2*si+2]); } public static int query(int si,int ss,int se,int qs,int qe) { if(qs>se || qe<ss)return 0; if(ss>=qs && se<=qe)return seg[si]; int mid=(ss+se)/2; return max(query(2*si+1,ss,mid,qs,qe),query(2*si+2,mid+1,se,qs,qe)); } //Segment Tree Code end //Prefix Function of KMP Algorithm public static int[] KMP(char c[],int n) { int pi[]=new int[n]; for(int i=1;i<n;i++){ int j=pi[i-1]; while(j>0 && c[i]!=c[j]){ j=pi[j-1]; } if(c[i]==c[j])j++; pi[i]=j; } return pi; } public static long nPr(int n,int r) { long ans=divide(fact(n),fact(n-r),mod); return ans; } public static long nCr(int n,int r) { long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod); return ans; } public static boolean isSorted(int a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static boolean isSorted(long a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static int computeXOR(int n) //compute XOR of all numbers between 1 to n. { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } public static int np2(int x) { x--; x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x++; return x; } public static int hp2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static long hp2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static ArrayList<Integer> primeSieve(int n) { ArrayList<Integer> arr=new ArrayList<>(); boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } // Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n); public static class FenwickTree { int farr[]; int n; public FenwickTree(int c) { n=c+1; farr=new int[n]; } // public void update_range(int l,int r,long p) // { // update(l,p); // update(r+1,(-1)*p); // } public void update(int x,int p) { for(;x<n;x+=x&(-x)) { farr[x]+=p; } } public int get(int x) { int ans=0; for(;x>0;x-=x&(-x)) { ans=ans+farr[x]; } return ans; } } //Disjoint Set Union public static class DSU { int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } public static HashMap<Integer,Integer> primeFact(int a) { // HashSet<Long> arr=new HashSet<>(); HashMap<Integer,Integer> hm=new HashMap<>(); int p=0; while(a%2==0){ // arr.add(2L); p++; a=a/2; } hm.put(2,hm.getOrDefault(2,0)+p); for(int i=3;i*i<=a;i+=2){ p=0; while(a%i==0){ // arr.add(i); p++; a=a/i; } hm.put(i,hm.getOrDefault(i,0)+p); } if(a>2){ // arr.add(a); hm.put(a,hm.getOrDefault(a,0)+1); } // return arr; return hm; } public static boolean isInteger(double N) { int X = (int)N; double temp2 = N - X; if (temp2 > 0) { return false; } return true; } public static boolean isPalindrome(String s) { int n=s.length(); for(int i=0;i<=n/2;i++){ if(s.charAt(i)!=s.charAt(n-i-1)){ return false; } } return true; } public static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static long fact(long n) { long fact=1; for(long i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static long fact(int n) { long fact=1; for(int i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void printArray(long a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(int a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(char a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]); } out.println(); } public static void printArray(String a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(boolean a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(pair a[]) { for(pair p:a){ out.println(p.a+"->"+p.b); } } public static void printArray(int a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(long a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(char a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(ArrayList<?> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printMapI(HashMap<?,?> hm){ for(Map.Entry<?,?> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } public static void printMap(HashMap<Long,ArrayList<Integer>> hm){ for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){ out.print(e.getKey()+"->"); ArrayList<Integer> arr=e.getValue(); for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); }out.println(); } } //Modular Arithmetic public static long add(long a,long b) { a+=b; if(a>=mod)a-=mod; return a; } public static long sub(long a,long b) { a-=b; if(a<0)a+=mod; return a; } public static long mul(long a,long b) { return ((a%mod)*(b%mod))%mod; } public static long divide(long a,long b,long m) { a=mul(a,modInverse(b,m)); return a; } public static long modInverse(long a,long m) { int x=0,y=0; own p=new own(x,y); long g=gcdExt(a,m,p); if(g!=1){ out.println("inverse does not exists"); return -1; }else{ long res=((p.a%m)+m)%m; return res; } } public static long gcdExt(long a,long b,own p) { if(b==0){ p.a=1; p.b=0; return a; } int x1=0,y1=0; own p1=new own(x1,y1); long gcd=gcdExt(b,a%b,p1); p.b=p1.a - (a/b) * p1.b; p.a=p1.b; return gcd; } public static long pwr(long m,long n) { long res=1; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m); } n=n>>1; m=(m*m); } return res; } public static long modpwr(long m,long n) { long res=1; m=m%mod; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m)%mod; } n=n>>1; m=(m*m)%mod; } return res; } public static class own { long a; long b; public own(long val,long index) { a=val; b=index; } } //Modular Airthmetic public static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(char[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { char tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } //max & min public static int max(int a,int b){return Math.max(a,b);} public static int min(int a,int b){return Math.min(a,b);} public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));} public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));} public static long max(long a,long b){return Math.max(a,b);} public static long min(long a,long b){return Math.min(a,b);} public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));} public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));} public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} //end public static int[] I(int n) { int a[]=new int[n]; for(int i=0;i<n;i++){ a[i]=I(); } return a; } public static long[] IL(int n) { long a[]=new long[n]; for(int i=0;i<n;i++){ a[i]=L(); } return a; } public static long[] prefix(int a[]) { int n=a.length; long pre[]=new long[n]; pre[0]=a[0]; for(int i=1;i<n;i++){ pre[i]=pre[i-1]+a[i]; } return pre; } public static long[] prefix(long a[]) { int n=a.length; long pre[]=new long[n]; pre[0]=a[0]; for(int i=1;i<n;i++){ pre[i]=pre[i-1]+a[i]; } return pre; } public static int I(){return sc.I();} public static long L(){return sc.L();} public static String S(){return sc.S();} public static double D(){return sc.D();} } class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try { st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int I(){ return Integer.parseInt(next()); } long L(){ return Long.parseLong(next()); } double D(){ return Double.parseDouble(next()); } String S(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
595717bf072617a9fda48150bfa792d1
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); // int a = 1; int t; t = in.nextInt(); //t = 1; while (t > 0) { // out.print("Case #"+(a++)+": "); solver.call(in,out); t--; } out.close(); } static class TaskA { Map<String, Integer> map; int n, x; int[][] dp = new int[5001][5001]; int[] arr = new int[5001]; public void call(InputReader in, PrintWriter out) { n = in.nextInt(); x = in.nextInt(); map = new HashMap<>(); for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } for (int i = 0; i <= n; i++) { Arrays.fill(dp[i], 0); } int max, sum, ans = 0; for (int i = 0; i < n; i++) { max = 0; sum = arr[i]; max = Math.max(max, sum); for (int j = i - 1; j >= 0; j--) { sum += arr[j]; max = Math.max(max, sum); } dp[0][i] = max; ans = Math.max(dp[0][i], ans); } out.print(ans+" "); for (int i = 1; i <= n; i++) { ans = 0; for (int j = 0; j < n; j++) { if(j==0){ dp[i][j] = Math.max(dp[i][j], x + arr[j]); } else { dp[i][j] = Math.max(dp[i][j], x + arr[j] + dp[i - 1][j - 1]); } ans = Math.max(dp[i][j],ans); } out.print(ans+" "); } out.println(); } } static int gcd(int a, int b ) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class answer implements Comparable<answer>{ int a, b; public answer(int a, int b, int c) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return Integer.compare(this.b, o.b); } } static class answer1 implements Comparable<answer1>{ int a, b, c; public answer1(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } @Override public int compareTo(answer1 o) { return (o.a - this.a); } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (Long i:a) l.add(i); l.sort(Collections.reverseOrder()); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static final Random random=new Random(); static void shuffleSort(int[] a) { int n = a.length; for (int i=0; i<n; i++) { int oi= random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
899a2b7dc4f59d8635fa03b9fcedb80a
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; public class code { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t= sc.nextInt(); while(t-->0){ solve(); } } static void solve() { int n=sc.nextInt(); long x=sc.nextInt(); long a[]=new long[n]; for (int i = 0; i < n; i++) { a[i]=sc.nextInt(); } long[] pre=new long[n+1]; pre[0]=0; for (int i = 0; i < n; i++) { pre[i+1]=pre[i]+a[i]; } long[] sum=new long[n+1]; // long primmax=Integer.MIN_VALUE; // long si=0; for (int i = 0; i <=n; i++) { long temp=Integer.MIN_VALUE; for (int j = 0; j+i<=n; j++) { temp=Math.max(pre[j+i]-pre[j],temp); } sum[i]=temp; // // if(temp>=primmax){ // primmax=Math.max(primmax,temp); // si=i; // } } // if(primmax>=0){ // for (int i = 0; i <=n; i++) { // System.out.print(primmax+(x*i)+" "); // } // } for (int i = 0; i <=n; i++) { long ans=Integer.MIN_VALUE; for (int j = 0; j <=n; j++) { ans= Math.max(ans,sum[j]+(Math.min(i,j))*x); } System.out.print(ans+" "); } System.out.println(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
245954499733f79f9b74758503d92eef
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class IncreaseSubarraySums { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pr=new PrintWriter(System.out); int t=Integer.parseInt(br.readLine()); while(t!=0){ solve(br,pr); t--; } pr.flush(); pr.close(); } public static void solve(BufferedReader br,PrintWriter pr) throws IOException{ String[] temp=br.readLine().split(" "); int n=Integer.parseInt(temp[0]); int x=Integer.parseInt(temp[1]); int[] nums=new int[n]; temp=br.readLine().split(" "); for(int i=0;i<n;i++){ nums[i]=Integer.parseInt(temp[i]); } long[] preSum=new long[n+1]; for(int i=0;i<n;i++){ preSum[i+1]=preSum[i]+nums[i]; } long[] max=new long[n+1]; for(int l=1;l<=n;l++){ long maxSum=Long.MIN_VALUE; for(int i=0;i+l-1<n;i++){ int left=i; int right=left+l-1; maxSum=Math.max(maxSum,preSum[right+1]-preSum[left]); } max[l]=maxSum; } for(int k=0;k<=n;k++){ long res=0; for(int l=1;l<=n;l++){ res=Math.max(res,max[l]+(long)x*Math.min(k,l)); } pr.print(res+" "); } pr.print('\n'); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
9f9c042a61fc50d5afa7bf22326db701
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { static Scanner scn = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int t = 1; t = scn.nextInt(); for(int tests = 0; tests < t; tests++) solve(); out.close(); } public static void solve() { int n = scn.nextInt(); long x = scn.nextLong(); long[] a = new long[n]; for(int i = 0; i < n; i++) a[i] = scn.nextLong(); long[] sub = new long[n + 1]; Arrays.fill(sub, Integer.MIN_VALUE / 2); long maxSum = 0; int maxLength = 1; for(int i = 0; i < n; i++) { long s = 0; for(int j = i; j < n; j++) { int size = (j - i + 1); s += a[j]; sub[size] = max(sub[size], s); } } long[] res = new long[n + 1]; for(int i = 0; i <= n; i++) { long ans = 0; for(int j = 0; j <= n; j++) { // 'i' is the distinct position, that i can fill // 'j' is the size of subarrays ans = max(ans, (sub[j] + (min(j, i) * (long) x))); } res[i] = max(0, ans); } for(long i : res) out.print(i + " "); out.println(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
11df8de551e2d5a5d79c54395ebf9ab5
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.lang.Math; import java.util.Scanner; public class Main { static final int N=5010; static Scanner cin=new Scanner(System.in); static int T; static int n, x; static int[] a=new int[N]; static long[][] f=new long[N][N]; static long[] ans=new long[N]; public static void main(String[] args) { T=cin.nextInt(); for(int C=1;C<=T;C++) { n=cin.nextInt(); x=cin.nextInt(); for(int i=1;i<=n;i++) a[i]=cin.nextInt(); init(); for(int i=1;i<=n;i++) for(int j=0;j<=n;j++) { f[i][j]=Math.max(0L, f[i-1][j])+a[i]; if(j>0) f[i][j]=Math.max(f[i][j], Math.max(0L, f[i-1][j-1])+a[i]+x); ans[j]=Math.max(ans[j], f[i][j]); } for(int i=0;i<=n;i++) System.out.print(ans[i]+" "); System.out.println(); } } static void init() { for(int i=0;i<=n;i++) { ans[i]=0; for(int j=0;j<=n;j++) f[i][j]=0; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
bfa414c710a0126d61a49fb3092660f8
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.lang.Math; import java.util.Scanner; public class Main { static final int N=5010; static Scanner cin=new Scanner(System.in); static int T; static int n, x; static int[] a=new int[N]; static long[][] f=new long[N][N]; static long[] ans=new long[N]; public static void main(String[] args) { T=cin.nextInt(); for(int C=1;C<=T;C++) { n=cin.nextInt(); x=cin.nextInt(); for(int i=1;i<=n;i++) a[i]=cin.nextInt(); init(); for(int i=1;i<=n;i++) for(int j=0;j<=n;j++) { f[i][j]=Math.max(0L, f[i-1][j])+a[i]; if(j>0) f[i][j]=Math.max(f[i][j], Math.max(0L, f[i-1][j-1])+a[i]+x); ans[j]=Math.max(ans[j], f[i][j]); } for(int i=0;i<=n;i++) System.out.print(ans[i]+" "); System.out.println(); } } public static void init() { for(int i=0;i<=n;i++) { ans[i]=0; for(int j=0;j<=n;j++) f[i][j]=0; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
17ced54d63bfcc55d0beedc4dde3274e
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.lang.Math; import java.util.Scanner; public class Main { static final int N=5010; static Scanner cin=new Scanner(System.in); static int T; static int n, x; static int[] a=new int[N]; static long[][] f=new long[N][N]; static long[] ans=new long[N]; public static void main(String[] args) { T=cin.nextInt(); for(int C=1;C<=T;C++) { n=cin.nextInt(); x=cin.nextInt(); for(int i=1;i<=n;i++) a[i]=cin.nextInt(); init(); for(int i=1;i<=n;i++) for(int j=0;j<=n;j++) { f[i][j]=Math.max(0L, f[i-1][j])+a[i]; if(j>0) f[i][j]=Math.max(f[i][j], Math.max(0L, f[i-1][j-1])+a[i]+x); ans[j]=Math.max(ans[j], f[i][j]); } for(int i=0;i<=n;i++) System.out.print(ans[i]+" "); System.out.println(); } } public static void init() { for(int i=0;i<=n;i++) { ans[i]=0; for(int j=0;j<=n;j++) f[i][j]=0; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
58e3b395c12a6f2bc3ec3afec7c5248b
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class Main { static long cr[][]=new long[1001][1001]; //static double EPS = 1e-7; static long mod=1000000007; static long val=0; public static void main(String[] args) { FScanner sc = new FScanner(); //Arrays.fill(prime, true); //sieve(); //ncr(); int t=sc.nextInt(); StringBuilder sb = new StringBuilder(); while(t-->0) { int n=sc.nextInt(); long x=sc.nextInt(); long arr[]=new long[n+1]; for(int i=1;i<=n;i++) { arr[i]=sc.nextInt(); arr[i]=arr[i]+arr[i-1]; } long res[]=new long[n]; Arrays.fill(res,Long.MIN_VALUE); for(int i=1;i<=n;i++) { for(int j=i;j>0;j--) { res[i-j]=Math.max(res[i-j], arr[i]-arr[j-1]); } } long max=Long.MIN_VALUE;int ind=0; for(int i=0;i<n;i++) { if(max<=res[i]) { max=res[i]; ind=i+1; } } long val=max; max=Math.max(max,0); sb.append(max+" "); for(int i=0;i<n;i++) { for(int j=i;j<n;j++) {max=Math.max(max,Math.max(res[j]+(i+1)*x,val+x*Math.min(ind,i+1)));} sb.append(max+" "); } sb.append("\n"); } System.out.println(sb.toString()); } public static int check(String s1,String s2) { int diff=0; for(int i=0;i<7;i++) { if(s1.charAt(i)!=s2.charAt(i)) diff++; if(s1.charAt(i)=='0' && s2.charAt(i)=='1') return -1; } return diff; } public static long gcd(long a,long b) { return b==0 ? a:gcd(b,a%b); } /* public static void sieve() { prime[0]=prime[1]=false; int n=1000000; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } */ public static void ncr() { cr[0][0]=1; for(int i=1;i<=1000;i++) { cr[i][0]=1; for(int j=1;j<i;j++) { cr[i][j]=(cr[i-1][j-1]+cr[i-1][j])%mod; } cr[i][i]=1; } } } class pair //implements Comparable<pair> { int a;int b; pair(int a,int b) { this.b=b; this.a=a; } } class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(next()); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
87976dbf7d3ad3c268b8747461d78279
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class Main { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(), x = sc.ni(); int[] arr = sc.na(n); int[] pre = new int[n]; pre[0] = arr[0]; for(int i = 1; i < n; i++) pre[i] = pre[i-1]+arr[i]; int[] hash = new int[n+1]; Arrays.fill(hash, imi); for(int i = 0; i < n; i++) { for(int j = i; j < n; j++) { int sum = i==0?pre[j]:pre[j]-pre[i-1]; int elem = j-i+1; hash[elem] = Math.max(hash[elem], sum); } } for(int i = 0; i <= n; i++) { int best = 0; for(int j = 0; j <= n; j++) { if(j < i) { best = Math.max(best, hash[j]+(x*j)); } else best = Math.max(best, hash[j]+(x*i)); } w.pr(best+" "); } w.pl(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
f749a86549df00ce5fd4d1a5a8486a50
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; import java.util.function.BinaryOperator; public class Main { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(), x = sc.ni(); int[] arr = sc.na(n); int[] pre = new int[n]; pre[0] = arr[0]; for(int i = 1; i < n; i++) { pre[i]+=pre[i-1]+arr[i]; } long[] hashMax = new long[n+1]; long[] hashPrev = new long[n+1]; Arrays.fill(hashMax, imi); Arrays.fill(hashPrev, imi); for(int i = 0; i < n; i++) { for(int j = i; j < n; j++) { int sum = i==0?pre[j]:pre[j]-pre[i-1]; int elems = j-i+1; hashMax[elems] = Math.max(hashMax[elems], sum); hashPrev[elems] = Math.max(hashPrev[elems], sum+(elems*(long)x)); } } long[] ans = new long[n+1]; ST st = new ST(hashMax, ST.Operation.MAX); ST stPrev = new ST(hashPrev, ST.Operation.MAX); for(int i = 0; i <= n; i++) { long curr = 0; // less if(i!=0) { long max = stPrev.query(0, i-1); curr = Math.max(curr, max); } // more long max = st.query(i, n); curr = Math.max(curr, max + ((long)x*i)); ans[i] = curr; } for(long l: ans) { w.pr(l+" "); } w.pl(); } /** sparseTable(long[], SparseTable.Operation)*/ // region SparseTable static class ST { private int n, P; private int[] log2; private long[][] dp; private int[][] it; public enum Operation { MIN, MAX, SUM, MULT, GCD } private Operation op; private BinaryOperator<Long> sumFn = (a, b) -> a + b; private BinaryOperator<Long> minFn = (a, b) -> Math.min(a, b); private BinaryOperator<Long> maxFn = (a, b) -> Math.max(a, b); private BinaryOperator<Long> multFn = (a, b) -> a * b; private BinaryOperator<Long> gcdFn = (a, b) -> { long gcd = a; while (b != 0) { gcd = b; b = a % b; a = gcd; } return Math.abs(gcd); }; public ST(long[] values, Operation op) { this.op = op; init(values); } private void init(long[] v) { n = v.length; P = (int) (Math.log(n) / Math.log(2)); dp = new long[P + 1][n]; it = new int[P + 1][n]; for (int i = 0; i < n; i++) { dp[0][i] = v[i]; it[0][i] = i; } log2 = new int[n + 1]; for (int i = 2; i <= n; i++) { log2[i] = log2[i / 2] + 1; } for (int i = 1; i <= P; i++) { for (int j = 0; j + (1 << i) <= n; j++) { long leftInterval = dp[i - 1][j]; long rightInterval = dp[i - 1][j + (1 << (i - 1))]; if (op == Operation.MIN) { dp[i][j] = minFn.apply(leftInterval, rightInterval); if (leftInterval <= rightInterval) { it[i][j] = it[i - 1][j]; } else { it[i][j] = it[i - 1][j + (1 << (i - 1))]; } } else if (op == Operation.MAX) { dp[i][j] = maxFn.apply(leftInterval, rightInterval); if (leftInterval >= rightInterval) { it[i][j] = it[i - 1][j]; } else { it[i][j] = it[i - 1][j + (1 << (i - 1))]; } } else if (op == Operation.SUM) { dp[i][j] = sumFn.apply(leftInterval, rightInterval); } else if (op == Operation.MULT) { dp[i][j] = multFn.apply(leftInterval, rightInterval); } else if (op == Operation.GCD) { dp[i][j] = gcdFn.apply(leftInterval, rightInterval); } } } } public long query(int l, int r) { if (op == Operation.MIN) { return query(l, r, minFn); } else if (op == Operation.MAX) { return query(l, r, maxFn); } else if (op == Operation.GCD) { return query(l, r, gcdFn); } if (op == Operation.SUM) { return sumQuery(l, r); } else { return multQuery(l, r); } } public int queryIndex(int l, int r) { if (op == Operation.MIN) { return minQueryIndex(l, r); } else if (op == Operation.MAX) { return maxQueryIndex(l, r); } throw new UnsupportedOperationException( "Operation type: " + op + " doesn't support index queries :/"); } private int minQueryIndex(int l, int r) { int len = r - l + 1; int p = log2[len]; long leftInterval = dp[p][l]; long rightInterval = dp[p][r - (1 << p) + 1]; if (leftInterval <= rightInterval) { return it[p][l]; } else { return it[p][r - (1 << p) + 1]; } } private int maxQueryIndex(int l, int r) { int len = r - l + 1; int p = log2[len]; long leftInterval = dp[p][l]; long rightInterval = dp[p][r - (1 << p) + 1]; if (leftInterval >= rightInterval) { return it[p][l]; } else { return it[p][r - (1 << p) + 1]; } } private long sumQuery(int l, int r) { long sum = 0; for (int p = log2[r - l + 1]; l <= r; p = log2[r - l + 1]) { sum += dp[p][l]; l += (1 << p); } return sum; } private long multQuery(int l, int r) { long result = 1; for (int p = log2[r - l + 1]; l <= r; p = log2[r - l + 1]) { result *= dp[p][l]; l += (1 << p); } return result; } private long query(int l, int r, BinaryOperator<Long> fn) { int len = r - l + 1; int p = log2[len]; return fn.apply(dp[p][l], dp[p][r - (1 << p) + 1]); } } // endregion }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
ae13810a721e531bcac7d9de93191009
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class IncreaseSubarraySums { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); int[] arr = new int[n]; int[] prefix = new int[n + 1]; int[] max = new int[n + 1]; st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(st.nextToken()); if (i != 0) prefix[i + 1] = prefix[i] + arr[i]; else prefix[i + 1] = arr[i]; } for (int i = 1; i < n + 1; i++) { max[i] = Integer.MIN_VALUE; } for (int j = 0; j <= n; j++) { for (int k = j + 1; k <= n; k++) { max[k - j] = Math.max(prefix[k] - prefix[j], max[k - j]); } } for (int i = 0; i <= n; i++) { int ans = 0; for (int j = 0; j <= n; j++) { int min = Math.min(i, j); ans = Math.max(ans, max[j] + x*min); } pw.print(ans + " "); } pw.println(); } pw.close(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
8c5275fa4abf94d41ea889ee2a5f2e59
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class IncreaseSubarraySums { public static void main(String[] args) throws InterruptedException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); int[] arr = new int[n]; int[] prefix = new int[n + 1]; int max[] = new int[n + 1]; st = new StringTokenizer(br.readLine()); prefix[0] = 0; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(st.nextToken()); if (i != 0) prefix[i + 1] = prefix[i] + arr[i]; else prefix[i + 1] = arr[i]; } for (int i = 0; i < n + 1; i++) { max[i] = Integer.MIN_VALUE; } max[0] = 0; for (int j = 0; j <= n; j++) { for (int k = j + 1; k <= n; k++) { max[k - j] = Math.max(prefix[k] - prefix[j] + x * (k - j), max[k - j]); } } for (int i = 0; i <= n; i++) { int ans = 0; for (int j = 0; j <= n; j++) { int min = Math.min(j, i); if (i - j < 0) ans = Math.max(ans, max[j] + (i - j)*x); else ans = Math.max(ans, max[j]); } pw.print(ans + " "); } pw.println(); } pw.close(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
7e5a75180f2e913ecc9b27b1a7019c6b
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) throws IOException { int N = 5100; long[] lenmax = new long[N]; int[] a = new int[N]; in.nextToken(); int t = (int) in.nval; while (t-- > 0) { in.nextToken(); int n = (int) in.nval; in.nextToken(); int x = (int) in.nval; for (int i = 1; i <= n; i++) { in.nextToken(); a[i] = a[i - 1] + (int) in.nval; } for (int len = 0; len <= n; len++) { long max = -9999999999999l; int idx = -1; for (int i = 0, j = i + len; j <= n; i++, j++) { if (a[j] - a[i] > max) { max = a[j] - a[i]; idx = i; } } lenmax[len] = max; } // out.println(q); long max = 0; for (int k = 0; k <= n; k++) { for (int i = 0; i <= n; i++) { max = Math.max(max, lenmax[i] + x * Math.min(i, k)); //out.println(lenmax[i]+"---"+ i + " " + k + " " + Math.min(i, k)); } out.print(max + " "); } out.println(); out.flush(); } out.close(); } public static class Point implements Comparable<Point> { int cot; int len; long val; public String toString() { return this.cot + "-" + this.val; } public Point(int cot, int len, long val) { this.cot = cot; this.len = len; this.val = val; } @Override public int compareTo(Point o) { return this.cot - o.cot; } } // static Scanner in = new Scanner(System.in); // static BufferedReader in = new BufferedReader(new // InputStreamReader(System.in)); static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
7cdaa0d7080cd6ab59d92e3481557c8f
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
//created by Toufique on 15/07/2022 import java.io.*; import java.util.*; public class IncreaseSubarraySums { static HashMap<Integer,Long> lists; public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = in.nextInt(); for (int tt = 0; tt < t; tt++) { int n = in.nextInt(); long x = in.nextLong(); long[] a = new long[n + 2]; for (int i = 1; i <= n; i++) a[i] = in.nextInt(); ArrayList<Long> ans = solve(n, x, a); for (long ii: ans) pw.print(ii + " "); pw.println(); } pw.close(); } static ArrayList<Long> solve(int n, long k, long[] a) { ArrayList<Long> ans = new ArrayList<>(); for (int i = 1; i <= n; i++) a[i] += a[i - 1]; generateSubArray(n, a); for (int i = 0; i <= n; i++) { long res = 0; for (int j = 0; j <= n; j++) { long temp = lists.get(j) + Math.min(i, j) * k; res = Math.max(res, temp); } ans.add(res); } return ans; } static void generateSubArray(int n, long[] a) { lists = new HashMap<>(); lists.put(0, 0L); for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { long temp = a[j] - a[i - 1]; int diff = j - i + 1; long get = lists.getOrDefault(diff, Long.MIN_VALUE); lists.put(diff, Math.max(get, temp)); } } // debug(lists); } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
a6d29e6d021ed0bad78268ee59515f1d
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.math.*; import java.util.* ; import java.io.* ; @SuppressWarnings("unused") //Scanner s = new Scanner(new File("input.txt")); //s.close(); //PrintWriter writer = new PrintWriter("output.txt"); //writer.close(); public class cf { static long gcd(long a, long b){if (b == 0) {return a;}return gcd(b, a % b);} public static void main(String[] args)throws Exception { FastReader in = new FastReader() ; // FastIO in = new FastIO(System.in) ; StringBuilder op = new StringBuilder() ; int T = in.nextInt() ; // int T=1 ; for(int tt=0 ; tt<T ; tt++){ //CODE : int n = in.nextInt() ; long x = in.nextInt() ; int a[] = in.readArray(n) ; int maxsum[] = new int[n+1] ; Arrays.fill(maxsum, Integer.MIN_VALUE); int ps[] = new int[n] ; ps[0]=a[0] ; for(int i=1 ; i<n ; i++) { ps[i] = ps[i-1]+a[i] ; } for(int i=0 ; i<n ; i++) { for(int j=i ; j<n ; j++) { //sum from i to j : int segsum=0 ; if(i==0)segsum = ps[j] ; else segsum=ps[j]-ps[i-1] ; maxsum[j-i+1] = Math.max(maxsum[j-i+1], segsum) ; } } for(int k=0 ; k<=n ; k++) { long ans=0 ; for(int i=1 ; i<=n ; i++) { ans = Math.max(ans, maxsum[i]+Math.min(k, i)*x); }op.append(ans+" ") ; } op.append("\n") ; } System.out.println(op.toString()) ; } static class pair implements Comparable<pair> { int first, second; pair(int first, int second) { this.first = first; this.second = second; } @Override public int compareTo(pair other) { return this.first - other.first; } } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); }; String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
ee2520fffd0e8a18cb251cabf66a0eba
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.Scanner; public class C_Increase_Subarray_Sums3 { static Scanner in = new Scanner(System.in); static int testCases, n, x; static int a[]; static StringBuilder ans = new StringBuilder(); static void solve() { long dp[] = new long[n + 1]; for(int i = 0; i <= n; ++i) { dp[i] = Integer.MIN_VALUE; } for(int i = 0; i < n; ++i) { long sum = 0; for(int j = i; j < n; ++j) { sum += a[j]; dp[j - i + 1] = Math.max(sum, dp[j - i + 1]); } } //printArray(dp); for(int i = 0; i <= n; ++i) { long ans_temporary = 0; for(int j = 0; j <= n; ++j) { ans_temporary = Math.max(ans_temporary, Math.min(i, j) * x + dp[j]); } ans.append(ans_temporary).append(" "); } ans.append("\n"); } public static void main(String [] priya) { testCases = in.nextInt(); for(int t = 0; t < testCases; ++t) { n = in.nextInt(); x = in.nextInt(); a = new int[n + 1]; for(int i = 0; i < n; ++i) { a[i] = in.nextInt(); } solve(); } System.out.print(ans.toString().trim()); } static void printArray(long a[]) { for(long i : a) { System.out.print(i + " "); } System.out.println(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
ba2eab9411f7fa9c8a2e34990982912f
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.Scanner; public class C_Increase_Subarray_Sum1s { static Scanner in = new Scanner(System.in); static int testCases, n, x; static int a[]; static void solve() { long dp[] = new long[n + 1]; for (int i = 0; i <= n; ++i) { dp[i] = Integer.MIN_VALUE; } for (int i = 0; i < n; ++i) { long sum = 0; for (int j = i; j < n; ++j) { sum += a[j]; dp[j - i + 1] = Math.max(dp[j - i + 1], sum); } //printArray(dp); } for (int i = 0; i <= n; ++i) { long ans1 = 0; for (int j = 0; j <= n; ++j) { ans1 = Math.max(ans1, Math.min(i, j) * x + dp[j]); } System.out.print(ans1 + " "); } System.out.println(); } public static void main(String[] priya) { testCases = in.nextInt(); for (int t = 0; t < testCases; ++t) { n = in.nextInt(); x = in.nextInt(); a = new int[n + 1]; for (int i = 0; i < n; ++i) { a[i] = in.nextInt(); } solve(); } } static void printArray(long a[]) { for (long i : a) { System.out.print(i + " "); } System.out.println(); } } /* 1 4 2 4 1 3 2 */ /* 1 3 5 -2 -7 -1 */ /* 1 10 2 -6 -1 -2 4 -6 -1 -4 4 -5 -4 */
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
b4975ae664ddf5d90990e90073cb6c25
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.Scanner; public class C_Increase_Subarray_Sums { static Scanner in = new Scanner(System.in); static int testCases, n, x; static int a[]; static StringBuilder ans = new StringBuilder(); static void solve() { long dp[] = new long[n + 1]; for (int i = 0; i <= n; ++i) { dp[i] = Integer.MIN_VALUE; } for (int i = 0; i < n; ++i) { long sum = 0; for (int j = i; j < n; ++j) { sum += a[j]; dp[j - i + 1] = Math.max(dp[j - i + 1], sum); } // printArray(dp); } for (int i = 0; i <= n; ++i) { long ans1 = 0; for (int j = 0; j <= n; ++j) { ans1 = Math.max(ans1, Math.min(i, j) * x + dp[j]); } ans.append(ans1).append(" "); } ans.append("\n"); } public static void main(String[] priya) { testCases = in.nextInt(); for (int t = 0; t < testCases; ++t) { n = in.nextInt(); x = in.nextInt(); a = new int[n + 1]; for (int i = 0; i < n; ++i) { a[i] = in.nextInt(); } solve(); } System.out.print(ans.toString()); } static void printArray(long dp[]) { for (long i : dp) { System.out.print(i + " "); } System.out.println(); } } /* 1 10 2 -6 -1 -2 4 -6 -1 -4 4 -5 -4 */
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
3e89058fdeae43701c57c3f6cd5543e3
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.util.stream.Collectors; import java.io.*; import java.math.*; public class R774_C{ public static FastScanner sc; public static PrintWriter pw; public static StringBuilder sb; public static int MOD= 1000000007; public static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void printList(List<Long> al) { System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" "))); } public static void printList(Deque<Long> al) { System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" "))); } public static void printArr(long[] arr) { System.out.println(Arrays.toString(arr).trim().replace("[", "").replace("]","").replace(","," ")); } public static long gcd(long a, long b) { if(b==0) return a; return gcd(b,a%b); } public static long lcm(long a, long b) { return((a*b)/gcd(a,b)); } public static void decreasingOrder(ArrayList<Long> al) { Comparator<Long> c = Collections.reverseOrder(); Collections.sort(al,c); } public static boolean[] sieveOfEratosthenes(int n) { boolean isPrime[] = new boolean[n+1]; Arrays.fill(isPrime, true); isPrime[0]=false; isPrime[1]=false; for(int i=2;i*i<n;i++) { for(int j=2*i;j<n;j+=i) { isPrime[j]=false; } } return isPrime; } public static long nCr(long N, long R) { double result=1; for(int i=1;i<=R;i++) result=((result*(N-R+i))/i); return (long) result; } public static long fastPow(long a, long b, int n) { long result=1; while(b>0) { if((b&1)==1) result=(result*a %n)%n; a=(a%n * a%n)%n; b>>=1; } return result; } public static int BinarySearch(long[] arr, long key) { int low=0; int high=arr.length-1; while(low<=high) { int mid=(low+high)/2; if(arr[mid]==key) return mid; else if(arr[mid]>key) high=mid-1; else low=mid+1; } return low; //High=low-1 } public static void solve(int t) { int n=sc.nextInt(); long k=sc.nextLong(); long[] arr = new long[n]; for(int i=0;i<n;i++) arr[i]=sc.nextLong(); long[] maxSubArraySize = new long[n+1]; Arrays.fill(maxSubArraySize, Long.MIN_VALUE); long overAllMax=0; for(int i=0;i<n;i++) { long sum=0; for(int j=i;j<n;j++) { sum+=arr[j]; overAllMax=Math.max(overAllMax, sum); maxSubArraySize[j-i+1]=Math.max(maxSubArraySize[j-i+1], sum); } } long arr2[]= new long[n+1]; arr2[n]=maxSubArraySize[n]; for(int i=n-1;i>=0;i--) { arr2[i]=Math.max(arr2[i+1],maxSubArraySize[i]); } // System.out.println("mss"+" "); // printArr(maxSubArraySize); // System.out.println("arr2"+" "); // printArr(arr2); long ans[] = new long[n+1]; for(int i=0;i<=n;i++) { ans[i]=Math.max(overAllMax, arr2[i]+ (i*k)); overAllMax=ans[i]; } printArr(ans); } public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(new OutputStreamWriter(System.out)); sb= new StringBuilder(""); int t=sc.nextInt(); for(int i=1;i<=t;i++) solve(i); System.out.println(sb); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
7db0fc273bcbab388307190fb5f90716
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class incsubarraysum{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); while(T-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine()); int N = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] nums = new int[N]; for(int i = 0;i < N;i++) { nums[i] = Integer.parseInt(st.nextToken()); } long[] best = new long[N+1]; Arrays.fill(best, -Long.MIN_VALUE); best[0] = 0; for(int i = 0;i < N;i++) { long sum = 0; for(int j = i;j < N;j++) { sum += nums[j]; best[j-i+1] = Math.max(best[j-i+1], sum); } } // System.out.println(Arrays.toString(best)); for(int k = 0;k <= N;k++) { long max = Long.MIN_VALUE; for(int i = 0;i <= N;i++) { max = Math.max(max, best[i] + Math.min(k, i) * (long) x); } System.out.print(max + " "); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
b9b79da006b7150a71188e225659d923
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
//import java.io.IOException; import java.io.*; import java.util.*; public class IncreaseSubarraySums { static InputReader inputReader=new InputReader(System.in); static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static List<LinkedHashSet<Integer>>answer=new ArrayList<>(); static void solve() throws IOException { int n=inputReader.nextInt(); long x=inputReader.nextInt(); long arr[]=new long[n]; for (int i=0;i<n;i++) { arr[i]=inputReader.nextLong(); } long prefix[]=new long[n]; prefix[0]=arr[0]; for (int i=1;i<n;i++) { prefix[i]=prefix[i-1]+arr[i]; } long max=0; int maxlen=0; long maxsum[]=new long[n+1]; for (int l=1;l<=n;l++) { long currmax=Long.MIN_VALUE; for (int i=0;i<=n-l;i++) { int j = i + l - 1; long sum = prefix[j] - (((i - 1) >= 0) ? prefix[i - 1] : 0); if (sum>max) { max=sum; maxlen=l; } currmax=Math.max(currmax,sum); } maxsum[l]=currmax; } long dp[]=new long[n+1]; for (int i=0;i<=n;i++) { dp[i]=max; } for (int i=0;i<=n;i++) { int xicanadd=Math.min(i,maxlen); dp[i]=Math.max(dp[i]+xicanadd*x,maxsum[i]+x*i); for (int j=0;j<=n;j++) { long sumofarray=maxsum[j]+Math.min(j,i)*x; dp[i]=Math.max(dp[i],sumofarray); } } for (long ele:dp) { out.print(ele+" "); } out.println(); } static PrintWriter out=new PrintWriter((System.out)); static void SortDec(long arr[]) { List<Long>list=new ArrayList<>(); for(long ele:arr) { list.add(ele); } Collections.sort(list,Collections.reverseOrder()); for (int i=0;i<list.size();i++) { arr[i]=list.get(i); } } static void Sort(long arr[]) { List<Long>list=new ArrayList<>(); for(long ele:arr) { list.add(ele); } Collections.sort(list); for (int i=0;i<list.size();i++) { arr[i]=list.get(i); } } public static void main(String args[])throws IOException { int t=inputReader.nextInt(); while (t-->0) { solve();} long s = System.currentTimeMillis(); // out.println(System.currentTimeMillis()-s+"ms"); out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
0b3304c4209c52e39d01c44260ac2156
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { new C().run(); } BufferedReader br; PrintWriter out; long mod = (long) (1e9 + 7), inf = (long) (3e18); class pair { int F, S; pair(int f, int s) { F = f; S = s; } } void solve() { int t = ni(); while(t-- > 0) { //TODO: int n= ni(); long x= nl(); long[] a= new long[n]; for(int i=0; i<n; i++){ a[i]=nl(); } long[] dp= new long[n+1]; Arrays.fill(dp, -inf); for(int i = 0; i < n; i++) { long val = 0; for(int j = i; j < n; j++) { val += a[j]; dp[j - i + 1] = Math.max(dp[j - i + 1], val); } } for(int i = 0; i <= n; i++) { long ans = 0; for(int j = 1; j <= n; j++) { ans = Math.max(ans, dp[j] + (Math.min(i, j) * x)); } out.print(ans + " "); } out.println(); } } long maxSum(long arr[], int n, int k) { long res = 0; for (int i=0; i<k; i++) res += arr[i]; long curr_sum = res; for (int i=k; i<n; i++) { curr_sum += arr[i] - arr[i-k]; res = Math.max(res, curr_sum); } return res; } long maxSubArraySum(long a[]) { int size = a.length; long max_so_far = Long.MIN_VALUE, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } // -------- I/O Template ------------- char nc() { return ns().charAt(0); } String nLine() { try { return br.readLine(); } catch(IOException e) { return "-1"; } } double nd() { return Double.parseDouble(ns()); } long nl() { return Long.parseLong(ns()); } int ni() { return Integer.parseInt(ns()); } int[] na(int n) { int a[] = new int[n]; for(int i = 0; i < n; i++) a[i] = ni(); return a; } StringTokenizer ip; String ns() { if(ip == null || !ip.hasMoreTokens()) { try { ip = new StringTokenizer(br.readLine()); if(ip == null || !ip.hasMoreTokens()) ip = new StringTokenizer(br.readLine()); } catch(IOException e) { throw new InputMismatchException(); } } return ip.nextToken(); } void run() { try { if (System.getProperty("ONLINE_JUDGE") == null) { br = new BufferedReader(new FileReader("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/input.txt")); out = new PrintWriter("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/output.txt"); } else { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } catch (FileNotFoundException e) { System.out.println(e); } solve(); out.flush(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
dbc8151abdd6a51300a4a2a0a0d866dd
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; /** * @author Naitik * */ public class Main { static FastReader sc=new FastReader(); static long dp[]; // static boolean v[][][]; // static int mod=998244353;; static int mod=1000000007; static int max; static int bit[]; //static long fact[]; // static long A[]; static HashMap<Integer,Integer> map; //static StringBuffer sb=new StringBuffer(""); //static HashMap<Integer,Integer> map; static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { // StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); outer :while (ttt-- > 0) { int n=i(); long x=i(); int A[]=input(n); long B[]=new long[n+1]; int C[]=new int[n+1]; long max=0; int len=0; long D[]=new long[n+1]; Arrays.fill(D, Integer.MIN_VALUE); for(int i=0;i<n;i++) { long sum=0; for(int j=i;j<n;j++) { sum+=A[j]; if(sum>max) { max=sum; len=j-i+1; } else if(sum==max) { len=max(len,j-i+1); } long tm=sum; int l=j-i+1; if(D[l]<tm) { D[l]=tm; // System.out.println(tm+" "+l); } } } B[0]=max; C[0]=len; for(int i=1;i<=n;i++) { long op1=B[i-1]; len=C[i-1]; if(len>=i) { op1+=x; } long op2=D[i]+x*i; if(op1>op2) { B[i]=op1; C[i]=len; } else { B[i]=op2; C[i]=i; } } long E[]=new long[n+1]; E[n]=D[n]; for(int i=n-1;i>=0;i--) { E[i]=max(D[i],E[i+1]); } for(int i=0;i<=n;i++) { E[i]+=x*i; } for(int i=0;i<=n;i++) { B[i]=max(B[i],E[i]); } for(long i : B) { out.print(i+" "); } out.println(); } //System.out.println(sb.toString()); out.close(); //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 } static void go(int n,int a,int b) { for(int i=a;i>0;i--) { out.print(i+" "); } for(int i=n;i>a;i--) { out.print(i+" "); } out.println(); } static class Pair implements Comparable<Pair> { int x; int y; int z; Pair(int x,int y){ this.x=x; this.y=y; // this.z=z; } @Override public int compareTo(Pair o) { if(this.x>o.x) return 1; else if(this.x<o.x) return -1; else { if(this.y>o.y) return 1; else if(this.y<o.y) return -1; else return 0; } } public int hashCode() { final int temp = 14; int ans = 1; ans =x*31+y*13; return ans; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null) { return false; } if (this.getClass() != o.getClass()) { return false; } Pair other = (Pair)o; if (this.x != other.x || this.y!=other.y) { return false; } return true; } // /* FOR TREE MAP PAIR USE */ // public int compareTo(Pair o) { // if (x > o.x) { // return 1; // } // if (x < o.x) { // return -1; // } // if (y > o.y) { // return 1; // } // if (y < o.y) { // return -1; // } // return 0; // } } static int find(int A[],int a) { if(A[a]==a) return a; return A[a]=find(A, A[a]); } //static int find(int A[],int a) { // if(A[a]==a) // return a; // return find(A, A[a]); //} //FENWICK TREE static void update(int i, int x){ for(; i < bit.length; i += (i&-i)) bit[i] += x; } static int sum(int i){ int ans = 0; for(; i > 0; i -= (i&-i)) ans += bit[i]; return ans; } //END static void add(int v) { if(!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v)+1); } } static void remove(int v) { if(map.containsKey(v)) { map.put(v, map.get(v)-1); if(map.get(v)==0) map.remove(v); } } public static int upper(int A[],int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static int lower(int A[],int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void reverse(long A[]) { int n=A.length; long B[]=new long[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void reverse(int A[]) { int n=A.length; int B[]=new int[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static int[][] input(int n,int m){ int A[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=i(); } } return A; } static char[][] charinput(int n,int m){ char A[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) { A[i][j]=s.charAt(j); } } return A; } static int nextPowerOf2(int n) { if(n==0) return 1; n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } static int highestPowerof2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static long highestPowerof2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long [] prefix(long A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] prefix(int A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] suffix(long A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static long [] suffix(int A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static void fill(int dp[]) { Arrays.fill(dp, -1); } static void fill(int dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(int dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(int dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static void fill(long dp[]) { Arrays.fill(dp, -1); } static void fill(long dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(long dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(long dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long power(long x, long y, long p) { if(y==0) return 1; if(x==0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static void print(int A[]) { for(int i : A) { out.print(i+" "); } out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Integer> hash(int A[]){ HashMap<Integer,Integer> map=new HashMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static HashMap<Long,Integer> hash(long A[]){ HashMap<Long,Integer> map=new HashMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Integer,Integer> tree(int A[]){ TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Long,Integer> tree(long A[]){ TreeMap<Long,Integer> map=new TreeMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
521874140b996addda9aa57e59f68a84
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; public class IncreaseSubarraySums { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int x = sc.nextInt(); int a[] = new int[n]; int cumul[] = new int[n]; for(int i = 0; i<n; i++) { a[i] = sc.nextInt(); } cumul[0] = a[0]; for(int i = 1; i<n; i++) { cumul[i]=cumul[i-1]+a[i]; } int out[] = new int[n+1]; int sums[] = new int[n]; Arrays.fill(sums, Integer.MIN_VALUE); for(int i = 1; i<=n; i++) { for(int j = 0; j<n-(i-1); j++) { int z = j+i-1; int l = cumul[z]-((j==0)?0:cumul[j-1]); sums[i-1] = Math.max(sums[i-1],l); } } for(int k = 0; k<=n; k++) { int b = Integer.MIN_VALUE; for(int i = 0; i<n; i++) { int l = sums[i] + (k>(i+1)?(i+1)*x:(k*x)); b = Math.max(b,l); } out[k] = Math.max(0,b); } for(int i = 0; i<=n; i++) { System.out.print(out[i]+" "); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
0140304b613af10e8b2b6d513e43a113
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class incsubarraysum{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); while(T-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine()); int N = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] nums = new int[N]; for(int i = 0;i < N;i++) { nums[i] = Integer.parseInt(st.nextToken()); } long[] best = new long[N+1]; Arrays.fill(best, -Long.MIN_VALUE); best[0] = 0; for(int i = 0;i < N;i++) { long sum = 0; for(int j = i;j < N;j++) { sum += nums[j]; best[j-i+1] = Math.max(best[j-i+1], sum); } } // System.out.println(Arrays.toString(best)); for(int k = 0;k <= N;k++) { long max = Long.MIN_VALUE; for(int i = 0;i <= N;i++) { max = Math.max(max, best[i] + Math.min(k, i) * (long) x); } System.out.print(max + " "); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
977d5d6d40199afaac43458315d8018c
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class Main { // public static int gcd(int a, int b) { // return b == 0 ? a : gcd(b, a % b); // } // // public static int lcm(int a, int b) { // return a * b / gcd(a, b); // } // // public static long pow(int number, long i) { // if (i == 0) // return 1; // long sum = pow(number, i / 2); // sum *= sum; // if ((i & 1) == 1) // sum *= number; // return sum; // } // // public static long fac(long i) { // if (i == 0) // return 1; // return i * fac(i - 1); // } // // public static long compliment(int number, int bits) { // return number ^ (pow(2, bits) - 1); // } public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int x = sc.nextInt(); int[] arr = sc.nextIntArray(n); Integer[] a = new Integer[n + 1]; a[0] = 0; for (int i = 0; i < n; i++) { int pre = 0, p = 1; for (int j = i; j < n; j++) { pre += arr[j]; if (a[p] != null) { if (pre > a[p]) a[p] = pre; } else a[p] = pre; p++; } } int max = 0; for (int i = 0; i <= n; i++) { max = Math.max(max, a[i]); } pw.print(max + " "); for (int i = 1; i <= n; i++) { for (int j = i - 1; j < n + 1; j++) { if (j >= i) a[j] += x; } for (int j = 0; j <= n; j++) { max = Math.max(max, a[j]); } pw.print(max + " "); } pw.println(); } pw.flush(); } static class Pair { int a; int b; public Pair(int a, int b) { this.a = a; this.b = b; } public String toString() { return "( " + a + " " + b + " )"; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
76092b67876ea160b3d2c182f78f9f61
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class Main { // public static int gcd(int a, int b) { // return b == 0 ? a : gcd(b, a % b); // } // // public static int lcm(int a, int b) { // return a * b / gcd(a, b); // } // // public static long pow(int number, long i) { // if (i == 0) // return 1; // long sum = pow(number, i / 2); // sum *= sum; // if ((i & 1) == 1) // sum *= number; // return sum; // } // // public static long fac(long i) { // if (i == 0) // return 1; // return i * fac(i - 1); // } // // public static long compliment(int number, int bits) { // return number ^ (pow(2, bits) - 1); // } public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int x = sc.nextInt(); int[] arr = sc.nextIntArray(n); Pair[] pair = new Pair[n+1]; pair[0] = new Pair(0,0); for (int i = 0; i < n; i++) { int pre = 0,p = 1; for (int j = i; j < n; j++) { pre += arr[j]; if (pair[p] != null) { if (pre > pair[p].a) pair[p] = new Pair(pre, j + 1 - i); } else pair[p] = new Pair(pre, j + 1 - i); p++; } } Arrays.sort(pair,(y,z)->z.a-y.a); pw.print(pair[0].a + " "); for (int i = 1; i <= n; i++) { for (Pair p : pair) { if(p.b>=i) p.a+=x; } Arrays.sort(pair,(y,z)->z.a-y.a); pw.print(pair[0].a + " "); } pw.println(); } pw.flush(); } static class Pair { int a; int b; public Pair(int a, int b) { this.a = a; this.b = b; } public String toString() { return "( " + a + " " + b + " )"; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
2d9f98113ea5787afa1115f1b8f79414
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.sql.Array; import java.util.*; public class Main { public static void main(String[] args) { IO io = new IO(); int t = io.nextInt(); while (t-- > 0) { int n = io.nextInt(), x = io.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; ++i) a[i] = io.nextInt(); int[] dp = new int[n + 1]; Arrays.fill(dp, (int) -1e9); for (int i = 0; i < n; ++i) { int cur_sum = 0; for (int j = i; j < n; ++j) { cur_sum += a[j]; dp[j - i + 1] = Math.max(dp[j - i + 1], cur_sum); } } for (int k = 0; k <= n; ++k) { int answer = 0; for (int i = 0; i <= n; ++i) answer = Math.max(answer, dp[i] + Math.min(i, k) * x); io.print(answer + " "); } io.println(); } io.flush(); io.close(); } static final int mod = (int) 1e9 + 7; static final Random random = new Random(); static void shuffleSort(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { int nex = random.nextInt(n), temp = a[nex]; a[nex] = a[i]; a[i] = temp; } Arrays.sort(a); } static long add(long a, long b) { return (a + b) % mod; } static long sub(long a, long b) { return ((a - b) % mod + mod) % mod; } static long mul(long a, long b) { return (a * b) % mod; } static long exp(long base, long exp) { if (exp == 0) return 1; long half = exp(base, exp >> 1); if ((exp & 1) == 0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials = new long[2_000_001]; static long[] invFactorials = new long[2_000_001]; static void calc_factorials() { factorials[0] = invFactorials[0] = 1; for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i); invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2); for (int i=invFactorials.length - 2; i >=0; i--) invFactorials[i] = mul(invFactorials[i + 1], i + 1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k])); } static class IO extends PrintWriter { private final int BS = 1 << 16; private final char NC = (char) 0; private final byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private final BufferedInputStream in; // standard input public IO() { this(System.in, System.out); } public IO(InputStream i, OutputStream o) { super(o); in = new BufferedInputStream(System.in, BS); } public IO(String problemName) throws IOException { super(problemName + ".out"); in = new BufferedInputStream(new FileInputStream(problemName + ".in"), BS); } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
31737a1a10b7dc03947948b02d22e26f
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
/*package whatever //do not write package name here */ import java.io.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int x=sc.nextInt(); long a[]=new long[n]; int i; for(i=0;i<n;i++) a[i]=sc.nextLong(); long dp[][]=new long[n+1][n+1]; long max2=0; for(i=1;i<=n;i++){ dp[0][i]=Math.max(0,dp[0][i-1]+a[i-1]); max2=Math.max(max2,dp[0][i]); } System.out.print(max2+" "); for(i=1;i<=n;i++){ for(int j=i;j<=n;j++){ dp[i][j]=Math.max(0,dp[i-1][j-1]+a[j-1]+x); max2=Math.max(max2,dp[i][j]); } System.out.print(max2+" "); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
7765d904d85647b18c0545f47595ca9e
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
/*package whatever //do not write package name here */ import java.io.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int x=sc.nextInt(); long a[]=new long[n]; int i; for(i=0;i<n;i++) a[i]=sc.nextLong(); long dp[][]=new long[n+1][n+1]; long max2=0; for(i=1;i<=n;i++){ dp[0][i]=Math.max(0,dp[0][i-1]+a[i-1]); max2=Math.max(max2,dp[0][i]); } System.out.print(max2+" "); for(i=1;i<=n;i++){ max2=0; for(int j=1;j<=n;j++){ dp[i][j]=Math.max(0,dp[i-1][j-1]+a[j-1]+x); max2=Math.max(max2,dp[i][j]); } System.out.print(max2+" "); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
77da5a269cf76fceb375a3c3b66bf0cb
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception {int t = sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); int x=sc.nextInt(); int []arr=sc.nextIntArray(n); long [] ans=new long[n+1]; Arrays.fill(ans, Long.MIN_VALUE); for(int i=0;i<n;i++) { long sum=0; for(int j=i;j<n;j++) { sum+=arr[j]; ans[j-i+1]=Math.max(ans[j-i+1], sum); } } for(int i=0;i<=n;i++) { long out=0; for(int j=1;j<=n;j++) { out=Math.max(out, Math.min(i, j)*x+ans[j]); } pw.print(out+" "); } pw.println(); } pw.flush();} static class SegmentTree { int[] arr, sTree, lazy; int N; public SegmentTree(int[] in) { arr=in; N = in.length-1; sTree = new int[2*N]; lazy = new int[2*N]; build(1,1,N); } public void build(int Node, int left, int right) { //O(n) if(left==right) sTree[Node]=arr[left]; else { int r = 2*Node +1; int l = 2*Node; int mid =(left+right)/2; build(l, left ,mid); build(r, mid+1, right); sTree[Node] = sTree[l]+sTree[r]; } } public int query(int i, int j) { return query(1,1,N,i,j ); } public int query(int Node, int left, int right , int i, int j ) { if(right<i || left>j) return 0; if(right<=j && i<=left) { return sTree[Node]; } int mid = (left+right)/2; int l = 2*Node; int r = 2*Node +1; return query(l, left, mid, i, j)+query(r, mid+1, right, i, j); } public void updatePoint(int idx, int value) { int node = idx+N-1; arr[idx]=value; sTree[node]=value; while(node>1) { node/=2; int l = 2*node; int r = 2*node+1; sTree[node]=sTree[l]+sTree[r]; } } public void updateRange(int i, int j , int val) { updateRange(1, 1, N, i, j, val); } public void updateRange(int Node, int left , int right, int i, int j, int val) { if(i>right || left>j) return; if(right<=j && i<=left) { sTree[Node]+=val*(right-left+1); lazy[Node]+=val; } else { int l = 2*Node; int r = 2*Node+1; int mid = (left+right)/2; propagate(Node, left, right); updateRange(l, left, mid, i, j, val); updateRange(r, mid+1, right, mid, j, val); sTree[Node] = sTree[l]+sTree[r]; } } public void propagate(int node , int left , int right) { int l = 2*node; int r = 2*node +1; int mid = (left+right)/2; lazy[l] = lazy[node]; lazy[r] = lazy[node]; sTree[l]+=lazy[node]*(mid-left+1); sTree[r]+=lazy[node]*(right-mid); lazy[node] = 0; } } public static void sort(int[] in) { shuffle(in); Arrays.sort(in); } public static void shuffle(int[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); int tmp = in[i]; in[i] = in[idx]; in[idx] = tmp; } } static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return x - o.x; } public String toString() { return x + " " + y; } } static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } public static void display(char[][] a) { for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j]); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
cae97bc228f154c864efe96f6ab9ca11
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.stream.Collector; import java.util.stream.Collectors; import javax.print.DocFlavor.INPUT_STREAM; public class Main { public static void main(String[] args) throws Exception { Sol obj=new Sol(); obj.runner(); } } class Sol{ FastScanner fs=new FastScanner(); Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); List<Integer> sieve; Set<Integer> sieveSet; void runner() throws Exception{ int T=1; //sieve=sieve(); //sieveSet=new HashSet<>(sieve); //T=sc.nextInt(); T=fs.nextInt(); while(T-->0) { solve(T); } out.close(); System.gc(); } private void solve(int T) throws Exception { int n=fs.nextInt(); int x=fs.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=fs.nextInt(); } long maxSum[ ]= new long[n+1]; for( int i=1;i<=n;i++ ) { int j=0, k=0; long sum=0; while( k<i ) { sum+=arr[k]; k++; } maxSum[i]=sum; while( k<n ) { sum-=arr[j++]; sum+=arr[k++]; maxSum[i]=Math.max(maxSum[i], sum); } } for(int k=0;k<=n;k++) { long sum=0; for(int i=0;i<=n;i++) { sum=Math.max(sum, maxSum[i]+ Math.min(k, i)*x ); } System.out.print(sum+" "); } System.out.println(); /**/ } static class FastScanner { BufferedReader br; StringTokenizer st ; FastScanner(){ br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } FastScanner(String file) { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); st = new StringTokenizer(""); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("file not found"); e.printStackTrace(); } } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } String readLine() throws IOException{ return br.readLine(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
bf3138037bfe4839cbe325ce1c7473fb
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.StringTokenizer; public class CodeForces { static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } public FastScanner(File f){ try { br=new BufferedReader(new FileReader(f)); st=new StringTokenizer(""); } catch(FileNotFoundException e){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } } String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readLongArray(int n) { long[] a =new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } public static long factorial(int n){ if(n==0)return 1; return (long)n*factorial(n-1); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static void sort (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sortReversed (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b,new Comparator<Integer>(){ @Override public int compare(Integer o1, Integer o2) { return o2-o1; } }); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sort (long[]a){ ArrayList<Long> b = new ArrayList<>(); for(long i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static ArrayList<Integer> sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } ArrayList<Integer> ans = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (prime[i] == true) ans.add(i); } return ans; } static int binarySearchSmallerOrEqual(int arr[], int key) { int n = arr.length; int left = 0, right = n; int mid = 0; while (left < right) { mid = (right + left) >> 1; if (arr[mid] == key) { while (mid + 1 < n && arr[mid + 1] == key) mid++; break; } else if (arr[mid] > key) right = mid; else left = mid + 1; } while (mid > -1 && arr[mid] > key) mid--; return mid; } public static int binarySearchStrictlySmaller(int[] arr, int target) { int start = 0, end = arr.length-1; if(end == 0) return -1; if (target > arr[end]) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } static int binarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static int binarySearch(long arr[], long x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static void init(int[]arr,int val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static void init(int[][]arr,int val){ for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){ arr[i][j]=val; } } } static void init(long[]arr,long val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static<T> void init(ArrayList<ArrayList<T>>arr,int n){ for(int i=0;i<n;i++){ arr.add(new ArrayList()); } } static int binarySearchStrictlySmaller(ArrayList<Pair> arr, int target) { int start = 0, end = arr.size()-1; if(end == 0) return -1; if (target > arr.get(end).y) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid).y >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } static int binarySearchStrictlyGreater(int[] arr, int target) { int start = 0, end = arr.length - 1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] <= target) { start = mid + 1; } else { ans = mid; end = mid - 1; } } return ans; } static long sum (int []a, int[][] vals){ long ans =0; int m=0; while(m<5){ for (int i=m+1;i<5;i+=2){ ans+=(long)vals[a[i]-1][a[i-1]-1]; ans+=(long)vals[a[i-1]-1][a[i]-1]; } m++; } return ans; } public static long pow(long n, long pow) { if (pow == 0) { return 1; } long retval = n; for (long i = 2; i <= pow; i++) { retval *= n; } return retval; } static String reverse(String s){ StringBuffer b = new StringBuffer(s); b.reverse(); return b.toString(); } static String charToString (char[] arr){ String t=""; for(char c :arr){ t+=c; } return t; } int[] copy (int [] arr , int start){ int[] res = new int[arr.length-start]; for (int i=start;i<arr.length;i++)res[i-start]=arr[i]; return res; } public static void main(String[] args) { // StringBuilder sbd = new StringBuilder(); // PrintWriter out = new PrintWriter("output.txt"); // File input = new File("input.txt"); // FastScanner fs = new FastScanner(input); // FastScanner fs = new FastScanner(); // Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int testNumber =fs.nextInt(); // ArrayList<Integer> arr = new ArrayList<>(); for (int T =0;T<testNumber;T++){ // StringBuffer sbf = new StringBuffer(); int n = fs.nextInt(); int x = fs.nextInt(); int[] arr = fs.readArray(n); int [] ans = new int[n+1]; int[] sums = new int[n]; sums[0]=arr[0]; for (int i=1;i<n;i++){ sums[i]=sums[i-1]+arr[i]; } for (int l=1;l<=n;l++){ int sum=(int)-1e9; for (int i=0;i<=n-l;i++){ int s=sums[i+l-1]-(i==0?0:sums[i-1]); sum=Math.max(s,sum); } for (int i=0;i<=n;i++)ans[i]=Math.max(ans[i],sum+Math.min(i,l)*x); } for (int i:ans)out.print(i+" "); out.print("\n"); } out.flush(); } static class Pair { int x;//a int y;//k public Pair(int x,int y){ this.x=x; this.y=y; } @Override public boolean equals(Object o) { if(o instanceof Pair){ if(o.hashCode()!=hashCode()){ return false; } else { return x==((Pair)o).x&&y==((Pair)o).y; } } return false; } @Override public int hashCode() { return x+2*y; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
591b30d2ae292ff8e45e97b84cdd3b18
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); // int a = 1; int t; t = in.nextInt(); //t = 1; while (t > 0) { // out.print("Case #"+(a++)+": "); solver.call(in,out); t--; } out.close(); } static class TaskA { Map<String, Integer> map; int n, x; int[][] dp = new int[5001][5001]; int[] arr = new int[5001]; public void call(InputReader in, PrintWriter out) { n = in.nextInt(); x = in.nextInt(); map = new HashMap<>(); for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } for (int i = 0; i <= n; i++) { Arrays.fill(dp[i], 0); } int max, sum, ans = 0; for (int i = 0; i < n; i++) { max = 0; sum = arr[i]; max = Math.max(max, sum); for (int j = i - 1; j >= 0; j--) { sum += arr[j]; max = Math.max(max, sum); } dp[0][i] = max; ans = Math.max(dp[0][i], ans); } out.print(ans+" "); for (int i = 1; i <= n; i++) { ans = 0; for (int j = 0; j < n; j++) { if(j==0){ dp[i][j] = Math.max(dp[i][j], x + arr[j]); } else { dp[i][j] = Math.max(dp[i][j], x + arr[j] + dp[i - 1][j - 1]); } ans = Math.max(dp[i][j],ans); } out.print(ans+" "); } out.println(); } } static int gcd(int a, int b ) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class answer implements Comparable<answer>{ int a, b; public answer(int a, int b, int c) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return Integer.compare(this.b, o.b); } } static class answer1 implements Comparable<answer1>{ int a, b, c; public answer1(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } @Override public int compareTo(answer1 o) { return (o.a - this.a); } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (Long i:a) l.add(i); l.sort(Collections.reverseOrder()); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static final Random random=new Random(); static void shuffleSort(int[] a) { int n = a.length; for (int i=0; i<n; i++) { int oi= random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
605146ae5d0e0b1870de27d2c9f9246c
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.concurrent.ThreadLocalRandom; //3 6 6 1 //2 7 4 6 public class D { private static void sport(int[] a, int x) { int n = a.length; long[] p = new long[n + 1]; for (int i = 1; i < n + 1; i++) { p[i] = p[i - 1] + a[i - 1]; } long[] ans = new long[n + 1]; Queue<long[]> pq = new PriorityQueue<>(new Comparator<long[]>() { @Override public int compare(long[] o1, long[] o2) { return (int) (o2[0] - o1[0]); } }); //pq.add(new long[]{0, n + 1, 0}); long[][] temp = new long[n + 1][4]; for (long[] longs : temp) { Arrays.fill(longs, (long) -1e12); } for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int len = j - i + 1; if (temp[len][0] < p[j + 1] - p[i]) { temp[len] = new long[]{p[j + 1] - p[i], i, j, 0}; } } } for (int len = 0; len < n + 1; len++) { for (int i = 0; len >= i && i < n + 1; i++) { ans[i] = Math.max(ans[i], temp[len][0] + (long) i * x); } } for (int i = 1; i < n + 1; i++) { ans[i] = Math.max(ans[i - 1], ans[i]); } for (long an : ans) { System.out.print(an + " "); } System.out.println(); } public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int x = sc.nextInt(); int[] a = sc.readArrayInt(n); sport(a, x); } } static void shuffleArrayA(int[][] ar) { // If running on Java 6 or older, use `new Random()` on RHS here Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int[] a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } static void shuffleArray(int[] ar) { // If running on Java 6 or older, use `new Random()` on RHS here Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } void nextLine() throws IOException { br.readLine(); } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } int[] readArrayInt(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
6a81127b8026d11ff43fe6bc1c57dfa4
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; //import java.math.BigInteger; public class code{ public static class Pair{ int a; int b; Pair(int i,int j){ a=i; b=j; } } public static int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } public static void shuffle(int a[], int n) { for (int i = 0; i < n; i++) { // getting the random index int t = (int)Math.random() * a.length; // and swapping values a random index // with the current index int x = a[t]; a[t] = a[i]; a[i] = x; } } public static PrintWriter out = new PrintWriter(System.out); //public static long mod=(long)(1e9+7); //@SuppressWarnings("unchecked") public static void main(String[] arg) throws IOException{ //Reader in=new Reader(); //PrintWriter out = new PrintWriter(System.out); //Scanner in = new Scanner(System.in); FastScanner in=new FastScanner(); int t=in.nextInt(); while(t-- > 0){ int n=in.nextInt(); long x=in.nextLong(); long[] a=new long[n]; long[] res=new long[n+1]; for(int i=0;i<n;i++) a[i]=in.nextInt(); Arrays.fill(res,(long)Integer.MIN_VALUE); res[0]=0; for(int i=0;i<n;i++){ long sum=0; for(int j=i;j<n;j++){ sum+=a[j]; res[j-i+1]=Math.max(res[j-i+1],sum); } } long max=(long)Integer.MIN_VALUE; for(int i=0;i<=n;i++){ for(int j=i;j<=n;j++){ max=Math.max(res[j]+i*x,max); } out.print(max+" "); } out.println(); } out.flush(); } } class Fenwick{ int[] bit; public Fenwick(int n){ bit=new int[n]; //int sum=0; } public void update(int index,int val){ index++; for(;index<bit.length;index += index&(-index)) bit[index]+=val; } public int presum(int index){ int sum=0; for(;index>0;index-=index&(-index)) sum+=bit[index]; return sum; } public int sum(int l,int r){ return presum(r+1)-presum(l); } } class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
2e0e3d434f69a7955b1c959ecca199b1
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // import org.w3c.dom.Node; import java.awt.image.AreaAveragingScaleFilter; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.*; public class CFC { static CFC.Reader sc = new CFC.Reader(); static BufferedWriter bw; static int team1; static int team2; static int minkicks; public CFC() { } public static void main(String[] args) throws IOException { int t = inputInt(); process: for (int p = 0; p < t; p++) { int n = inputInt(); int x = inputInt(); int[] ar = new int[n]; for (int i = 0; i < n; i++) ar[i] = inputInt(); int[][] dp=new int[n+2][n+2]; int sum=0; for(int i=1;i<=n;i++){ dp[i][0]=Math.max(dp[i-1][0]+ar[i-1],ar[i-1]); dp[i][0]=Math.max(0,dp[i][0]); } for(int j=1;j<=n;j++){ for(int i=1;i<=n;i++){ dp[i][j]=Math.max(dp[i-1][j]+ar[i-1],dp[i-1][j-1]+x+ar[i-1]); dp[i][j]=Math.max(0,dp[i][j]); } } int max=0; for (int j=0;j<=n;j++){ int max1=0; for(int i=1;i<=n;i++){ max1=Math.max(max1,dp[i][j]); } print(max1+" "); } println(""); } bw.flush(); bw.close(); } static class Checker { int num, index; public Checker(int num, int index) { this.num = num; this.index = index; } } static void dfs(List<List<Integer>> lists, boolean[] vis, int i) { vis[i] = true; for (Integer integer : lists.get(i)) { if (!vis[integer]) { dfs(lists, vis, integer); } } } static void make_set(int n, int[] par, int[] rnk) { for (int i = 1; i <= n; i++) { par[i] = i; rnk[i] = 1; } } static void union_set(int a, int b, int[] rnk, int[] par) { int p1 = find_set(a, par); int p2 = find_set(b, par); if (p1 == p2) return; if (rnk[p1] > rnk[p2]) { par[p2] = p1; rnk[p1] = rnk[p1] + rnk[p2]; } else { par[p1] = p2; rnk[p2] = rnk[p1] + rnk[p2]; } } static int find_set(int x, int[] par) { if (par[x] == x) { return x; } par[x] = find_set(par[x], par); return par[x]; } public static int checker(int a) { if (a % 2 == 0) { return 1; } else { byte b; do { b = 2; } while (a + b != (a ^ b)); return b; } } public static boolean checkPrime(long n) { if (n != 0 && n != 1) { for (long j = 2; j * j <= n; ++j) { if (n % j == 0) { return false; } } return true; } else { return false; } } public static int inputInt() throws IOException { return sc.nextInt(); } public static long inputLong() throws IOException { return sc.nextLong(); } public static double inputDouble() throws IOException { return sc.nextDouble(); } public static String inputString() throws IOException { return sc.readLine(); } public static void print(String a) throws IOException { bw.write(a); } public static void printSp(String a) throws IOException { bw.write(a + " "); } public static void println(String a) throws IOException { bw.write(a + "\n"); } public static long power(long a, long b, long c) { long ans; for (ans = 1L; b != 0L; b /= 2L) { if (b % 2L == 1L) { ans *= a; ans %= c; } a *= a; a %= c; } return ans; } public static long power1(long a, long b, long c) { long ans; for (ans = 1L; b != 0L; b /= 2L) { if (b % 2L == 1L) { ans = multiply(ans, a, c); } a = multiply(a, a, c); } return ans; } public static long multiply(long a, long b, long c) { long res = 0L; for (a %= c; b > 0L; b /= 2L) { if (b % 2L == 1L) { res = (res + a) % c; } a = (a + a) % c; } return res % c; } public static long totient(long n) { long result = n; for (long i = 2L; i * i <= n; ++i) { if (n % i == 0L) { while (n % i == 0L) { n /= i; } result -= result / i; } } if (n > 1L) { result -= result / n; } return result; } public static long gcd(long a, long b) { return b == 0L ? a : gcd(b, a % b); } public static boolean[] primes(int n) { boolean[] p = new boolean[n + 1]; p[0] = false; p[1] = false; int i; for (i = 2; i <= n; ++i) { p[i] = true; } for (i = 2; i * i <= n; ++i) { if (p[i]) { for (int j = i * i; j <= n; j += i) { p[j] = false; } } } return p; } public String LargestEven(String S) { int[] count = new int[10]; int num; for (num = 0; num < S.length(); ++num) { ++count[S.charAt(num) - 48]; } num = -1; for (int i = 0; i <= 8; i += 2) { if (count[i] > 0) { num = i; break; } } StringBuilder ans = new StringBuilder(); for (int i = 9; i >= 0; --i) { int j; if (i != num) { for (j = 1; j <= count[i]; ++j) { ans.append(i); } } else { for (j = 1; j <= count[num] - 1; ++j) { ans.append(num); } } } if (num != -1 && count[num] > 0) { ans.append(num); } return ans.toString(); } static { bw = new BufferedWriter(new OutputStreamWriter(System.out)); team1 = 0; team2 = 0; minkicks = 10; } public static class Score { int l; String s; public Score(int l, String s) { this.l = l; this.s = s; } } static class Reader { private final int BUFFER_SIZE = 65536; private DataInputStream din; private byte[] buffer; private int bufferPointer; private int bytesRead; public Reader() { this.din = new DataInputStream(System.in); this.buffer = new byte[65536]; this.bufferPointer = this.bytesRead = 0; } public Reader(String file_name) throws IOException { this.din = new DataInputStream(new FileInputStream(file_name)); this.buffer = new byte[65536]; this.bufferPointer = this.bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; int cnt; byte c; for (cnt = 0; (c = this.read()) != -1 && c != 10; buf[cnt++] = (byte) c) { } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c; for (c = this.read(); c <= 32; c = this.read()) { } boolean neg = c == 45; if (neg) { c = this.read(); } do { ret = ret * 10 + c - 48; } while ((c = this.read()) >= 48 && c <= 57); return neg ? -ret : ret; } public long nextLong() throws IOException { long ret = 0L; byte c; for (c = this.read(); c <= 32; c = this.read()) { } boolean neg = c == 45; if (neg) { c = this.read(); } do { ret = ret * 10L + (long) c - 48L; } while ((c = this.read()) >= 48 && c <= 57); return neg ? -ret : ret; } public double nextDouble() throws IOException { double ret = 0.0D; double div = 1.0D; byte c; for (c = this.read(); c <= 32; c = this.read()) { } boolean neg = c == 45; if (neg) { c = this.read(); } do { ret = ret * 10.0D + (double) c - 48.0D; } while ((c = this.read()) >= 48 && c <= 57); if (c == 46) { while ((c = this.read()) >= 48 && c <= 57) { ret += (double) (c - 48) / (div *= 10.0D); } } return neg ? -ret : ret; } private void fillBuffer() throws IOException { this.bytesRead = this.din.read(this.buffer, this.bufferPointer = 0, 65536); if (this.bytesRead == -1) { this.buffer[0] = -1; } } private byte read() throws IOException { if (this.bufferPointer == this.bytesRead) { this.fillBuffer(); } return this.buffer[this.bufferPointer++]; } public void close() throws IOException { if (this.din != null) { this.din.close(); } } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
05c53de2417756d3286b7a413236d335
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; import java.math.BigDecimal; import java.math.*; public class Main{ public static void main(String[] args) { TaskA solver = new TaskA(); // boolean[]prime=seive(3*100001); int t = in.nextInt(); for (int i = 1; i <= t ; i++) { solver.solve(1, in, out); } // solver.solve(1, in, out); out.flush(); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n= in.nextInt(); int x= in.nextInt(); int[]arr=new int[n]; int[]sum=new int[n]; int max=Integer.MIN_VALUE; for(int i=0;i<n;i++) { arr[i]=in.nextInt(); max=Math.max(max, arr[i]); if(i==0) { sum[i]=arr[i]; } else { sum[i]=sum[i-1]+arr[i]; } } int[]mA=new int[n+1]; int[]ans=new int[n+1]; // print(Math.max(max, 0)+" "); for(int i=0;i<n;i++) { int max1=Integer.MIN_VALUE; for(int j=0;j<n-i;j++) { max1=Math.max(max1,sum[j+i]-sum[j]+arr[j]); } mA[i+1]=max1; max=Math.max(max1, max); } // printIArray(mA); for(int k=0;k<=n;k++) { max=mA[k]; for(int i=0;i<=n;i++) { if(i<k) { max=Math.max(max, mA[i]+i*x); } else { max=Math.max(max, mA[i]+k*x); } } ans[k]=max; } printIArray(ans); } } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } static long ceil(long a,long b) { return (a/b + Math.min(a%b, 1)); } static char[] rev(char[]ans,int n) { for(int i=ans.length-1;i>=n;i--) { ans[i]=ans[ans.length-i-1]; } return ans; } static int countStep(int[]arr,long sum,int k,int count1) { int count=count1; int index=arr.length-1; while(sum>k&&index>0) { sum-=(arr[index]-arr[0]); count++; if(sum<=k) { break; } index--; // println("c"); } if(sum<=k) { return count; } else { return Integer.MAX_VALUE; } } static long pow(long b, long e) { long ans = 1; while (e > 0) { if (e % 2 == 1) ans = ans * b % mod; e >>= 1; b = b * b % mod; } return ans; } static void sortDiff(Pair arr[]) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.first==p2.first) { return (p1.second-p1.first)-(p2.second-p1.first); } return (p1.second-p1.first)-(p2.second-p2.first); } }); } static void sortF(Pair arr[]) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.first==p2.first) { return p1.second-p2.second; } return p1.first - p2.first; } }); } static int[] shift (int[]inp,int i,int j){ int[]arr=new int[j-i+1]; int n=j-i+1; for(int k=i;k<=j;k++) { arr[(k-i+1)%n]=inp[k]; } for(int k=0;k<n;k++ ) { inp[k+i]=arr[k]; } return inp; } static long[] fac; static long mod = (long) 1000000007; static void initFac(long n) { fac = new long[(int)n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (fac[i - 1] * i) % mod; } } static long nck(int n, int k) { if (n < k) return 0; long den = inv((int) (fac[k] * fac[n - k] % mod)); return fac[n] * den % mod; } static long inv(long x) { return pow(x, mod - 2); } static void sort(int[] a) { ArrayList<Integer> q = new ArrayList<>(); for (int i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static void sort(long[] a) { ArrayList<Long> q = new ArrayList<>(); for (long i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } public static int bfsSize(int source,LinkedList<Integer>[]a,boolean[]visited) { Queue<Integer>q=new LinkedList<>(); q.add(source); visited[source]=true; int distance=0; while(!q.isEmpty()) { int curr=q.poll(); distance++; for(int neighbour:a[curr]) { if(!visited[neighbour]) { visited[neighbour]=true; q.add(neighbour); } } } return distance; } public static Set<Integer>factors(int n){ Set<Integer>ans=new HashSet<>(); ans.add(1); for(int i=2;i*i<=n;i++) { if(n%i==0) { ans.add(i); ans.add(n/i); } } return ans; } public static int bfsSp(int source,int destination,ArrayList<Integer>[]a) { boolean[]visited=new boolean[a.length]; int[]parent=new int[a.length]; Queue<Integer>q=new LinkedList<>(); int distance=0; q.add(source); visited[source]=true; parent[source]=-1; while(!q.isEmpty()) { int curr=q.poll(); if(curr==destination) { break; } for(int neighbour:a[curr]) { if(neighbour!=-1&&!visited[neighbour]) { visited[neighbour]=true; q.add(neighbour); parent[neighbour]=curr; } } } int cur=destination; while(parent[cur]!=-1) { distance++; cur=parent[cur]; } return distance; } static int bs(int size,int[]arr) { int x = -1; for (int b = size/2; b >= 1; b /= 2) { while (!ok(arr)); } int k = x+1; return k; } static boolean ok(int[]x) { return false; } public static int solve1(ArrayList<Integer> A) { long[]arr =new long[A.size()+1]; int n=A.size(); for(int i=1;i<=A.size();i++) { arr[i]=((i%2)*((n-i+1)%2))%2; arr[i]%=2; } int ans=0; for(int i=0;i<A.size();i++) { if(arr[i+1]==1) { ans^=A.get(i); } } return ans; } public static String printBinary(long a) { String str=""; for(int i=31;i>=0;i--) { if((a&(1<<i))!=0) { str+=1; } if((a&(1<<i))==0 && !str.isEmpty()) { str+=0; } } return str; } public static String reverse(long a) { long rev=0; String str=""; int x=(int)(Math.log(a)/Math.log(2))+1; for(int i=0;i<32;i++) { rev<<=1; if((a&(1<<i))!=0) { rev|=1; str+=1; } else { str+=0; } } return str; } //////////////////////////////////////////////////////// static void sortS(Pair arr[]) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return p1.second - p2.second; } }); } static class Pair implements Comparable<Pair> { int first ;int second ; public Pair(int x, int y) { this.first = x ;this.second = y ; } @Override public boolean equals(Object obj) { if(obj == this)return true ; if(obj == null)return false ; if(this.getClass() != obj.getClass()) return false ; Pair other = (Pair)(obj) ; if(this.first != other.first)return false ; if(this.second != other.second)return false ; return true ; } @Override public int hashCode() { return this.first^this.second ; } @Override public String toString() { String ans = "" ;ans += this.first ; ans += " "; ans += this.second ; return ans ; } @Override public int compareTo(Main.Pair o) { { if(this.first==o.first) { return this.second-o.second; } return this.first - o.first; } } } ////////////////////////////////////////////////////////////////////////// static int nD(long num) { String s=String.valueOf(num); return s.length(); } static int CommonDigits(int x,int y) { String s=String.valueOf(x); String s2=String.valueOf(y); return 0; } static int lcs(String str1, String str2, int m, int n) { int L[][] = new int[m + 1][n + 1]; int i, j; // Following steps build L[m+1][n+1] in // bottom up fashion. Note that L[i][j] // contains length of LCS of str1[0..i-1] // and str2[0..j-1] for (i = 0; i <= m; i++) { for (j = 0; j <= n; j++) { if (i == 0 || j == 0) L[i][j] = 0; else if (str1.charAt(i - 1) == str2.charAt(j - 1)) L[i][j] = L[i - 1][j - 1] + 1; else L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]); } } // L[m][n] contains length of LCS // for X[0..n-1] and Y[0..m-1] return L[m][n]; } ///////////////////////////////// boolean IsPowerOfTwo(int x) { return (x != 0) && ((x & (x - 1)) == 0); } //////////////////////////////// static long power(long a,long b,long m ) { long ans=1; while(b>0) { if(b%2==1) { ans=((ans%m)*(a%m))%m; b--; } else { a=(a*a)%m;b/=2; } } return ans%m; } /////////////////////////////// public static boolean repeatedSubString(String string) { return ((string + string).indexOf(string, 1) != string.length()); } static int search(char[]c,int start,int end,char x) { for(int i=start;i<end;i++) { if(c[i]==x) {return i;} } return -2; } //////////////////////////////// static long gcd(long a, long b) { while (b != 0) { long t = b; b = a % b; a = t; } return a; } static long fac(long a) { if(a== 0L || a==1L)return 1L ; return a*fac(a-1L) ; } static ArrayList al() { ArrayList<Integer>a =new ArrayList<>(); return a; } static HashSet h() { return new HashSet<Integer>(); } static void debug(int[][]a) { int n= a.length; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { out.print(a[i][j]+" "); } out.print("\n"); } } static void debug(int[]a) { out.println(Arrays.toString(a)); } static void debug(ArrayList<Integer>a) { out.println(a.toString()); } static boolean[]seive(int n){ boolean[]b=new boolean[n+1]; for (int i = 2; i <= n; i++) b[i] = true; for(int i=2;i*i<=n;i++) { if(b[i]) { for(int j=i*i;j<=n;j+=i) { b[j]=false; } } } return b; } static int longestIncreasingSubseq(int[]arr) { int[]sizes=new int[arr.length]; Arrays.fill(sizes, 1); int max=1; for(int i=1;i<arr.length;i++) { for(int j=0;j<i;j++) { if(arr[j]<arr[i]) { sizes[i]=Math.max(sizes[i],sizes[j]+1); max=Math.max(max, sizes[i]); } } } return max; } public static ArrayList primeFactors(long n) { ArrayList<Long>h= new ArrayList<>(); // Print the number of 2s that divide n if(n%2 ==0) {h.add(2L);} // n must be odd at this point. So we can // skip one element (Note i = i +2) for (long i = 3; i <= Math.sqrt(n); i+= 2) { if(n%i==0) {h.add(i);} } if (n > 2) h.add(n); return h; } static boolean Divisors(long n){ if(n%2==1) { return true; } for (long i=2; i<=Math.sqrt(n); i++){ if (n%i==0 && i%2==1){ return true; } } return false; } static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); public static void superSet(int[]a,ArrayList<String>al,int i,String s) { if(i==a.length) { al.add(s); return; } superSet(a,al,i+1,s); superSet(a,al,i+1,s+a[i]+" "); } public static long[] makeArr() { long size=in.nextInt(); long []arr=new long[(int)size]; for(int i=0;i<size;i++) { arr[i]=in.nextInt(); } return arr; } public static long[] arr(int n) { long []arr=new long[n+1]; for(int i=1;i<n+1;i++) { arr[i]=in.nextLong(); } return arr; } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } static void println(long x) { out.println(x); } static void print(long c) { out.print(c); } static void print(int c) { out.print(c); } static void println(int x) { out.println(x); } static void print(String s) { out.print(s); } static void println(String s) { out.println(s); } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } public static void reverse(int[] array) { int n = array.length; for (int i = 0; i < n / 2; i++) { int temp = array[i]; array[i] = array[n - i - 1]; array[n - i - 1] = temp; } } public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; for( int j=1;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } static int searchMax(int index,long[]inp) { while(index+1<inp.length &&inp[index+1]>inp[index]) { index+=1; } return index; } static int searchMin(int index,long[]inp) { while(index+1<inp.length &&inp[index+1]<inp[index]) { index+=1; } return index; } public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } static class Pairr implements Comparable<Pairr>{ private int index; private int cumsum; private ArrayList<Integer>indices; public Pairr(int index,int cumsum) { this.index=index; this.cumsum=cumsum; indices=new ArrayList<Integer>(); } public int compareTo(Pairr other) { return Integer.compare(cumsum, other.cumsum); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
50ec24e1002db9998e174fd36c7755b5
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; //import javafx.util.*; public class Main { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<Integer> g[]; //static ArrayList<ArrayList<TASK>> t; static long mod=(long)(1e9+7); static boolean set[],post[][]; static int prime[],c[]; static int par[]; // static int dp[][]; static HashMap<String,Long> mp; static long max=1; static boolean temp[][]; static int K=0; static int size[],dp[][],iv_A[],iv_B[]; static long modulo = 998244353; public static void main(String args[])throws IOException { /* * star,rope,TPST * BS,LST,MS,MQ */ int t = i(); while(t-- > 0){ int n = i(); int x = i(); int[] arr = input(n); int[] temp = new int[n+1]; Arrays.fill(temp, Integer.MIN_VALUE); for(int i = 0;i < n;i++){ int curr = 0; for(int j = i;j < n;j++){ curr += arr[j]; temp[j - i + 1] = Math.max(temp[j - i + 1],curr); } } for(int k = 0;k <= n;k++){ int ans = 0; for(int j = 1;j <= n;j++){ ans = Math.max(ans, temp[j] + Math.min(j,k)*x); } System.out.print(ans + " "); } System.out.println(); } } static int kadane(int a[]) { int size = a.length; int max_so_far = Integer.MIN_VALUE, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } static int maxSubArraySum(int a[], int size) { int max_so_far = Integer.MIN_VALUE, max_ending_here = 0,start = 0, end = 0, s = 0; for (int i = 0; i < size; i++) { max_ending_here += a[i]; if (max_so_far < max_ending_here) { max_so_far = max_ending_here; start = s; end = i; } if (max_ending_here < 0) { max_ending_here = 0; s = i + 1; } } return (end - start + 1); } static Boolean isSubsetSum(int n, int arr[], int sum){ // code here boolean[][] dp = new boolean[n+1][sum+1]; for(int i = 0;i < n+1;i++){ for(int j = 0;j < sum + 1;j++){ if(i == 0){ dp[i][j] = false; } if(j == 0){ dp[i][j] = true; } } } for(int i = 1;i < n+1;i++){ for(int j = 1;j < sum + 1;j++){ if(arr[i-1] <= j){ dp[i][j] = dp[i-1][j - arr[i-1]] || dp[i-1][j]; }else{ dp[i][j] = dp[i-1][j]; } } } return dp[n][sum]; } static int getmax(ArrayList<Integer> arr){ int n = arr.size(); int max = Integer.MIN_VALUE; for(int i = 0;i < n;i++){ max = Math.max(max,arr.get(i)); } return max; } static boolean check(ArrayList<Integer> arr){ int n = arr.size(); boolean flag = true; for(int i = 0;i < n-1;i++){ if(arr.get(i) != arr.get(i+1)){ flag = false; break; } } return flag; } static int MinimumFlips(String s, int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = (s.charAt(i) == '1' ? 1 : 0); } // Initialize prefix arrays to store // number of changes required to put // 1s at either even or odd position int[] oddone = new int[n + 1]; int[] evenone = new int[n + 1]; oddone[0] = 0; evenone[0] = 0; for (int i = 0; i < n; i++) { // If i is odd if (i % 2 != 0) { // Update the oddone // and evenone count oddone[i + 1] = oddone[i] + (a[i] == 1 ? 1 : 0); evenone[i + 1] = evenone[i] + (a[i] == 0 ? 1 : 0); } // Else i is even else { // Update the oddone // and evenone count oddone[i + 1] = oddone[i] + (a[i] == 0 ? 1 : 0); evenone[i + 1] = evenone[i] + (a[i] == 1 ? 1 : 0); } } // Initialize minimum flips return Math.min(evenone[n],oddone[n]); } static int nextPowerOf2(int n) { int count = 0; // First n in the below // condition is for the // case where n is 0 if (n > 0 && (n & (n - 1)) == 0){ while(n != 1) { n >>= 1; count += 1; } return count; }else{ while(n != 0) { n >>= 1; count += 1; } return count; } } static int length(int n){ int count = 0; while(n > 0){ n = n/10; count++; } return count; } static boolean isPrimeInt(int N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } public static int lcs(int[] nums) { int[] tails = new int[nums.length]; int size = 0; for (int x : nums) { int i = 0, j = size; while (i != j) { int m = (i + j) / 2; if (tails[m] <= x) i = m + 1; else j = m; } tails[i] = x; if (i == size) ++size; } return size; } static int CeilIndex(int A[], int l, int r, int key) { while (r - l > 1) { int m = l + (r - l) / 2; if (A[m] >= key) r = m; else l = m; } return r; } static int f(int A[], int size) { // Add boundary case, when array size is one int[] tailTable = new int[size]; int len; // always points empty slot tailTable[0] = A[0]; len = 1; for (int i = 1; i < size; i++) { if (A[i] < tailTable[0]) // new smallest value tailTable[0] = A[i]; else if (A[i] > tailTable[len - 1]) // A[i] wants to extend largest subsequence tailTable[len++] = A[i]; else // A[i] wants to be current end candidate of an existing // subsequence. It will replace ceil value in tailTable tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]; } return len; } static int containsBoth(char X[],int N) { for(int i=1; i<N; i++)if(X[i]!=X[i-1])return i-1; return -1; } static void f(char X[],int N,int A[]) { int c=0; for(int i=N-1; i>=0; i--) { if(X[i]=='1') { A[i]=c; } else c++; A[i]+=A[i+1]; } } static int f(int i,int j,char X[],char Y[],int zero) { if(i==X.length && j==Y.length)return 0; if(i==X.length)return iv_B[j]; //return inversion count here if(j==Y.length)return iv_A[i]; if(dp[i][j]==-1) { int cost_x=0,cost_y=0; if(X[i]=='1') { cost_x=zero+f(i+1,j,X,Y,zero); } else cost_x=f(i+1,j,X,Y,zero-1); if(Y[j]=='1') { cost_y=zero+f(i,j+1,X,Y,zero); } else cost_y=f(i,j+1,X,Y,zero-1); dp[i][j]=Math.min(cost_x, cost_y); } return dp[i][j]; } static boolean f(long last,long next,long l,long r,long A,long B) { while(l<=r) { long m=(l+r)/2; long s=((m)*(m-1))/2; long l1=(A*m)+s,r1=(m*B)-s; if(Math.min(next, r1)<Math.max(last, l1)) { if(l1>last)r=m-1; else l=m+1; } else return true; } return false; } static boolean isVowel(char x) { if(x=='a' || x=='e' || x=='i' ||x=='u' || x=='o')return true; return false; } static boolean f(int i,int j) { //i is no of one //j is no of ai>1 if(i==0 && j==0)return true; //this player has no pile to pick --> last move iska rha hoga if(dp[i][j]==-1) { boolean f=false; if(i>0) { if(!f(i-1,j))f=true; } if(j>0) { if(!f(i,j-1) && !f(i+1,j-1))f=true; } if(f)dp[i][j]=1; else dp[i][j]=0; } return dp[i][j]==1; } static int last=-1; static void dfs(int n,int p) { last=n; System.out.println("n--> "+n+" p--> "+p); for(int c:g[n]) { if(c!=p)dfs(c,n); } } static long abs(long a,long b) { return Math.abs(a-b); } static int lower(long A[],long x) { int l=0,r=A.length; while(r-l>1) { int m=(l+r)/2; if(A[m]<=x)l=m; else r=m; } return l; } static int f(int i,int s,int j,int N,int A[],HashMap<Integer,Integer> mp) { if(i==N) { return s; } if(dp[i][j]==-1) { if(mp.containsKey(A[i])) { int f=mp.get(A[i]); int c=0; if(f==1)c++; mp.put(A[i], f+1); HashMap<Integer,Integer> temp=new HashMap<>(); temp.put(A[i],1); return dp[i][j]=Math.min(f(i+1,s+1+c,j,N,A,mp), s+K+f(i+1,0,i,N,A,temp)); } else { mp.put(A[i],1); return dp[i][j]=f(i+1,s,j,N,A,mp); } } return dp[i][j]; } static boolean inRange(int a,int l,int r) { if(l<=a && r>=a)return true; return false; } static long f(long a,long b) { if(a%b==0)return a/b; else return (a/b)+f(b,a%b); } static void f(int index,long A[],int i,long xor) { if(index+1==A.length) { if(valid(xor^A[index],i)) { xor=xor^A[index]; max=Math.max(max, i); } return; } if(dp[index][i]==0) { dp[index][i]=1; if(valid(xor^A[index],i)) { f(index+1,A,i+1,0); f(index+1,A,i,xor^A[index]); } else { f(index+1,A,i,xor^A[index]); } } } static boolean valid(long xor,int i) { if(xor==0)return true; while(xor%2==0 ) { xor/=2; i--; } return i<=0; } static int next(int i,long pre[],long S) { int l=i,r=pre.length; while(r-l>1) { int m=(l+r)/2; if(pre[m]-pre[i-1]>S)r=m; else l=m; } return r; } static boolean lexo(long A[],long B[]) { for(int i=0; i<A.length; i++) { if(B[i]>A[i])return true; if(A[i]>B[i])return false; } return false; } static long [] f(long A[],long B[],int j) { int N=A.length; long X[]=new long[N]; for(int i=0; i<N; i++) { X[i]=(B[j]+A[i])%N; j++; j%=N; } return X; } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { b=find(b); a=find(a); if(a!=b) { par[b]=a; } } static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } static long lower_Bound(long A[],int low,int high, long x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void setGraph(int N) { size=new int[N+1]; // D=new int[N+1]; g=new ArrayList[N+1]; for(int i=0; i<=N; i++) { g[i]=new ArrayList<>(); } } static long pow(long a,long b) { long pow=1L; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(int x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
d3af0e34300ab8b1b7ab73b4bed7098e
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; //import javafx.util.*; public class Main { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<Integer> g[]; //static ArrayList<ArrayList<TASK>> t; static long mod=(long)(1e9+7); static boolean set[],post[][]; static int prime[],c[]; static int par[]; // static int dp[][]; static HashMap<String,Long> mp; static long max=1; static boolean temp[][]; static int K=0; static int size[],dp[][],iv_A[],iv_B[]; static long modulo = 998244353; public static void main(String args[])throws IOException { /* * star,rope,TPST * BS,LST,MS,MQ */ int t = i(); while(t-- > 0){ int n = i(); int x = i(); int[] arr = input(n); int[] temp = new int[n+1]; Arrays.fill(temp, Integer.MIN_VALUE); for(int i = 0;i < n;i++){ int curr = 0; for(int j = i;j < n;j++){ curr += arr[j]; temp[j - i + 1] = Math.max(temp[j - i + 1],curr); } } for(int k = 0;k <= n;k++){ int ans = 0; for(int j = 0;j <= n;j++){ ans = Math.max(ans, temp[j] + Math.min(j,k)*x); } System.out.print(ans + " "); } System.out.println(); } } static int kadane(int a[]) { int size = a.length; int max_so_far = Integer.MIN_VALUE, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } static int maxSubArraySum(int a[], int size) { int max_so_far = Integer.MIN_VALUE, max_ending_here = 0,start = 0, end = 0, s = 0; for (int i = 0; i < size; i++) { max_ending_here += a[i]; if (max_so_far < max_ending_here) { max_so_far = max_ending_here; start = s; end = i; } if (max_ending_here < 0) { max_ending_here = 0; s = i + 1; } } return (end - start + 1); } static Boolean isSubsetSum(int n, int arr[], int sum){ // code here boolean[][] dp = new boolean[n+1][sum+1]; for(int i = 0;i < n+1;i++){ for(int j = 0;j < sum + 1;j++){ if(i == 0){ dp[i][j] = false; } if(j == 0){ dp[i][j] = true; } } } for(int i = 1;i < n+1;i++){ for(int j = 1;j < sum + 1;j++){ if(arr[i-1] <= j){ dp[i][j] = dp[i-1][j - arr[i-1]] || dp[i-1][j]; }else{ dp[i][j] = dp[i-1][j]; } } } return dp[n][sum]; } static int getmax(ArrayList<Integer> arr){ int n = arr.size(); int max = Integer.MIN_VALUE; for(int i = 0;i < n;i++){ max = Math.max(max,arr.get(i)); } return max; } static boolean check(ArrayList<Integer> arr){ int n = arr.size(); boolean flag = true; for(int i = 0;i < n-1;i++){ if(arr.get(i) != arr.get(i+1)){ flag = false; break; } } return flag; } static int MinimumFlips(String s, int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = (s.charAt(i) == '1' ? 1 : 0); } // Initialize prefix arrays to store // number of changes required to put // 1s at either even or odd position int[] oddone = new int[n + 1]; int[] evenone = new int[n + 1]; oddone[0] = 0; evenone[0] = 0; for (int i = 0; i < n; i++) { // If i is odd if (i % 2 != 0) { // Update the oddone // and evenone count oddone[i + 1] = oddone[i] + (a[i] == 1 ? 1 : 0); evenone[i + 1] = evenone[i] + (a[i] == 0 ? 1 : 0); } // Else i is even else { // Update the oddone // and evenone count oddone[i + 1] = oddone[i] + (a[i] == 0 ? 1 : 0); evenone[i + 1] = evenone[i] + (a[i] == 1 ? 1 : 0); } } // Initialize minimum flips return Math.min(evenone[n],oddone[n]); } static int nextPowerOf2(int n) { int count = 0; // First n in the below // condition is for the // case where n is 0 if (n > 0 && (n & (n - 1)) == 0){ while(n != 1) { n >>= 1; count += 1; } return count; }else{ while(n != 0) { n >>= 1; count += 1; } return count; } } static int length(int n){ int count = 0; while(n > 0){ n = n/10; count++; } return count; } static boolean isPrimeInt(int N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } public static int lcs(int[] nums) { int[] tails = new int[nums.length]; int size = 0; for (int x : nums) { int i = 0, j = size; while (i != j) { int m = (i + j) / 2; if (tails[m] <= x) i = m + 1; else j = m; } tails[i] = x; if (i == size) ++size; } return size; } static int CeilIndex(int A[], int l, int r, int key) { while (r - l > 1) { int m = l + (r - l) / 2; if (A[m] >= key) r = m; else l = m; } return r; } static int f(int A[], int size) { // Add boundary case, when array size is one int[] tailTable = new int[size]; int len; // always points empty slot tailTable[0] = A[0]; len = 1; for (int i = 1; i < size; i++) { if (A[i] < tailTable[0]) // new smallest value tailTable[0] = A[i]; else if (A[i] > tailTable[len - 1]) // A[i] wants to extend largest subsequence tailTable[len++] = A[i]; else // A[i] wants to be current end candidate of an existing // subsequence. It will replace ceil value in tailTable tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]; } return len; } static int containsBoth(char X[],int N) { for(int i=1; i<N; i++)if(X[i]!=X[i-1])return i-1; return -1; } static void f(char X[],int N,int A[]) { int c=0; for(int i=N-1; i>=0; i--) { if(X[i]=='1') { A[i]=c; } else c++; A[i]+=A[i+1]; } } static int f(int i,int j,char X[],char Y[],int zero) { if(i==X.length && j==Y.length)return 0; if(i==X.length)return iv_B[j]; //return inversion count here if(j==Y.length)return iv_A[i]; if(dp[i][j]==-1) { int cost_x=0,cost_y=0; if(X[i]=='1') { cost_x=zero+f(i+1,j,X,Y,zero); } else cost_x=f(i+1,j,X,Y,zero-1); if(Y[j]=='1') { cost_y=zero+f(i,j+1,X,Y,zero); } else cost_y=f(i,j+1,X,Y,zero-1); dp[i][j]=Math.min(cost_x, cost_y); } return dp[i][j]; } static boolean f(long last,long next,long l,long r,long A,long B) { while(l<=r) { long m=(l+r)/2; long s=((m)*(m-1))/2; long l1=(A*m)+s,r1=(m*B)-s; if(Math.min(next, r1)<Math.max(last, l1)) { if(l1>last)r=m-1; else l=m+1; } else return true; } return false; } static boolean isVowel(char x) { if(x=='a' || x=='e' || x=='i' ||x=='u' || x=='o')return true; return false; } static boolean f(int i,int j) { //i is no of one //j is no of ai>1 if(i==0 && j==0)return true; //this player has no pile to pick --> last move iska rha hoga if(dp[i][j]==-1) { boolean f=false; if(i>0) { if(!f(i-1,j))f=true; } if(j>0) { if(!f(i,j-1) && !f(i+1,j-1))f=true; } if(f)dp[i][j]=1; else dp[i][j]=0; } return dp[i][j]==1; } static int last=-1; static void dfs(int n,int p) { last=n; System.out.println("n--> "+n+" p--> "+p); for(int c:g[n]) { if(c!=p)dfs(c,n); } } static long abs(long a,long b) { return Math.abs(a-b); } static int lower(long A[],long x) { int l=0,r=A.length; while(r-l>1) { int m=(l+r)/2; if(A[m]<=x)l=m; else r=m; } return l; } static int f(int i,int s,int j,int N,int A[],HashMap<Integer,Integer> mp) { if(i==N) { return s; } if(dp[i][j]==-1) { if(mp.containsKey(A[i])) { int f=mp.get(A[i]); int c=0; if(f==1)c++; mp.put(A[i], f+1); HashMap<Integer,Integer> temp=new HashMap<>(); temp.put(A[i],1); return dp[i][j]=Math.min(f(i+1,s+1+c,j,N,A,mp), s+K+f(i+1,0,i,N,A,temp)); } else { mp.put(A[i],1); return dp[i][j]=f(i+1,s,j,N,A,mp); } } return dp[i][j]; } static boolean inRange(int a,int l,int r) { if(l<=a && r>=a)return true; return false; } static long f(long a,long b) { if(a%b==0)return a/b; else return (a/b)+f(b,a%b); } static void f(int index,long A[],int i,long xor) { if(index+1==A.length) { if(valid(xor^A[index],i)) { xor=xor^A[index]; max=Math.max(max, i); } return; } if(dp[index][i]==0) { dp[index][i]=1; if(valid(xor^A[index],i)) { f(index+1,A,i+1,0); f(index+1,A,i,xor^A[index]); } else { f(index+1,A,i,xor^A[index]); } } } static boolean valid(long xor,int i) { if(xor==0)return true; while(xor%2==0 ) { xor/=2; i--; } return i<=0; } static int next(int i,long pre[],long S) { int l=i,r=pre.length; while(r-l>1) { int m=(l+r)/2; if(pre[m]-pre[i-1]>S)r=m; else l=m; } return r; } static boolean lexo(long A[],long B[]) { for(int i=0; i<A.length; i++) { if(B[i]>A[i])return true; if(A[i]>B[i])return false; } return false; } static long [] f(long A[],long B[],int j) { int N=A.length; long X[]=new long[N]; for(int i=0; i<N; i++) { X[i]=(B[j]+A[i])%N; j++; j%=N; } return X; } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { b=find(b); a=find(a); if(a!=b) { par[b]=a; } } static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } static long lower_Bound(long A[],int low,int high, long x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void setGraph(int N) { size=new int[N+1]; // D=new int[N+1]; g=new ArrayList[N+1]; for(int i=0; i<=N; i++) { g[i]=new ArrayList<>(); } } static long pow(long a,long b) { long pow=1L; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(int x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
7bbac4c3fd0413b2b52ee3ac6de7c2d7
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import javax.print.attribute.IntegerSyntax; import java.net.CookieHandler; import java.util.*; import java.io.*; //import static com.sun.tools.javac.jvm.ByteCodes.swap; // ?)(? public class fastTemp { static FastScanner fs = null; static ArrayList<Long>[] graph; static int mod = 998244353; static int[] bit; static int[] arr; static int hhh; static int hmm; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); outer: while (t-- > 0) { int n = fs.nextInt(); int z = fs.nextInt(); int[] a = fs.readArray(n); int dp[][] = new int[n+1][n]; for(int i=0;i<=n;i++){ Arrays.fill(dp[i],Integer.MIN_VALUE); } for(int i=0;i<=n;i++){ for(int j=0;j<n;j++){ if(j==0){ dp[i][j] = a[j]; if(i>0){ dp[i][j] = Math.max(dp[i][j],a[j]+z); } }else{ dp[i][j] = Math.max(a[j],dp[i][j-1]+a[j]); if(i>0) { dp[i][j] = Math.max(dp[i][j], Math.max(z + a[j], dp[i - 1][j - 1] + a[j] + z)); } } } } for(int i=0;i<=n;i++){ int max = 0; for(int j=0;j<n;j++){ if(dp[i][j]>max){ max = dp[i][j]; } } out.print(max+" "); } out.println(); } out.close(); } public static void solve(int n){ if(n == 3){ } } static class Pair1 { String s; int x; int y; Pair1(String s, int x, int y) { this.s = s; this.x = x; this.y = y; } } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return o.x - this.x; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readlongArray(int n){ long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
d7d203531e8b49efc9fef2c85eaaea35
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int T=sc.nextInt(); while(T-->0) { int n=sc.nextInt(); int x=sc.nextInt(); int[] a=new int[n+1]; for(int i=1;i<=n;i++) a[i]=sc.nextInt(); int[] pre=new int[n+1]; for(int i=1;i<=n;i++) pre[i]=pre[i-1]+a[i]; HashMap<Integer,Range> map=new HashMap<>(); map.put(0,new Range(0,-1,0)); for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { Range r=new Range(i,j,pre[j]-pre[i-1]); if(!map.containsKey(r.getLength())) map.put(r.getLength(),r); else { if(r.getSum()>map.get(r.getLength()).getSum()) map.put(r.getLength(),r); } } } for(int i=0;i<=n;i++) { int max=Integer.MIN_VALUE; for(int len:map.keySet()) { Range r=map.get(len); int temp=r.getSum(); if(r.getY()-r.getX()+1<i) temp+=(r.getY()-r.getX()+1)*x; else temp+=i*x; max=Math.max(temp,max); } System.out.print(max+" "); } System.out.println(); } sc.close(); } } class Range { private final int x; private final int y; private final int sum; public Range(int x,int y,int sum) { this.x=x; this.y=y; this.sum=sum; } public int getX() { return x; } public int getY() { return y; } public int getSum() { return sum; } public int getLength() { return y-x+1; } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
0c7fdc90d5f62847e8be556c7afd7221
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Solution { public static int maxSubArraySum(int a[]) { int size = a.length; int max_so_far = Integer.MIN_VALUE, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } public static void main (String[] args) throws java.lang.Exception { Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t>0){ int n=s.nextInt(); int k=s.nextInt(); int[] arr=new int[n]; int[] maxSum=new int[n+1]; for(int i=0;i<n;i++) arr[i]=s.nextInt(); for(int subSize=1;subSize<=n;subSize++){ int sum=0; int max=Integer.MIN_VALUE; for(int ind=0;ind<subSize;ind++){ sum+=arr[ind]; } max=Math.max(max,sum); for(int ind=subSize;ind<n;ind++){ sum+=arr[ind]; sum-=arr[ind-subSize]; max=Math.max(sum,max); } maxSum[subSize]=max; } int maxForZero=Math.max(0,maxSubArraySum(arr)); System.out.print(maxForZero+" "); for(int kn=1;kn<=n;kn++){ int maxAnswer=-1; for(int subSize=kn;subSize<=n;subSize++){ int cur=maxSum[subSize]+kn*k; maxAnswer=Math.max(cur,maxAnswer); } if(maxAnswer>maxForZero){ maxForZero=maxAnswer; } System.out.print(maxForZero+" "); } System.out.println(); t--; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
8be5b19cd8f4b4e5e1f9960a9867dade
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class CodeChef2 { static boolean lazy[]; static int val[]; static boolean colupd[]; static int mod = 998244353; public static void main(String args[]) throws Exception { FastReader fr = new FastReader(); int t = fr.nextInt(); // int t = 1; PrintWriter out = new PrintWriter(System.out); while (t-- > 0) { int n = fr.nextInt(); long x = fr.nextInt(); int arr[] = new int[n]; int sm[] = new int[n+1]; for(int i = 0; i < n; i++) { arr[i] = fr.nextInt(); sm[i+1] = sm[i] + arr[i]; } long maxsm[] = new long[n+1]; maxsm[0] = 0; for(int i = 1; i <= n; i++) { maxsm[i] = Integer.MIN_VALUE; for(int j = i; j <= n; j++) { long ts = (long)(sm[j] - sm[j - i]); if(ts > maxsm[i]) maxsm[i] = ts; } } for(int j = 0; j <= n; j++) { long cs = 0; for(int i = 0; i <= n; i++) { long l = Math.min(j, i); long ts = l * x + maxsm[i]; if(ts > cs) cs = ts; } out.print(cs + " "); } out.println(); } out.close(); } static void print(int arr[], PrintWriter out) { for(int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } static int check(int x) { StringBuilder sb = new StringBuilder(); sb.append(Integer.toBinaryString(x)); sb = sb.reverse(); int y = x ^ Integer.parseInt(sb.toString(), 2); if(y == 0) return 0; return check(y) + 1; } static int modExpo(int a, int n, int m) { a = a%m; if(a <= 1 || n ==1) return a; else if(n == 0) return 1; long p = modExpo(a, n/2, m); p = (p * p)%m; if(n%2 == 1) p = (p * (long)a)%m; return (int)p; } static int[] buildST(int arr[]) { int n = arr.length; int p = (int)(Math.ceil(Math.log(n)/Math.log(2))); n = (int)Math.pow(2, p); int size = 2*n - 1; int st[] = new int[size]; for(int i = 0; i < arr.length; i++) { st[n-1 + i] = arr[i]; } return st; } static long query(int col[], long val[], int l, int i, int s, int e) { if(l > e || l < s) return 0; if(s == e) { return val[i]; } int m = (s+e)/2; // if(val[i] != 0) { // updateV(val, col, 2*i + 1, s, m, col[2*i + 1], val[i]); // updateV(val, col, 2*i + 2, m+1, e, col[2*i + 2], val[i]); // val[i] = 0; // } long v = 0; if(l <= m) v = query(col, val, l, 2*i+1, s, m); else v = query(col, val, l, 2*i+2, m+1, e); return val[i] + v; } static void updateC(int col[], int l, int r, int i, int s, int e, int c) { if(l > r || l > e || r < s || col[i] == c) return; if(l <= s && e <= r) { col[i] = c; return; } int m = (s + e)/2; if(col[i] != 0 && col[i] != c) { updateC(col, s, m, 2*i + 1, s, m, col[i]); updateC(col, m+1, e, 2*i + 2, m+1, e, col[i]); col[i] = 0; } updateC(col, l, Math.min(m, r), 2*i + 1, s, m, c); updateC(col, Math.max(l, m+1), r, 2*i + 2, m+1, e, c); col[i] = (col[2*i + 1] != col[2*i + 2] || col[2*i + 1] == 0? 0 : c); } static void updateV(long val[], int col[], int i, int s, int e, int c, long v) { if(col[i] != 0 && col[i] != c) return; if(col[i] == c) { val[i] += v; return; } int m = (s + e)/2; // if(val[i] != 0) { // updateV(val, col, 2*i + 1, s, m, col[2*i + 1], val[i]); // updateV(val, col, 2*i + 2, m+1, e, col[2*i + 2], val[i]); // val[i] = 0; // } updateV(val, col,2*i + 1, s, m, c, v); updateV(val, col,2*i + 2, m+1, e, c, v); } static class Pair implements Comparable<Pair>{ int i; int v; Pair(int i, int j) { this.i = i; v = j; } public int compareTo(Pair o) { if(this.v < o.v) return -1; else if(this.v > o.v) return 1; else if(this.i < o.i) return -1; return 1; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } boolean hasMore() { try { return br.ready(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
15e613a86eb7010ea6193602e798b493
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
/* * Everything is Hard * Before Easy * Jai Mata Dii */ import java.util.*; import java.io.*; 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()){try{st = new StringTokenizer(br.readLine());}catch (IOException e){e.printStackTrace();}}return st.nextToken();}int nextInt(){ return Integer.parseInt(next());}long nextLong(){return Long.parseLong(next());}double nextDouble(){return Double.parseDouble(next());}String nextLine(){String str = ""; try{str = br.readLine(); } catch (IOException e) {e.printStackTrace();} return str; }} static long mod = (long)(1e9+7); // static long mod = 998244353; // static Scanner sc = new Scanner(System.in); static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static void main (String[] args) { int ttt = 1; ttt = sc.nextInt(); z :for(int tc=1;tc<=ttt;tc++){ int n = sc.nextInt(); long x = sc.nextInt(); long a[] = new long[n]; for(int i=0;i<n;i++) { a[i] = sc.nextLong(); } long dp[][] = new long[n+1][n]; for(int i=0;i<=n;i++) { for(int j=0;j<n;j++) { if(j == 0) { dp[i][j] = a[j]; if(i>=1) { dp[i][j] = Math.max(dp[i][j], a[j]+x); } // if(i == 1) System.out.println(dp[i][j][1]); } else { dp[i][j] = Math.max(a[j], a[j] + dp[i][j-1]); if(i>0) { dp[i][j] = Math.max(a[j]+x, Math.max(dp[i][j], a[j]+x+dp[i-1][j-1])); } } } } for(int i=0;i<=n;i++) { long cur = 0; for(int j=0;j<n;j++) { cur = Math.max(cur, dp[i][j]); } out.write(cur+" "); } out.write("\n"); } out.close(); } static long pow(long a, long b){long ret = 1;while(b>0){if(b%2 == 0){a = (a*a)%mod;b /= 2;}else{ret = (ret*a)%mod;b--;}}return ret%mod;} static long gcd(long a,long b){if(b==0) return a; return gcd(b,a%b); } private static void sort(int[] a) {List<Integer> k = new ArrayList<>();for(int val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);} private static void ini(List<Integer>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}} private static void init(List<int[]>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}} private static void sort(long[] a) {List<Long> k = new ArrayList<>();for(long val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);} }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
8d6a555e84beba118f99e4d7e21ce3f8
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class A { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static int g[][]; static long mod=(long) 998244353,INF=Long.MAX_VALUE; static boolean set[]; static int max=0; static int lca[][]; static int par[],col[],D[]; static long fact[]; static int size[],N; static int dp[][],dp2[][]; static ArrayList [] seg; public static void main(String args[])throws IOException { /* * star,rope,TPST * BS,LST,MS,MQ */ int T=i(); outer:while(T-->0) { int N=i(); long K=l(); long A[]=inputLong(N); long pre[]=new long[N+1]; for(int i=1; i<=N; i++)pre[i]=pre[i-1]+A[i-1]; long left[]=new long[N+5],right[]=new long[N+5]; for(int i=0; i<N; i++) { long s=0,temp=0; for(int j=i; j>=0; j--) { temp+=A[j]; s=Math.max(temp, s); } left[i+1]=s; s=0; temp=0; for(int j=i; j<N; j++) { temp+=A[j]; s=Math.max(temp, s); } right[i+1]=s; } long max[]=new long[N+1]; max[0]=kadane(A); for(int k=1; k<=N; k++) { long n=k; long sum=n*K; long m=0; for(int i=1; i<=N; i++) { int j=i+k-1; if(j>N)break; long temp=pre[j]-pre[i-1]; temp+=sum; temp+=left[i-1]; temp+=right[j+1]; m=Math.max(m, temp); } max[k]=m; } for(int i=1; i<=N; i++)max[i]=Math.max(max[i-1], max[i]); for(long a:max)ans.append(a+" "); ans.append("\n"); } out.println(ans); out.close(); } static long kadane(long A[]) { long lsum=A[0],gsum=0; gsum=Math.max(gsum, lsum); for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } public static boolean pal(int i) { StringBuilder sb=new StringBuilder(); StringBuilder rev=new StringBuilder(); int p=1; while(p<=i) { if((i&p)!=0) { sb.append("1"); } else sb.append("0"); p<<=1; } rev=new StringBuilder(sb.toString()); rev.reverse(); if(i==8)System.out.println(sb+" "+rev); return (sb.toString()).equals(rev.toString()); } public static void reverse(int i,int j,int A[]) { while(i<j) { int t=A[i]; A[i]=A[j]; A[j]=t; i++; j--; } } public static int ask(int a,int b,int c) { System.out.println("? "+a+" "+b+" "+c); return i(); } static int[] reverse(int A[],int N) { int B[]=new int[N]; for(int i=N-1; i>=0; i--) { B[N-i-1]=A[i]; } return B; } static boolean isPalin(char X[]) { int i=0,j=X.length-1; while(i<=j) { if(X[i]!=X[j])return false; i++; j--; } return true; } static int distance(int a,int b) { int d=D[a]+D[b]; int l=LCA(a,b); l=2*D[l]; return d-l; } static int LCA(int a,int b) { if(D[a]<D[b]) { int t=a; a=b; b=t; } int d=D[a]-D[b]; int p=1; for(int i=0; i>=0 && p<=d; i++) { if((p&d)!=0) { a=lca[a][i]; } p<<=1; } if(a==b)return a; for(int i=max-1; i>=0; i--) { if(lca[a][i]!=-1 && lca[a][i]!=lca[b][i]) { a=lca[a][i]; b=lca[b][i]; } } return lca[a][0]; } static void dfs(int n,int p) { lca[n][0]=p; if(p!=-1)D[n]=D[p]+1; for(int c:g[n]) { if(c!=p) { dfs(c,n); } } } static int[] prefix_function(char X[])//returns pi(i) array { int N=X.length; int pre[]=new int[N]; for(int i=1; i<N; i++) { int j=pre[i-1]; while(j>0 && X[i]!=X[j]) j=pre[j-1]; if(X[i]==X[j])j++; pre[i]=j; } return pre; } static TreeNode start; public static void f(TreeNode root,TreeNode p,int r) { if(root==null)return; if(p!=null) { root.par=p; } if(root.val==r)start=root; f(root.left,root,r); f(root.right,root,r); } static int right(int A[],int Limit,int l,int r) { while(r-l>1) { int m=(l+r)/2; if(A[m]<Limit)l=m; else r=m; } return l; } static int left(int A[],int a,int l,int r) { while(r-l>1) { int m=(l+r)/2; if(A[m]<a)l=m; else r=m; } return l; } // static void build(int v,int tl,int tr,int A[]) // { // if(tl==tr) // { // seg[v]=A[tl]; // return; // } // int tm=(tl+tr)/2; // build(v*2,tl,tm,A); // build(v*2+1,tm+1,tr,A); // if((tm+1-tl)%2==0) // seg[v]=seg[v*2]+seg[v*2+1]; // else seg[v]=seg[v*2]-seg[v*2+1]; // } static void update(int v,int tl,int tr,int l,int r,int a) { if(l>r)return; if(l==tl && tr==tr) { seg[v].add(a); } else { int tm=(tl+tr)/2; update(v*2,tl,tm,l,Math.min(r, tm),a); update(v*2+1,tm+1,tr,Math.max(tm+1, l),r,a); } } // static void ask(int v,int tl,int tr,int index) // { // // //if(l>r)return 0; // if(tl==index && tl==tr) // { // // } // int tm=(tl+tr)/2; // // return ask(v*2,tl,tm,l,Math.min(tm, r))+ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r); // } static boolean f(long A[],long m,int N) { long B[]=new long[N]; for(int i=0; i<N; i++) { B[i]=A[i]; } for(int i=N-1; i>=0; i--) { if(B[i]<m)return false; if(i>=2) { long extra=Math.min(B[i]-m, A[i]); long x=extra/3L; B[i-2]+=2L*x; B[i-1]+=x; } } return true; } static int f(int l,int r,long A[],long x) { while(r-l>1) { int m=(l+r)/2; if(A[m]>=x)l=m; else r=m; } return r; } static boolean f(long m,long H,long A[],int N) { long s=m; for(int i=0; i<N-1;i++) { s+=Math.min(m, A[i+1]-A[i]); } return s>=H; } static long ask(long l,long r) { System.out.println("? "+l+" "+r); return l(); } static long f(long N,long M) { long s=0; if(N%3==0) { N/=3; s=N*M; } else { long b=N%3; N/=3; N++; s=N*M; N--; long a=N*M; if(M%3==0) { M/=3; a+=(b*M); } else { M/=3; M++; a+=(b*M); } s=Math.min(s, a); } return s; } static int ask(StringBuilder sb,int a) { System.out.println(sb+""+a); return i(); } static void swap(char X[],int i,int j) { char x=X[i]; X[i]=X[j]; X[j]=x; } static int min(int a,int b,int c) { return Math.min(Math.min(a, b), c); } static long and(int i,int j) { System.out.println("and "+i+" "+j); return l(); } static long or(int i,int j) { System.out.println("or "+i+" "+j); return l(); } static int len=0,number=0; static void f(char X[],int i,int num,int l) { if(i==X.length) { if(num==0)return; //update our num if(isPrime(num))return; if(l<len) { len=l; number=num; } return; } int a=X[i]-'0'; f(X,i+1,num*10+a,l+1); f(X,i+1,num,l); } static boolean is_Sorted(int A[]) { int N=A.length; for(int i=1; i<=N; i++)if(A[i-1]!=i)return false; return true; } static boolean f(StringBuilder sb,String Y,String order) { StringBuilder res=new StringBuilder(sb.toString()); HashSet<Character> set=new HashSet<>(); for(char ch:order.toCharArray()) { set.add(ch); for(int i=0; i<sb.length(); i++) { char x=sb.charAt(i); if(set.contains(x))continue; res.append(x); } } String str=res.toString(); return str.equals(Y); } static boolean all_Zero(int f[]) { for(int a:f)if(a!=0)return false; return true; } static long form(int a,int l) { long x=0; while(l-->0) { x*=10; x+=a; } return x; } static int count(String X) { HashSet<Integer> set=new HashSet<>(); for(char x:X.toCharArray())set.add(x-'0'); return set.size(); } static int f(long K) { long l=0,r=K; while(r-l>1) { long m=(l+r)/2; if(m*m<K)l=m; else r=m; } return (int)l; } // static void build(int v,int tl,int tr,long A[]) // { // if(tl==tr) // { // seg[v]=A[tl]; // } // else // { // int tm=(tl+tr)/2; // build(v*2,tl,tm,A); // build(v*2+1,tm+1,tr,A); // seg[v]=Math.min(seg[v*2], seg[v*2+1]); // } // } static int [] sub(int A[],int B[]) { int N=A.length; int f[]=new int[N]; for(int i=N-1; i>=0; i--) { if(B[i]<A[i]) { B[i]+=26; B[i-1]-=1; } f[i]=B[i]-A[i]; } for(int i=0; i<N; i++) { if(f[i]%2!=0)f[i+1]+=26; f[i]/=2; } return f; } static int[] f(int N) { char X[]=in.next().toCharArray(); int A[]=new int[N]; for(int i=0; i<N; i++)A[i]=X[i]-'a'; return A; } static int max(int a ,int b,int c,int d) { a=Math.max(a, b); c=Math.max(c,d); return Math.max(a, c); } static int min(int a ,int b,int c,int d) { a=Math.min(a, b); c=Math.min(c,d); return Math.min(a, c); } static HashMap<Integer,Integer> Hash(int A[]) { HashMap<Integer,Integer> mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static long mul(long a, long b) { return ( a %mod * 1L * b%mod )%mod; } static void swap(int A[],int a,int b) { int t=A[a]; A[a]=A[b]; A[b]=t; } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { a=find(a); b=find(b); if(a!=b) { if(par[a]>par[b]) //this means size of a is less than that of b { int t=b; b=a; a=t; } par[a]+=par[b]; par[b]=a; } } static boolean isSorted(int A[]) { for(int i=1; i<A.length; i++) { if(A[i]<A[i-1])return false; } return true; } static boolean isDivisible(StringBuilder X,int i,long num) { long r=0; for(; i<X.length(); i++) { r=r*10+(X.charAt(i)-'0'); r=r%num; } return r==0; } static int lower_Bound(int A[],int low,int high, int x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static String f(String A) { String X=""; for(int i=A.length()-1; i>=0; i--) { int c=A.charAt(i)-'0'; X+=(c+1)%2; } return X; } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static String swap(String X,int i,int j) { char ch[]=X.toCharArray(); char a=ch[i]; ch[i]=ch[j]; ch[j]=a; return new String(ch); } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } // static void setGraph(int N,int nodes) // { //// size=new int[N+1]; // par=new int[N+1]; // col=new int[N+1]; //// g=new int[N+1][]; // D=new int[N+1]; // int deg[]=new int[N+1]; // int A[][]=new int[nodes][2]; // for(int i=0; i<nodes; i++) // { // int a=i(),b=i(); // A[i][0]=a; // A[i][1]=b; // deg[a]++; // deg[b]++; // } // for(int i=0; i<=N; i++) // { // g[i]=new int[deg[i]]; // deg[i]=0; // } // for(int a[]:A) // { // int x=a[0],y=a[1]; // g[x][deg[x]++]=y; // g[y][deg[y]++]=x; // } // } static long pow(long a,long b) { //long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(boolean A[][]) { for(boolean a[]:A)print(a); } static void print(long A[][]) { for(long a[]:A)print(a); } static void print(int A[][]) { for(int a[]:A)print(a); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } class segNode { long pref,suff,sum,max; segNode(long a,long b,long c,long d) { pref=a; suff=b; sum=c; max=d; } } //class TreeNode //{ // int cnt,index; // TreeNode left,right; // TreeNode(int c) // { // cnt=c; // index=-1; // } // TreeNode(int c,int index) // { // cnt=c; // this.index=index; // } //} class post implements Comparable<post> { long x,y,d,t; post(long a,long b,long c) { x=a; y=b; d=c; } public int compareTo(post X) { if(X.t==this.t) { return 0; } else { long xt=this.t-X.t; if(xt>0)return 1; return -1; } } } class TreeNode { int val; TreeNode left, right,par; TreeNode() {} TreeNode(int item) { val = item; left =null; right = null; par=null; } } class edge { int a,wt; edge(int a,int w) { this.a=a; wt=w; } } class pair3 implements Comparable<pair3> { long a; int index; pair3(long x,int i) { a=x; index=i; } public int compareTo(pair3 x) { return this.index-x.index; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
73cc59874782891f965d8fac76e86dc2
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class CodeForcesSolution { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static StringTokenizer st; static int mod = (int) 1e9+7; public static void main(String[] args) throws IOException{ int T = readInt(); for (int i = 0; i < T; i++) { int n = readInt(), x = readInt(), max_subarray = Integer.MIN_VALUE; int[] arr = new int[n+1], psa = new int[n+1], dp = new int[n+1]; for (int j = 1; j <= n; j++) { arr[j] = readInt(); psa[j] = psa[j-1]+arr[j]; } for (int len = 1; len <= n; len++) { int max = Integer.MIN_VALUE; for (int k = 1; k+len-1 <= n; k++) { max = Math.max(psa[k+len-1] - psa[k-1], max); } dp[len] = max; } for (int j = 0; j <= n; j++) { // calc f(j) int ans = 0; for (int len = 1; len <= n; len++) { ans = Math.max(Math.min(j, len) * x + dp[len], ans); } System.out.print(ans + " "); } System.out.println(); } } static long pow (long x, long exp){ if (exp==0) return 1; long t = pow(x, exp/2); t = t*t; if (exp%2 == 0) return t; return t*x; } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readLine() throws IOException { return br.readLine().trim(); } static int readALotInt() throws IOException{ int x = 0, c; while((c = br.read()) != ' ' && c != '\n') x = x * 10 + (c - '0'); return x; } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
058a3c8222f5eea1b7249682022ff58a
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Map.*; import static java.util.Arrays.*; import static java.util.Collections.*; import static java.lang.System.*; public class Main { public void tq() throws Exception { st=new StringTokenizer(bq.readLine()); int tq=i(); sb=new StringBuilder(2000000); o: while(tq-->0) { int n=i(); int k=i(); int ar[]=ari(n); int f[]=new int[n+1]; int s[]=new int[n]; fill(s,min); for(int x=0;x<n;x++) { int ss=0; for(int y=x;y<n;y++) { ss+=ar[y]; s[y-x]=max(s[y-x],ss); } } for(int x=0;x<=n;x++) { int ss=0; for(int y=0;y<n;y++)ss=max(ss,s[y]+min(y+1,x)*k); f[x]=ss; } s(f); } p(sb); } long mod=1000000007l; int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;long maxl=Long.MAX_VALUE,minl=Long.MIN_VALUE; BufferedReader bq = new BufferedReader(new InputStreamReader(in));StringTokenizer st;StringBuilder sb; public static void main(String[] a)throws Exception{new Main().tq();} int di[][]={{-1,0},{1,0},{0,-1},{0,1}}; int de[][]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}}; void f(){out.flush();} int p(int i,int p[]){return p[i]<0?i:(p[i]=p(p[i],p));} boolean c(int x,int y,int n,int m){return x>=0&&x<n&&y>=0&&y<m;} int[] so(int ar[]) { Integer r[] = new Integer[ar.length]; for (int x = 0; x < ar.length; x++) r[x] = ar[x]; sort(r); for (int x = 0; x < ar.length; x++) ar[x] = r[x]; return ar; } long[] so(long ar[]) { Long r[] = new Long[ar.length]; for (int x = 0; x < ar.length; x++) r[x] = ar[x]; sort(r); for (int x = 0; x < ar.length; x++) ar[x] = r[x]; return ar; } char[] so(char ar[]) { Character r[] = new Character[ar.length]; for (int x = 0; x < ar.length; x++) r[x] = ar[x]; sort(r); for (int x = 0; x < ar.length; x++) ar[x] = r[x]; return ar; } void p(Object p) {out.print(p);}void pl(Object p) {out.println(p);}void pl() {out.println();} void s(String s) {sb.append(s);} void s(int s) {sb.append(s);} void s(long s) {sb.append(s);} void s(char s) {sb.append(s);} void s(double s) {sb.append(s);} void ss() {sb.append(' ');} void sl(String s) {s(s);sb.append("\n");} void sl(int s) {s(s);sb.append("\n");} void sl(long s) {s(s);sb.append("\n");} void sl(char s) {s(s);sb.append("\n");} void sl(double s) {s(s);sb.append("\n");} void sl() {sb.append("\n");} int l(int v) {return 31 - Integer.numberOfLeadingZeros(v);} long l(long v) {return 63 - Long.numberOfLeadingZeros(v);} int sq(int a) {return (int) sqrt(a);} long sq(long a) {return (long) sqrt(a);} int gcd(int a, int b) { while (b > 0) { int c = a % b; a = b; b = c; } return a; } long gcd(long a, long b) { while (b > 0l) { long c = a % b; a = b; b = c; } return a; } boolean p(String s, int i, int j) { while (i < j) if (s.charAt(i++) != s.charAt(j--)) return false; return true; } boolean[] si(int n) { boolean bo[] = new boolean[n + 1]; bo[0] = bo[1] = true; for (int x = 4; x <= n; x += 2) bo[x] = true; for (int x = 3; x * x <= n; x += 2) { if (!bo[x]) { int vv = (x << 1); for (int y = x * x; y <= n; y += vv) bo[y] = true; } } return bo; } long mul(long a, long b, long m) { long r = 1l; a %= m; while (b > 0) { if ((b & 1) == 1) r = (r * a) % m; b >>= 1; a = (a * a) % m; } return r; } int i() throws IOException { if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); return Integer.parseInt(st.nextToken()); } long l() throws IOException { if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); return Long.parseLong(st.nextToken()); } String s() throws IOException { if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); return st.nextToken(); } double d() throws IOException { if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); return Double.parseDouble(st.nextToken()); } void s(int a[]) { for (int e : a){sb.append(e);sb.append(' ');} sb.append("\n"); } void s(long a[]) { for (long e : a){sb.append(e);sb.append(' ');} sb.append("\n"); } void s(char a[]) { for (char e : a){sb.append(e);sb.append(' ');} sb.append("\n"); } void s(int ar[][]) {for (int a[] : ar) s(a);} void s(long ar[][]) {for (long a[] : ar) s(a);} void s(char ar[][]) {for (char a[] : ar) s(a);} int[] ari(int n) throws IOException { int ar[] = new int[n]; if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); for (int x = 0; x < n; x++) ar[x] = Integer.parseInt(st.nextToken()); return ar; } long[] arl(int n) throws IOException { long ar[] = new long[n]; if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); for (int x = 0; x < n; x++) ar[x] = Long.parseLong(st.nextToken()); return ar; } char[] arc(int n) throws IOException { char ar[] = new char[n]; if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); for (int x = 0; x < n; x++) ar[x] = st.nextToken().charAt(0); return ar; } double[] ard(int n) throws IOException { double ar[] = new double[n]; if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); for (int x = 0; x < n; x++) ar[x] = Double.parseDouble(st.nextToken()); return ar; } String[] ars(int n) throws IOException { String ar[] = new String[n]; if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine()); for (int x = 0; x < n; x++) ar[x] = st.nextToken(); return ar; } int[][] ari(int n, int m) throws IOException { int ar[][] = new int[n][m]; for (int x = 0; x < n; x++)ar[x]=ari(m); return ar; } long[][] arl(int n, int m) throws IOException { long ar[][] = new long[n][m]; for (int x = 0; x < n; x++)ar[x]=arl(m); return ar; } char[][] arc(int n, int m) throws IOException { char ar[][] = new char[n][m]; for (int x = 0; x < n; x++)ar[x]=arc(m); return ar; } double[][] ard(int n, int m) throws IOException { double ar[][] = new double[n][m]; for (int x = 0; x < n; x++)ar[x]=ard(m); return ar; } void p(int ar[]) { sb = new StringBuilder(11 * ar.length); for (int a : ar) {sb.append(a);sb.append(' ');} out.println(sb); } void p(long ar[]) { StringBuilder sb = new StringBuilder(20 * ar.length); for (long a : ar){sb.append(a);sb.append(' ');} out.println(sb); } void p(double ar[]) { StringBuilder sb = new StringBuilder(22 * ar.length); for (double a : ar){sb.append(a);sb.append(' ');} out.println(sb); } void p(char ar[]) { StringBuilder sb = new StringBuilder(2 * ar.length); for (char aa : ar){sb.append(aa);sb.append(' ');} out.println(sb); } void p(String ar[]) { int c = 0; for (String s : ar) c += s.length() + 1; StringBuilder sb = new StringBuilder(c); for (String a : ar){sb.append(a);sb.append(' ');} out.println(sb); } void p(int ar[][]) { StringBuilder sb = new StringBuilder(11 * ar.length * ar[0].length); for (int a[] : ar) { for (int aa : a){sb.append(aa);sb.append(' ');} sb.append("\n"); } p(sb); } void p(long ar[][]) { StringBuilder sb = new StringBuilder(20 * ar.length * ar[0].length); for (long a[] : ar) { for (long aa : a){sb.append(aa);sb.append(' ');} sb.append("\n"); } p(sb); } void p(double ar[][]) { StringBuilder sb = new StringBuilder(22 * ar.length * ar[0].length); for (double a[] : ar) { for (double aa : a){sb.append(aa);sb.append(' ');} sb.append("\n"); } p(sb); } void p(char ar[][]) { StringBuilder sb = new StringBuilder(2 * ar.length * ar[0].length); for (char a[] : ar) { for (char aa : a){sb.append(aa);sb.append(' ');} sb.append("\n"); } p(sb); } void pl(Object... ar) {for (Object e : ar) p(e + " ");pl();} }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
fc1131cc371bfe1c27be656322162cf6
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static int gcd(int a, int b) { if (a < 0 || b < 0) { //求最大公因数 return -1; // 数学上不考虑负数的约数 } if (b == 0) { return a; } return a % b == 0 ? b : gcd(b, a % b); } public static int lcm(int m, int n) { //求最小公倍数 int mn = m * n; return mn / gcd(m, n); } static PrintWriter out = new PrintWriter(System.out); static Scanner in = new Scanner(System.in); static BufferedReader re = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out)); //String[] strs = re.readLine().split(" "); int a = Integer.parseInt(strs[0]); public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); //String[] strs = re.readLine().split(" "); //int T=Integer.parseInt(strs[0]); int T=in.nextInt(); //int T=1; while(T>0){ //String[] strs1 = re.readLine().split(" "); //int n=Integer.parseInt(strs1[0]); //String s=re.readLine(); //char arr[]=s.toCharArray(); //Set<Integer>set=new HashSet<>(); //Map<Long,Integer>map=new HashMap<>();26 //abcdefghijklmnopqrstuvwxyz //Map<Integer,List<Integer>>map=new HashMap<>(); //TreeSet<Integer> set = new TreeSet<>(); //int max=0;int min=2100000000; int n=in.nextInt(); //int arr[]=new int [n+1]; //for(int i=1;i<=n;i++)arr[i]=in.nextInt(); int x=in.nextInt(); int arr[]=new int [n+1]; int sum=0; int sum1=0;int max=0; int ans[]=new int [n+1]; int res[]=new int [n+1]; for(int i=1;i<=n;i++){ arr[i]=in.nextInt(); ans[i]=ans[i-1]+arr[i]; sum+=arr[i]; if(arr[i]<0){ sum1=0; } else { sum1+=arr[i]; if(sum1>max)max=sum1; } res[i]=-999999999; } res[0]=0; for(int i=1;i<=n;i++){ for(int j=0;j+i<=n;j++){ res[i]=Math.max(ans[i+j]-ans[j],res[i]); } } for(int i=n-1;i>=0;i--){ res[i]=Math.max(res[i],res[i+1]); //out.print(res[i]+" "); } //for(int i=0;i<=n;i++)out.print(res[i]+" "); //out.println(); int r=0; for(int i=0;i<=n;i++){ res[i]+=i*x; r=Math.max(res[i],r); out.print(r+" "); } out.println(); T--; } out.flush(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
d77eedc1beac011ebc6efe6ecee96ac0
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.Arrays; import java.util.Scanner; /** * https://codeforces.com/contest/1644/problem/C * C. Increase Subarray Sums * * 思路 * 1.计算出 * */ public class Main { static int N = 5 * 1010; static long[] a = new long[N], preSum = new long[N]; static long[] len = new long[N]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { Arrays.fill(len, Integer.MIN_VALUE); int n = sc.nextInt(), x = sc.nextInt(); for(int i = 1; i <= n; i++) { a[i] = sc.nextInt(); preSum[i] = preSum[i - 1] + a[i]; } // 找到长度len的最大值 for(int l = 1; l <= n; l++) { for(int i = 1; i <= n; i++) { if(i + l - 1 <= n) { long sum = preSum[i + l - 1] - preSum[i - 1]; len[l] = Math.max(len[l], sum); } } } // 计算f(k) for(int k = 0; k <= n; k++) { long max = -0x3f3f3f3f; for(int l = 1; l <= n; l++) { max = Math.max(max, len[l] + Math.min(l, k) * x); } System.out.print(Math.max(max, 0) + " "); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
94aebfe961f5222afc4dd3ffb0461c03
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { FastScanner scanner = new FastScanner(); int t = scanner.nextInt(); for (int p = 0; p < t; p++) { int n = scanner.nextInt(); long x = scanner.nextInt(); List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(scanner.nextInt()); } List<Long> sumArray = new ArrayList<>(); long sum = 0; sumArray.add(sum); for (int i = 0; i < n; i++) { sum += list.get(i); sumArray.add(sum); } Map<Integer, Long> map = new HashMap<>(); for (int i = 0; i < n; i++) { for (int j = i + 1; j <= n; j++) { long k = sumArray.get(j) - sumArray.get(i); if (!map.containsKey(j - i)) { map.put(j - i, k); } else { map.put(j - i, Math.max(k, map.get(j - i))); } } } for (int i = 0; i <= n; i++) { long max = Long.MIN_VALUE; for (int j = 1; j <= n; j++) { long k = map.get(j) + x * (Math.min(i, j)); max = Math.max(max, k); } System.out.print(Math.max(max, 0) + " "); } System.out.println(); } } private static void print(List<Integer> list) { for (int i = 0; i < list.size() - 1; i++) { System.out.print(list.get(i) + " "); } System.out.println(list.get(list.size() - 1)); } private static class Pair { long l; long r; public Pair(long l, long r) { this.l = l; this.r = r; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
05dd5e197d234a9e9535e6ae262d0e01
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
//package com.example.lib; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; public class Algorithm { static int ans =0; static boolean found= false; public static void main(String[] rgs) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); // BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\USER\\AndroidStudioProjects\\MyApplication\\lib\\src\\main\\java\\com\\example\\lib\\inpu")); StringBuilder stringBuilder = new StringBuilder(); int t = Integer.parseInt(bufferedReader.readLine()); for (int i = 0; i < t; i++) { String[] rc = bufferedReader.readLine().split(" "); int nom = Integer.parseInt(rc[0]); int add = Integer.parseInt(rc[1]); String[] s = bufferedReader.readLine().split(" "); int [] max = new int[s.length]; Arrays.fill(max,Integer.MIN_VALUE); for (int j = 0; j < s.length; j++) { int cur = Integer.parseInt(s[j]); int sum= cur; max[0]=Math.max(max[0],sum); for (int k = j+1; k < s.length; k++) { int newvsl = Integer.parseInt(s[k]); sum+=newvsl; max[k-j]=Math.max(max[k-j],sum); } } int[] ans = new int[s.length+1]; for (int j = 0; j < ans.length; j++) { int maxnum=0; for (int k = 0; k < max.length; k++) { maxnum= Math.max(maxnum, max[k]+ (add*Math.min(k+1,j))); } ans[j]=maxnum; } for (int j = 0; j < ans.length; j++) { stringBuilder.append(ans[j]).append(" "); } stringBuilder.append("\n"); } System.out.println(stringBuilder); } private static void gen(List<Integer> powetrs, List<Integer> comb, int sumSoFar, int pos) { if(pos==powetrs.size()){ if(sumSoFar==0)return; comb.add(sumSoFar); return; } gen(powetrs,comb,sumSoFar+powetrs.get(pos),pos+1); gen(powetrs,comb,sumSoFar,pos+1); } private static int gcd(int fir, int sec) { if(fir==0)return sec; if(fir>sec)return gcd(fir%sec,sec); return gcd(sec%fir,fir); } // private static void rec(List<Integer> primes, int position, int productsofar, HashSet<Integer> divisors) { // if(position==primes.size()){ // divisors.add(productsofar); // return; // } // rec(primes,position+1,productsofar*primes.get(position),divisors); // rec(primes,position+1,productsofar,divisors); // // } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
bd5d0afaba5c7d0911e53a74b93254b8
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import static java.lang.Math.*; public class IncreaseSubarraySums implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { int T = in.ni(); while (T-- > 0) { int n = in.ni(); long k = in.nl(); long[] maxArrayWithLength = new long[n + 1]; for (int i = 0; i <= n; i++) { maxArrayWithLength[i] = Long.MIN_VALUE; } maxArrayWithLength[0] = 0; long[] x = new long[n]; for (int i = 0; i < n; i++) { x[i] = in.ni(); } for (int i = 0; i < n; i++) { long sum = 0; for (int j = i; j < n; j++) { sum += x[j]; int size = j - i + 1; maxArrayWithLength[size] = Math.max(maxArrayWithLength[size], sum); } } long[] result = new long[n + 1]; for (int times = 0; times <= n; times++) { for (int size = 0; size <= n; size++) { result[times] = Math.max(result[times], min(times, size) * k + maxArrayWithLength[size]); } } for (int i = 0; i <= n; i++) { out.print(result[i] + " "); } out.println(); } } @Override public void close() throws IOException { in.close(); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (IncreaseSubarraySums instance = new IncreaseSubarraySums()) { instance.solve(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
a43e486ad9cf9801eb9b0b6d49651efa
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
//package codeforces; import java.io.*; import java.util.*; import java.lang.*; public class Stringinput { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(InputStream in) { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public boolean hasNextInt() { return true; } } // int trips[][] = new int[][]; // sort according to 1st ele of each row ,that is--->( trips[i][1], trips[j][1] ) // Arrays.sort(trips, new Comparator<int[]>(){ // public int compare(int[] i1, int[] i2) { // return i1[1] - i2[1]; // } // }); public static void main(String[] args) throws IOException { FastReader sc = new FastReader(System.in); // PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); StringBuffer top = new StringBuffer(); // char alp[]={'a', 'b', 'c' , 'd' , 'e', 'f', 'g', 'h','i','j','k','l','m','n','o','p', 'q','r','s', 't', 'u','v','w','x','y','z' }; while (t-- > 0) { ArrayList<pair> pr = new ArrayList<>(); //ArrayList<Long> al = new ArrayList<>(); // SortedSet<Integer> hs = new TreeSet<>(); TreeMap<Integer, ArrayList<Integer>> mp = new TreeMap<>(); HashMap<Integer, ArrayList<Integer>> mp2 = new HashMap<>(); HashSet<Integer> hs = new HashSet<>(); int n=sc.nextInt(); long x=sc.nextInt(); long a[] = new long[n]; long prf[] = new long[n]; for(int i=0; i<n; i++){ a[i]=sc.nextInt(); if(i==0){ prf[i]=a[i]; } else{ prf[i]=a[i]+prf[i-1]; } } long lensm[] = new long[n+1]; for(int i=1; i<=n; i++){ long max=prf[i-1]; long sm=prf[i-1]; for(int j=i; j<n; j++){ sm+=a[j]; sm-=a[j-i]; max=Math.max(sm,max); } lensm[i]=max; } for(int k=0; k<=n; k++){ long max=0; for(int i=0; i<=n; i++){ long val = lensm[i] + (long) Math.min(k,i)*x; max=Math.max(max,val); } top.append(max).append(" "); } top.append("\n"); } System.out.println(top); } public static boolean ispalindrome(int x){ String str=String.valueOf(x); int l=0; int r=str.length()-1; while(l<r){ if(str.charAt(l)!= str.charAt(r)) return false; l++; r--; } return true; } public static boolean check(int[] br, int[] cbr) { for(int i=0; i<br.length; i++){ if(br[i]!=cbr[i]) return false; } return true; } static void swap(int ar[],int i,int j){ int tmp = ar[i]; ar[i]=ar[j]; ar[j]=tmp; } static long differBit(long A, long B) { long XOR = A ^ B; // Check for 1's in the binary form using // Brian Kerninghan's Algorithm long count = 0; while (XOR > 0) { XOR = XOR & (XOR - 1); count++; } // return the count of different bits return count; } static boolean match(String st,int it){ for(int i=0; i+it<st.length(); i++){ if(st.charAt(i)!= st.charAt(i+it)){ return false; } } return true; } public static int upper_bound(long arr[], int N, long X) { int mid; // Initialise starting index and // ending index int low = 0; int high = N; // Till low is less than high while (low < high) { // Find the middle index mid = low + (high - low) / 2; // System.out.print(mid+" "); // If X is greater than or equal // to arr[mid] then find // in right subarray if (X >= arr[mid]) { low = mid + 1; } // If X is less than arr[mid] // then find in left subarray else { high = mid; } } // if X is greater than arr[n-1] // if(low < N && arr.get(low) <= X) { // low++; // } if(arr[low]>X){ return low; } if(arr[high]>X){ return high; } // Return the upper_bound index System.out.println("sumit"); return N; } static long meBinary(long[] ar,long X){ int l=0; int h=ar.length-1; int mid=0; while(h-l>1){ mid = (l+h)/2; long val =ar[mid]-(long) (mid+1); if(X>=val){ l=mid; }else { h=mid-1; } } if(X>=ar[h]-(long) (h+1)){ X=X-(ar[h]-(long) (h+1)); return ar[h]+X; } if(X>=ar[l]-(long) (l+1)){ X=X-(ar[l]-(long) (l+1)); return ar[l]+X; } return X; } static int lowestprimeFactor(int num){ int lst=num; for(int i=2; i*i<=num; i++){ if(num%i==0){ lst=i; break; } } return lst; } static long powerOptimised(long a, long n) { // Stores final answer long ans = 1; while (n > 0) { long last_bit = (n & 1); // Check if current LSB // is set if (last_bit > 0) { ans = ans * a; } a = a * a; // Right shift n = n >> 1; } return ans; } public static long lower_bound(long prf[], int N, long X) { int mid; // Initialise starting index and // ending index int low = 0; int high = N; // Till low is less than high while ( high-low >1) { mid = (high + low) / 2; // If X is less than or equal // to arr[mid], then find in // left subarray long rmg = X-prf[mid]; // If X is greater arr[mid] // then find in right subarray if(rmg<=prf[mid]){ high=mid; } else{ low=mid; } } long rmg = X-prf[low]; int cntr = N-low; int cntl = low+1; if(rmg>prf[low] && cntr<cntl) return 1; rmg = X-prf[high]; cntr = N-high; cntl = high+1; if(rmg>prf[high] && cntr<cntl) return 1; return 0; // if X is greater than arr[n-1] // if(low < N && arr.get(low) < X) { // low++; // } // if(((low*(low+1))/2)>=X){ // return low; // } // if(((high*(high+1))/2)>=X){ // return high; // } // Return the lower_bound index // return -1; } static boolean compareString(String curr,String prev,int n){ for(int i=0; i<n; i++){ if(curr.charAt(i)<prev.charAt(i)){ return true; } else if(curr.charAt(i)>prev.charAt(i)){ return false; } } return false; } private static boolean isLCM(long a, long b, long n) { long lcm = ((a*b)/gcd(a,b)); if(lcm==n) return true; return false; } static long highestPowerof2(long x) { // check for the set bits x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; // Then we remove all but the top bit by xor'ing the // string of 1's with that string of 1's shifted one to // the left, and we end up with just the one top bit // followed by 0's. return x ^ (x >> 1); } static int nextPowerOf2(int n) { n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static void height(int idx, ArrayList<ArrayList<Integer>> adj, int vis[], int n, long cnt, long maxh[]) { vis[idx] = 1; maxh[0] = Math.max(maxh[0], cnt); for (int it : adj.get(idx)) { cnt++; height(it, adj, vis, n, cnt, maxh); cnt--; } } } // lambda pair array sorting // Arrays.sort(stockPrices,(o1,o2)->o1[0]-o2[0]); class pair{ int nod; int wt; pair(int ft, int st){ this.nod=ft; this.wt=st; }} class change implements Comparator<pair>{ @Override public int compare(pair m1, pair m2){ if(m1.wt==m2.wt){ return (m1.nod-m2.nod); } else{ return (m1.wt-m2.wt); }}} class piro{ int ft; int st; piro(int ft,int st){ this.ft=ft; this.st=st; }} class chance implements Comparator<piro>{ @Override public int compare(piro m1, piro m2){ if(m1.ft==m2.ft){ return m1.st-m2.st; } else{ return (m1.ft-m2.ft); }}} //
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
174598f5255a24e900e8339d7aead79b
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class C1644 { public static void main(String[] args) { Scanner in = new Scanner(System.in); StringBuilder out = new StringBuilder(); int T = in.nextInt(); for (int t=0; t<T; t++) { int N = in.nextInt(); long X = in.nextInt(); long[] A = new long[N]; for (int n=0; n<N; n++) { A[n] = in.nextInt(); } long[] maxSumOfLength = new long[N+1]; Arrays.fill(maxSumOfLength, 1, N+1, Long.MIN_VALUE); for (int start=0; start<N; start++) { long sum = 0; int length = 0; for (int end=start; end<N; end++) { sum += A[end]; length++; maxSumOfLength[length] = Math.max(maxSumOfLength[length], sum); } } long[] maxSumOfLengthAtLeast = new long[N+1]; maxSumOfLengthAtLeast[N] = maxSumOfLength[N]; for (int n=N-1; n>=0; n--) { maxSumOfLengthAtLeast[n] = Math.max(maxSumOfLengthAtLeast[n+1], maxSumOfLength[n]); } long[] answer = new long[N+1]; answer[0] = maxSumOfLengthAtLeast[0]; for (int k=1; k<=N; k++) { answer[k] = Math.max(answer[k-1], k*X+maxSumOfLengthAtLeast[k]); } for (long a : answer) { out.append(a).append(' '); } out.append('\n'); } System.out.print(out); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
95cd0499a702dc536fee5503efa37d28
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T-->0){ int n = sc.nextInt(); int x = sc.nextInt(); int[] a = new int[n]; int[] sum = new int[n+1]; for(int i = 0;i<n;i++){ a[i] = sc.nextInt(); sum[i+1] = sum[i]+a[i]; } int[] res = new int[n+1]; Arrays.fill(res, 1, n+1, Integer.MIN_VALUE); for(int l = 0;l<n;l++){ for(int r = l;r<n;r++){ int len = r-l+1; res[len] = Math.max(res[len],sum[r+1]-sum[l]); } } int[] ans = new int[n+1]; for(int k = 0;k<=n;k++){ int val = 0; for(int i = 0;i<=n;i++){ val = Math.max(val,res[i]+Math.min(i,k)*x); } ans[k]=val; } StringBuilder sb =new StringBuilder(); for(int i = 0;i<=n;i++){ sb.append(ans[i]+" "); } System.out.println(sb); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
c88ff0edc97dde8b35d4495a25413ef9
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; public class Perm { public static void main(String[] args) { // TODO Auto-generated method stub Scanner xy=new Scanner(System.in); //diptiman code //say no to plagarism //i dont share code nor take from anyone StringBuilder sb = new StringBuilder(); int tc=xy.nextInt(); while(tc>0) { tc--; int n=xy.nextInt(); int x=xy.nextInt(); int A[]=new int[n]; for(int i=0;i<n;i++) A[i]=xy.nextInt(); Map<Integer,Integer> m=new HashMap<>(); for(int i=0;i<n;i++) { int S=0; for(int j=i;j<n;j++) { S+=A[j]; //System.out.println(j-i+1); if(m.containsKey(j-i+1)) m.put(j-i+1,(int) Math.max(m.get(j-i+1),S)); else m.put(j-i+1,S); } } StringBuilder ans=new StringBuilder(); for(int k=0;k<=n;k++) { int max=0; for(int i=1;i<=n;i++) { int S=m.get(i); if(i<k) max=Math.max(max, i*x+S); else max=Math.max(max, k*x+S); } ans.append(max); ans.append(" "); } System.out.println(ans); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
9563ae1b6b8811935bde88533cce9cd0
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class CF1 { static FastReader sc=new FastReader(); // static long dp[][]; // static boolean v[][][]; // static int mod=998244353;; // static int mod=1000000007; static long oset[]; static int oset_p; static long mod=1000000007; // static int max; // static int bit[]; //static long fact[]; //static HashMap<Long,Long> mp; //static StringBuffer sb=new StringBuffer(""); //static HashMap<Integer,Integer> map; //static List<Integer>list; //static int m; //static StringBuffer sb=new StringBuffer(); // static PriorityQueue<Integer>heap; //static int dp[]; static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { // StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); int cccc=1; outer :while (ttt-- > 0) { int n=i(); long x=l(); long arr[]=inputL(n); long sum[]=new long[n+1]; for(int i=1;i<=n;i++) { int l=0,r=0; long max=Integer.MIN_VALUE,s=0; while(r<n) { s+=arr[r]; if(r-l+1==i) { max=max(max,s); s-=arr[l++]; } r++; } sum[i]=max; } for(int i=0;i<=n;i++) { long max=0; for(int j=1;j<=n;j++) { max=max(max,sum[j]+(x*(min(j,i)))); } out.print(max+" "); } out.println(); } out.close(); } static class Pair implements Comparable<Pair> { int x; int y; // int z; Pair(int x,int y){ this.x=x; this.y=y; // this.z=z; } @Override public int compareTo(Pair o) { if(this.x>o.x) return 1; else if(this.x<o.x) return -1; else { if(this.y>o.y) return 1; else if(this.y<o.y) return -1; else return 0; } } public int hashCode() { final int temp = 14; int ans = 1; ans =x*31+y*13; return ans; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null) { return false; } if (this.getClass() != o.getClass()) { return false; } Pair other = (Pair)o; if (this.x != other.x || this.y!=other.y) { return false; } return true; } // /* FOR TREE MAP PAIR USE */ // public int compareTo(Pair o) { // if (x > o.x) { // return 1; // } // if (x < o.x) { // return -1; // } // if (y > o.y) { // return 1; // } // if (y < o.y) { // return -1; // } // return 0; // } } static int find(int A[],int a) { if(A[a]==a) return a; return A[a]=find(A, A[a]); } //static int find(int A[],int a) { // if(A[a]==a) // return a; // return find(A, A[a]); //} //FENWICK TREE static class BIT{ int bit[]; BIT(int n){ bit=new int[n+1]; } int lowbit(int i){ return i&(-i); } int query(int i){ int res=0; while(i>0){ res+=bit[i]; i-=lowbit(i); } return res; } void update(int i,int val){ while(i<bit.length){ bit[i]+=val; i+=lowbit(i); } } } //END static long summation(long A[],int si,int ei) { long ans=0; for(int i=si;i<=ei;i++) ans+=A[i]; return ans; } static void add(long v,Map<Long,Long>mp) { if(!mp.containsKey(v)) { mp.put(v, (long)1); } else { mp.put(v, mp.get(v)+(long)1); } } static void remove(long v,Map<Long,Long>mp) { if(mp.containsKey(v)) { mp.put(v, mp.get(v)-(long)1); if(mp.get(v)==0) mp.remove(v); } } public static int upper(long A[],long k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static int lower(long A[],long k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void reverse(long A[]) { int n=A.length; long B[]=new long[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void reverse(int A[]) { int n=A.length; int B[]=new int[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static long[][] input(int n,int m){ long A[][]=new long[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=l(); } } return A; } static char[][] charinput(int n,int m){ char A[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) { A[i][j]=s.charAt(j); } } return A; } static int nextPowerOf2(int n) { if(n==0) return 1; n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } static int highestPowerof2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static long highestPowerof2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long [] prefix(long A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] prefix(int A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] suffix(long A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static long [] suffix(int A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static void fill(int dp[]) { Arrays.fill(dp, -1); } static void fill(int dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(int dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(int dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static void fill(long dp[]) { Arrays.fill(dp, -1); } static void fill(long dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(long dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(long dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long power(long x, long y, long p) { if(y==0) return 1; if(x==0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static void dln(String s) { out.println(s); } static void d(String s) { out.print(s); } static void print(int A[]) { for(int i : A) { out.print(i+" "); } out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Long> hash(int A[]){ HashMap<Integer,Long> map=new HashMap<Integer, Long>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+(long)1); } else { map.put(i, (long)1); } } return map; } static HashMap<Long,Long> hash(long A[]){ HashMap<Long,Long> map=new HashMap<Long, Long>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+(long)1); } else { map.put(i, (long)1); } } return map; } static TreeMap<Integer,Integer> tree(int A[]){ TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Long,Integer> tree(long A[]){ TreeMap<Long,Integer> map=new TreeMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
8e6a0e69b30aa7b970a5f724e3d1c1b4
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class Subarray { static BufferedReader bf; static PrintWriter out; static Scanner sc; static StringTokenizer st; public static void main (String[] args)throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new Scanner(System.in); int t = Integer.parseInt(bf.readLine()); while(t-->0){ solve(); } } public static void solve()throws IOException{ int n = nextInt(); long x = nextLong(); long maxSum[] = new long[n+1]; long[]arr = new long[n]; for(int i =0;i<n;i++){ arr[i] = nextLong(); } for(int i = 0;i<n;i++){ int k = 0; int j = i; long sum = 0; for(int o =0 ;o<i;o++){ sum += arr[o]; } long max = Long.MIN_VALUE; while(j < n){ sum += arr[j]; max = Math.max(sum,max); sum -= arr[k]; k++; j++; } maxSum[i+1] = max; } for(int i = 0;i<n+1;i++){ long max = 0; for(int j = 1;j<n+1;j++){ max = Math.max(max,Math.min(i,j)*x+maxSum[j]); } out.print(max+" "); } out.println(); out.flush(); } // code for input public static void print(String s ){ System.out.print(s); } public static void print(int num ){ System.out.print(num); } public static void print(long num ){ System.out.print(num); } public static void println(String s){ System.out.println(s); } public static void println(int num){ System.out.println(num); } public static void println(long num){ System.out.println(num); } public static void println(){ System.out.println(); } public static int Int(String s){ return Integer.parseInt(s); } public static long Long(String s){ return Long.parseLong(s); } public static String[] nextStringArray()throws IOException{ return bf.readLine().split(" "); } public static String nextString()throws IOException{ return bf.readLine(); } public static long[] nextLongArray(int n)throws IOException{ String[]str = bf.readLine().split(" "); long[]arr = new long[n]; for(int i =0;i<n;i++){ arr[i] = Long.parseLong(str[i]); } return arr; } public static int[][] newIntMatrix(int r,int c)throws IOException{ int[][]arr = new int[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Integer.parseInt(str[j]); } } return arr; } public static long[][] newLongMatrix(int r,int c)throws IOException{ long[][]arr = new long[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Long.parseLong(str[j]); } } return arr; } static class pair{ int one; int two; pair(int one,int two){ this.one = one ; this.two =two; } } public static long gcd(long a,long b){ if(b == 0)return a; return gcd(b,a%b); } public static long lcm(long a,long b){ return (a*b)/(gcd(a,b)); } public static boolean isPalindrome(String s){ int i = 0; int j = s.length()-1; while(i<=j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } // these functions are to calculate the number of smaller elements after self public static void sort(int[]arr,int l,int r){ if(l < r){ int mid = (l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); smallerNumberAfterSelf(arr, l, mid, r); } } public static void smallerNumberAfterSelf(int[]arr,int l,int mid,int r){ int n1 = mid - l +1; int n2 = r - mid; int []a = new int[n1]; int[]b = new int[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[l+i]; } for(int i =0;i<n2;i++){ b[i] = arr[mid+i+1]; } int i = 0; int j =0; int k = l; while(i<n1 && j < n2){ if(a[i] < b[j]){ arr[k++] = a[i++]; } else{ arr[k++] = b[j++]; } } while(i<n1){ arr[k++] = a[i++]; } while(j<n2){ arr[k++] = b[j++]; } } public static String next(){ while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bf.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static double nextDouble() { return Double.parseDouble(next()); } static String nextLine(){ String str = ""; try { str = bf.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // use some math tricks it might help // sometimes just try to think in straightforward plan in A and B problems don't always complecate the questions with thinking too much differently // always use long number to do 10^9+7 modulo // if a problem is related to binary string it could also be related to parenthesis // *****try to use binary search(it is a very beautiful thing it can work in some of the very unexpected problems ) in the question it might work****** // try sorting // try to think in opposite direction of question it might work in your way // if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+d or a,b,c,d in general // if the question is to much related to left and/or right side of any element in an array then try monotonic stack it could work. // in range query sums try to do binary search it could work // analyse the time complexity of program thoroughly // anylyse the test cases properly // if we divide any number by 2 till it gets 1 then there will be (number - 1) operation required // try to do the opposite operation of what is given in the problem //think about the base cases properly //If a question is related to numbers try prime factorisation or something related to number theory // keep in mind unique strings //you can calculate the number of inversion in O(n log n) // in a matrix you could sometimes think about row and cols indenpendentaly.
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
936569f631264c4a37b79a2b4f21cae8
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static long WST(int a[],int size) { long ans = Long.MIN_VALUE,now=0; int i =0; while(i<size) { now+=a[i]; i++; } ans= Math.max(ans, now); int j =0; while(i<a.length) { now-=a[j]; now+=a[i]; ans = Math.max(ans, now); i++; j++; } return ans; } public static void main(String args[]) throws java.lang.Exception { FastScanner input = new FastScanner(); int tc = input.nextInt(); work: while (tc-- > 0) { int n = input.nextInt(); int x = input.nextInt(); int a[] = new int[n]; for (int i = 0; i <n; i++) { a[i] = input.nextInt(); } ArrayList<Long> subarray = new ArrayList<>(); long ans =0; for (int i = 1; i <=n; i++) { long value = WST(a,i); subarray.add(value); ans= Math.max(ans, value); } // System.out.println(subarray); StringBuilder result = new StringBuilder(); result.append(ans+" "); for (int i = 1; i <=n; i++) { ans =0; for (int j = 0; j <n; j++) { ans = Math.max(ans, subarray.get(j)+(x*Math.min(i, j+1))); } result.append(ans+" "); } System.out.println(result); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
da0c9da365eb44588e02a959ee137541
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Odd_Selection { static InputStreamReader r = new InputStreamReader(System.in); static BufferedReader br = new BufferedReader(r); static PrintWriter p = new PrintWriter(System.out); public static void main(String[] args) throws Exception { int test = Integer.parseInt(br.readLine()); // int test = 1; me146: while (test-- > 0) { String str[] = splitString(); int n = Integer.parseInt(str[0]); long x = Integer.parseInt(str[1]); str=splitString(); long a[]=new long[n+1]; for(int i=0;i<n;i++)a[i+1]=Long.parseLong(str[i]); long dp[][]=new long[n+1][n+1]; for(int i=0;i<=n;i++){ long max=0; for(int j=1;j<=n;j++){ if(i==0){ dp[i][j]=Math.max(dp[i][j-1]+a[j],a[j]); dp[i][j]=Math.max(dp[i][j],0); }else{ dp[i][j] = Math.max(dp[i][j - 1] + a[j],dp[i-1][j-1]+ a[j]+x); dp[i][j] = Math.max(dp[i][j], 0); } max=Math.max(max,dp[i][j]); } p.print(max+" "); } p.println(); } p.flush(); } static int[] readIntArray() throws IOException { String str[] = br.readLine().trim().split(" "); int a[] = new int[str.length]; int i = 0; for (String x : str) { a[i] = Integer.parseInt(x); i++; } return a; } static long[] readLongArray() throws IOException { String str[] = br.readLine().trim().split(" "); long a[] = new long[str.length]; int i = 0; for (String x : str) { a[i] = Long.parseLong(x); i++; } return a; } static long readLong() throws IOException { String str[] = br.readLine().trim().split(" "); return Long.parseLong(str[0]); } static int readInt() throws IOException { String str[] = br.readLine().trim().split(" "); return Integer.parseInt(str[0]); } static String[] splitString() throws IOException { return br.readLine().trim().split(" "); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
9fdb6835013577d069f40502b08b8197
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import static java.lang.Math.*; import java.io.*; public class S { public static int surv = 0; public static long f[]; public static void main(String args[])throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while(t > 0){ t--; StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); int a[] = new int[n]; st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++){ a[i] = Integer.parseInt(st.nextToken()); } solve(a, n, x, sb); } System.out.print(sb.toString()); } final static int MOD = 1000000007; public static void solve(int a[], int n, int x, StringBuilder sb){ int best[] = new int[n+1]; Arrays.fill(best, Integer.MIN_VALUE); best[0] = 0; for(int i = 1; i <= n; i++){ int l = 0; int r = i-1; int curr = 0; for(int j = 0; j <= r; j++){ curr += a[j]; } int max = curr; for(int j = r + 1; j < n; j++){ curr += a[j]; curr -= a[l]; l++; max = Math.max(max, curr); } best[i] = Math.max(best[i], max); } for(int i = 0; i <= n; i++){ int ans = Integer.MIN_VALUE; for(int j = 0; j < best.length; j++){ int curr = best[j] + (Math.min(j, i) * x); ans = Math.max(ans, curr); } sb.append(ans + " "); } sb.append("\n"); } public static int checkP(long n){ int ans = 0; while(n > 0){ ans += (n & 1); n >>= 1; } return ans; } public static List<int[]> primeFactorization(int n){ List<Integer> primes = sievePrimes(1000000); List<int[]> ans = new ArrayList<int[]>(); for(int i = 0; (long)primes.get(i)*(long)primes.get(i) <= n; i++){ int curr = primes.get(i); if(n % curr == 0){ int a[] = new int[]{curr, 0}; while(n % curr == 0){ n /= curr; a[1]++; } ans.add(a); } } if(n > 1){ ans.add(new int[]{n, 1}); } return ans; } public static List<Integer> sievePrimes(int limit){ boolean p[] = new boolean[limit+1]; for(int i = 2; i*i <= p.length; i++){ if(!p[i]){ for(int j = i*i; j < p.length; j += i){ p[j] = true; } } } List<Integer> primes = new ArrayList<Integer>(); for(int i = 2; i < p.length; i++){ if(!p[i])primes.add(i); } return primes; } public static long powerr(long base, long exp){ if(exp < 2)return base; if(exp % 2 == 0){ long ans = powerr(base, exp/2) % MOD; ans *= ans; ans %= MOD; return ans; }else{ return (((powerr(base, exp-1)) % MOD) * base) % MOD; } } public static long power(long a, long b){ if(b == 0)return 1l; long ans = power(a, b/2); ans *= ans; ans %= MOD; if(b % 2 != 0){ ans *= a; } return ans % MOD; } public static int logLong(long a){ int ans = 0; long b = 1; while(b < a){ b*=2; ans++; } return ans; } public static void sort(int a[]){ List<Integer> l = new ArrayList<Integer>(); for(int val : a){ l.add(val); } Collections.sort(l); int k = 0; for(int val : l){ a[k++] = val; } } public static void sortLong(long a[]){ List<Long> l = new ArrayList<Long>(); for(long val : a){ l.add(val); } Collections.sort(l); int k = 0; for(long val : l){ a[k++] = val; } } public static boolean isPal(String s){ int l = 0; int r = s.length() - 1; while(l <= r){ if(s.charAt(l) != s.charAt(r)){ return false; } l++; r--; } return true; } /* public static int gcd(int a, int b){ if(b > a){ int temp = a; a = b; b = temp; } if(b == 0)return a; return gcd(b, a%b); }*/ public static long gcd(long a, long b){ if(b > a){ long temp = a; a = b; b = temp; } if(b == 0)return a; return gcd(b, a%b); } // public static long lcm(long a, long b){ // return a * b/gcd(a, b); // } /*public static class DJSet{ public int a[]; public DJSet(int n){ this.a = new int[n]; Arrays.fill(a, -1); } public int find(int val){ if(a[val] >= 0){ a[val] = find(a[val]); return a[val]; }else{ return val; } } public boolean union(int val1, int val2){ int p1 = find(val1); int p2 = find(val2); if(p1 == p2){ return false; } int size1 = Math.abs(a[p1]); int size2 = Math.abs(a[p2]); if(size1 >= size2){ a[p2] = p1; a[p1] = (size1 + size2) * -1; }else{ a[p1] = p2; a[p2] = (size1 + size2) * -1; } return true; }*/ /* public static class DSU{ public int a[]; public DSU(int size){ this.a = new int[size]; Arrays.fill(a, -1); } public int find(int u){ if(a[u] < 0)return u; a[u] = find(a[u]); return a[u]; } public boolean union(int u, int v){ int p1 = find(u); int p2 = find(v); if(p1 == p2)return false; int size1 = a[p1] * -1; int size2 = a[p2] * -1; if(size1 >= size2){ a[p2] = p1; a[p1] = (size1 + size2) * -1; }else{ a[p1] = p2; a[p2] = (size1 + size2) * -1; } return true; } }*/ }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
c56deaee16321c3cf54746babf558339
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; public class IncreaseSubarraySums { public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt() , x = sc.nextInt(); int[] a = sc.nextIntArray(n); long[] res = new long[n+1]; Arrays.fill(res,Long.MIN_VALUE); for(int i=0;i<n;i++) { long sum=0; for(int j=i;j<n;j++) { sum+=a[j]; res[j-i+1]=Math.max(res[j-i+1], sum); } } for(int i=0; i<=n ;i++){ long tmp = 0; for(int j=1; j<=n; j++) tmp = Math.max(tmp , res[j] + (long) Math.min(j, i) * x); pw.print(tmp+" "); } pw.println(); } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws IOException { br = new BufferedReader(new FileReader(file)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
15cce46e366cba8194ae41516919dd8f
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import static java.lang.Math.*; import java.io.*; import java.math.*; import java.util.*; public class cf3 { static FastReader x = new FastReader(); static OutputStream outputStream = System.out; static PrintWriter out = new PrintWriter(outputStream); /*---------------------------------------CODE STARTS HERE-------------------------*/ public static void main(String[] args) { int t = x.nextInt(); StringBuilder str = new StringBuilder(); while (t > 0) { int n = x.nextInt(); int xx = x.nextInt(); int a[] = new int[n]; ArrayList<Integer> arr = new ArrayList<>(); for(int i=0;i<n;i++) { a[i] = x.nextInt(); } for(int i=0;i<n;i++) { arr.add(findmax(a,i+1)); } int ans =0; int max =0; for(int i=0;i<=n;i++) { ans =0; max=Integer.MIN_VALUE; for(int j=0;j<arr.size();j++) { int len = j+1; if(len<i) { ans = arr.get(j)+(xx*len); }else { ans = arr.get(j)+(xx*i); } if(ans>max) { max = ans; } } if(max<0) max=0; System.out.print(max+" "); } System.out.println(); str.append("\n"); t--; } out.println(str); out.flush(); } static public int findmax(int a[] ,int l) { int le=0,r=l-1; int sum=0; for(int i=0;i<=r;i++) { sum+=a[i]; } int max = sum; for(int i=r+1;i<a.length;i++) { le++; sum = sum-a[le-1]+a[i]; if(sum>max) { max = sum; } } return max; } /*--------------------------------------------FAST I/O--------------------------------*/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } char nextchar() { char ch = ' '; try { ch = (char) br.read(); } catch (IOException e) { e.printStackTrace(); } return ch; } } /*--------------------------------------------BOILER PLATE---------------------------*/ static int[] readarr(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = x.nextInt(); } return arr; } static int[] sortint(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static long[] sortlong(long a[]) { ArrayList<Long> al = new ArrayList<>(); for (long i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < al.size(); i++) { a[i] = al.get(i); } return a; } static int pow(int x, int y) { int result = 1; while (y > 0) { if (y % 2 == 0) { x = x * x; y = y / 2; } else { result = result * x; y = y - 1; } } return result; } static int[] revsort(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al, Comparator.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static int[] gcd(int a, int b, int ar[]) { if (b == 0) { ar[0] = a; ar[1] = 1; ar[2] = 0; return ar; } ar = gcd(b, a % b, ar); int t = ar[1]; ar[1] = ar[2]; ar[2] = t - (a / b) * ar[2]; return ar; } static boolean[] esieve(int n) { boolean p[] = new boolean[n + 1]; Arrays.fill(p, true); for (int i = 2; i * i <= n; i++) { if (p[i] == true) { for (int j = i * i; j <= n; j += i) { p[j] = false; } } } return p; } static ArrayList<Integer> primes(int n) { boolean p[] = new boolean[n + 1]; ArrayList<Integer> al = new ArrayList<>(); Arrays.fill(p, true); int i = 0; for (i = 2; i * i <= n; i++) { if (p[i] == true) { al.add(i); for (int j = i * i; j <= n; j += i) { p[j] = false; } } } for (i = i; i <= n; i++) { if (p[i] == true) { al.add(i); } } return al; } static int etf(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res /= i; res *= (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res /= n; res *= (n - 1); } return res; } static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
9497e33084b65cdd6d0e78e2b7b9240c
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class A { public static void main(String[] args) throws IOException { InputStreamReader re=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(re); int t = Integer.parseInt(br.readLine()); int i,j,k,num; for(i=0;i<t;i++) { String s = br.readLine(); String[] c = s.split(" "); int n = Integer.parseInt(c[0]); int x = Integer.parseInt(c[1]); s = br.readLine(); String[] ce = s.split(" "); int[] arr = new int[n]; for(j=0;j<n;j++) arr[j] = Integer.parseInt(ce[j]); int[] tem = new int[n+1]; Arrays.fill(tem, Integer.MIN_VALUE); for(j=0;j<n;j++) { num = 0; for(k=j;k<n;k++) { num +=arr[k]; tem[k-j+1] = Math.max(tem[k-j+1], num); } } for(j=0;j<=n;j++) { int res = 0; for(k=0;k<=n;k++) res = Math.max(res, (Math.min(j, k)*x)+tem[k]); System.out.print(res+" "); } System.out.println(); } } static long solve(long[] arr,long l,long r, int n,int k) { int j; if(l<=r) { long m = l+(r-l)/2; long sum = arr[0]+m; for(j=1;j<n;j++) { if((double)arr[j]> (double)(sum*k)/(double)100) break; sum+=arr[j]; } if(j==n) return solve(arr,l,m-1,n,k); else return solve(arr,m+1,r,n,k); } return l; } static long factorial(long n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } static long setbitNumber(long n) { long k = (int)(Math.log(n) / Math.log(2)); return 1 << k; } static int search(int l, int r,int x, int[]arr) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return search( l, mid - 1, x,arr); return search( mid + 1, r, x,arr); } return -1; } static long combination(long n, long k){ // nCr combination long res = 1; if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } static long modpower(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static int xor(int n){ // If n is a multiple of 4 if (n % 4 == 0) return n; // If n%4 gives remainder 1 if (n % 4 == 1) return 1; // If n%4 gives remainder 2 if (n % 4 == 2) return n + 1; return 0; } static int getsum(int n) // sum of digits { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static boolean isPrime(int n) // check prime { if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
1510657f70dc9a6f9699938ebd5dd991
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.Scanner; public class C_Increase_Subarray_Sums { static Scanner in = new Scanner(System.in); static int testCases, n, x; static int a[]; static StringBuilder ans = new StringBuilder(); static void solve() { long dp[] = new long[n + 1]; for(int i = 0; i <= n; ++i) { dp[i] = Integer.MIN_VALUE; } for(int i = 0; i < n; ++i) { long sum = 0; for(int j = i; j < n; j++) { sum += a[j]; dp[j - i + 1] = Math.max(dp[j - i + 1], sum); } } for(int i = 0; i <= n; ++i) { long ans1 = 0; for(int j = 0; j <= n; ++j) { ans1 = Math.max(ans1, Math.min(i, j) * x + dp[j] ); } ans.append(ans1).append(" "); } ans.append("\n"); } public static void main(String [] amit) { testCases = in.nextInt(); for(int t = 0; t < testCases; ++t) { n = in.nextInt(); x = in.nextInt(); a = new int[n + 1]; for(int i = 0; i < n; ++i) { a[i] = in.nextInt(); } solve(); } System.out.print(ans.toString()); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
525933158eda40f7cbb91edc3d539a17
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.lang.*; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.File; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class Main { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { long first, second; public Pair(long first, long second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(first - ob.first); } } static class Tuple implements Comparable<Tuple> { long first, second,third; public Tuple(long first, long second, long third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int count = 0; int[] parent; int[] rank; public DSU(int n) { count = n; parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public void union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return; if(rank[a] < rank[b]) { parent[a] = b; } else if(rank[a] > rank[b]) { parent[b] = a; } else { parent[b] = a; rank[a] = 1 + rank[a]; } count--; } public int countConnected() { return count; } } static class Reader { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static long lcm(long a, long b) { return (a / LongGCD(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long powMod(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return powMod(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(long n, long k) { long ans = 1; for(long i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } public static boolean isPrime(long n) { if(n == 1) { return false; } //check only for sqrt of the number as the divisors //keep repeating so only half of them are required. So,sqrt. for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Long> SieveList(int n) { boolean prime[] = new boolean[(int)(n+1)]; Arrays.fill(prime, true); List<Long> l = new ArrayList<>(); for (long p = 2; p*p<=n; p++) { if (prime[(int)(p)] == true) { for(long i = p*p; i<=n; i += p) { prime[(int)(i)] = false; } } } for (long p = 2; p<=n; p++) { if (prime[(int)(p)] == true) { l.add(p); } } return l; } public static int countDivisors(int x) { int c = 0; for(int i = 1;i*i<=x;i++) { if(x%i == 0) { if(x/i != i) { c+=2; } else { c++; } } } return c; } public static long log2(long n) { long ans = (long)(log(n)/log(2)); return ans; } public static boolean isPow2(long n) { return (n != 0 && ((n & (n-1))) == 0); } public static boolean isSq(int x) { long s = (long)Math.round(Math.sqrt(x)); return s*s==x; } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(long a[], long x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(long a[], long x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void Sort(long[] a) { List<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i] = l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int tt = sc.nextInt(); fr: while(tt-- > 0) { int n = sc.nextInt(); long x = sc.nextLong(); int[] a = sc.readArray(n); long ams = 0, sum = 0; for(int it : a) { sum += it; sum = max(sum,0); ams = max(ams, sum); } long[] maxSubArray = new long[n], ans = new long[n+1]; Arrays.fill(maxSubArray, Long.MIN_VALUE); for(int i = 0;i<n;i++) { long curr =0 ; for(int j = i;j<n;j++) { curr += a[j]; int win = (j - i); maxSubArray[win] = max(maxSubArray[win], curr); } } ans[0] = ams; for(int i = 1;i<=n;i++) { ans[i] = max(ans[i-1],0); for(int j = i;j<=n;j++) { ans[i] = max(ans[i], maxSubArray[j-1] + i * x); } } for(long it : ans) fout.print(it + " "); fout.println(); } fout.close(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
26fc36d5d8384da09c9bf8dfe8b343e9
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuffer out = new StringBuffer(); int T = in.nextInt(); OUTER: while (T-->0) { int n = in.nextInt(), x = in.nextInt(); long[] a = new long[n]; for(int i=0; i<n; i++) { a[i] = in.nextInt(); } long[] sum = new long[n+1]; Arrays.fill(sum, Long.MIN_VALUE); sum[0] = 0; for(int i=0; i<n; i++) { long currSum = 0; for(int j=i; j<n; j++) { currSum += a[j]; int size = j-i+1; sum[size] = Math.max(currSum, sum[size]); } } // println(Arrays.toString(sum)); for(int k=0; k<=n; k++) { long max = Long.MIN_VALUE; for(int i=0; i<=n; i++) { max = Math.max(max, sum[i]+ (long) x * Math.min(k, i)); } out.append(max+" "); } out.append("\n"); } System.out.print(out); } private static long gcd(long a, long b) { if (a==0) return b; return gcd(b%a, a); } private static int toInt(String s) { return Integer.parseInt(s); } private static long toLong(String s) { return Long.parseLong(s); } private static void print(String s) { System.out.print(s); } private static void println(String s) { System.out.println(s); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
4e43b914e948bebce5936e75eb859c0b
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
//package cf; import java.util.Scanner; public class C1644 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i = 0 ;i < n; i++) { int m = sc.nextInt(); int x = sc.nextInt(); int [] a = new int[5050]; int [] dp = new int[5050]; for(int j = 1 ; j <= m ;j++) { a[j] = sc.nextInt(); dp[j] = (int) -1e9; } for(int j = 1; j <= m;j++) { int ans = 0; for(int k = 1 ; k < j ;k++) ans+=a[k]; for(int l = j ; l <= m ;l++) { ans = ans + a[l]-a[l-j]; dp[j] = Math.max(ans,dp[j]); } } // for(int j = 1 ;j <= m; j++) // System.out.print(dp[j]+" "); for(int j = 0; j <= m; j++) { int cnt = 0; for(int k = 1 ;k <= m; k++) { cnt = Math.max(cnt,dp[k]+Math.min(j,k)*x); } System.out.print(cnt+" "); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
48d167be7089044d27259338252cdff0
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main (String[] args) throws IOException { Kattio io = new Kattio(); int t = io.nextInt(); for (int ii=0; ii<t; ii++) { int n = io.nextInt(); long x = io.nextInt(); long[] arr = new long[n]; for (int i=0; i<n; i++) { arr[i] = io.nextLong(); } long[] dp = new long[n+1]; //max sum for length i Arrays.fill(dp, Long.MIN_VALUE); dp[0] = 0; for (int i=0; i<n; i++) { long total = 0; int length = 1; for (int j=0; j<n; j++) { if (i + j >= n) break; total += arr[i+j]; dp[length] = Math.max(dp[length], total); //best = Math.max(best, dp[length]); length++; } } for (int i=0; i<=n; i++) { long best = Long.MIN_VALUE; for (int length=0; length<=n; length++) { best = Math.max(best, dp[length] + (Math.min(length, i) * x)); } System.out.print(best + " "); } System.out.println(); } } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(new FileWriter(problemName + ".out")); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
0c8802009301973ca09f01cead98c874
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; public class MyCpClass{ public static void main(String []args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int T = Integer.parseInt(br.readLine().trim()); while(T-- > 0){ String []ip = br.readLine().trim().split(" "); int n = Integer.parseInt(ip[0]); int x = Integer.parseInt(ip[1]); int []a = new int[n]; String []str = br.readLine().trim().split(" "); for(int i=0; i<n; i++) a[i] = Integer.parseInt(str[i]); int [][]dp = new int[n+1][n+1]; for(int len=0; len<=n; len++){ int mxsum = 0, s = 0, e = len-1; for(int i=0; i<len; i++) mxsum += a[i]; int sum = mxsum; while(e < n-1){ e++; sum = sum+a[e]-a[s]; mxsum = Math.max(mxsum, sum); s++; } for(int k=0; k<=n; k++) dp[len][k] = mxsum + x*Math.min(len, k); } for(int k=0; k<=n; k++){ int max = Integer.MIN_VALUE; for(int i=0; i<=n; i++) max = Math.max(max, dp[i][k]); sb.append(max + " "); } sb.append("\n"); } System.out.println(sb); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
8b3b58585f565594c2dc15fdbef658f6
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; import java.lang.Math; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; public class Main { static PrintWriter pw; static Scanner sc; static StringBuilder ans; static long mod = 1000000000+7; static void pn(final Object arg) { pw.print(arg); pw.flush(); } /*-------------- for input in an value ---------------------*/ static int ni() { return sc.nextInt(); } static long nl() { return sc.nextLong(); } static double nd() { return sc.nextDouble(); } static String ns() { return sc.next(); } static String nLine() { return sc.nextLine(); } static void ap(int arg) { ans.append(arg); } static void ap(long arg) { ans.append(arg); } static void ap(String arg) { ans.append(arg); } static void ap(StringBuilder arg) { ans.append(arg); } static void apn() { ans.append("\n"); } static void apn(int arg) { ans.append(arg+"\n"); } static void apn(long arg) { ans.append(arg+"\n"); } static void apn(String arg) { ans.append(arg+"\n"); } static void apn(StringBuilder arg) { ans.append(arg+"\n"); } static void yes() { ap("Yes\n"); } static void no() { ap("No\n"); } /* for Dubuggin */ static void printArr(int ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); } static void printArr(long ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); } static void printArr(String ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); } static void printIntegerList(List<Integer> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); } static void printLongList(List<Long> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); } static void printStringList(List<String> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); } /*-------------- for input in an array ---------------------*/ static void readArray(int arr[]){ for(int i=0; i<arr.length; i++)arr[i] = ni(); } static void readArray(long arr[]){ for(int i=0; i<arr.length; i++)arr[i] = nl(); } static void readArray(String arr[]){ for(int i=0; i<arr.length; i++)arr[i] = ns(); } static void readArray(double arr[]){ for(int i=0; i<arr.length; i++)arr[i] = nd(); } /*-------------- File vs Input ---------------------*/ static void runFile() throws Exception { sc = new Scanner(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); } static void runIo() throws Exception { pw =new PrintWriter(System.out); sc = new Scanner(System.in); } static int log2(int n) { return (int)(Math.log(n) / Math.log(2)); } static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0);} static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long nCr(long n, long r) { // Combinations if (n < r) return 0; if (r > n - r) { // because nCr(n, r) == nCr(n, n - r) r = n - r; } long ans = 1L; for (long i = 0; i < r; i++) { ans *= (n - i); ans /= (i + 1); } return ans; } static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);} static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean sv[] = new boolean[10002]; static void seive() { //true -> not prime // false->prime sv[0] = sv[1] = true; sv[2] = false; for(int i = 0; i< sv.length; i++) { if( !sv[i] && (long)i*(long)i < sv.length ) { for ( int j = i*i; j<sv.length ; j += i ) { sv[j] = true; } } } } static long kadensAlgo(long ar[]) { int n = ar.length; long pre = ar[0]; long ans = ar[0]; for(int i = 1; i<n; i++) { pre = Math.max(pre + ar[i], ar[i]); ans = Math.max(pre, ans); } return ans; } static long binpow( long a, long b) { long res = 1; while (b > 0) { if ( (b & 1) > 0){ res = (res * a); } a = (a * a); b >>= 1; } return res; } static long factorial(long n) { long res = 1, i; for (i = 2; i <= n; i++){ res = ((res%mod) * (i%mod))%mod; } return res; } static int getCountPrime(int n) { int ans = 0; while (n%2==0) { ans ++; n /= 2; } for (int i = 3; i *i<= n; i+= 2) { while (n%i == 0) { ans ++; n /= i; } } if(n > 1 ) ans++; return ans; } static void sort(int[] arr) { /* because Arrays.sort() uses quicksort which is dumb Collections.sort() uses merge sort */ ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } static void sort(long[] arr) { /* because Arrays.sort() uses quicksort which is dumb Collections.sort() uses merge sort */ ArrayList<Long> ls = new ArrayList<Long>(); for(Long x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } static int lowerBound(ArrayList<Integer> ar, int k, int l, int r) { while (l <= r) { int m = (l + r) >> 1 ; if (ar.get(m) >= k) { r = m - 1 ; } else { l = m + 1 ; } } return l ; } static int upperBound(long ar[], long k, int l, int r) { while (l <= r) { int m = (l + r) >> 1 ; if (ar[m] > k) { r = m - 1 ; } else { l = m + 1 ; } } return l ; } static int lowerBound(int ar[], int k, int l, int r) { while (l <= r) { int m = (l + r) >> 1 ; if (ar[m] >= k) { r = m - 1 ; } else { l = m + 1 ; } } return l ; } static void findDivisor(int n, ArrayList<Integer> al) { for(int i = 1; i*i <= n; i++){ if( n % i == 0){ if(n/i==i){ al.add(i); } else{ al.add(n/i); al.add(i); } } } } static class Pair{ int a; int b; Pair( int a, int b ){ this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Pair)) return false; Pair pair = (Pair) o; return a == pair.a && b == pair.b; } @Override public int hashCode() { int result = a; result = 31 * result + b; return result; } } public static void main(String[] args) throws Exception { // runFile(); runIo(); int t; t = 1; t = sc.nextInt(); ans = new StringBuilder(); while( t-- > 0 ) { solve(); } pn(ans+""); } public static void solve ( ) { int n =ni(); int x = ni(); int ar[] = new int[n]; readArray(ar); int maxAr[] = new int[n+1]; for(int i = 1; i<=n; i++) { int max = 0; int sum = 0; int j = 0; for( j = 0; j<i; j++ ) sum += ar[j]; max = sum; for( ; j<n; j++) { sum += ar[j]; sum -= ar[j-i]; max = max(sum, max); } maxAr[i] = max; } for(int k = 0; k<=n; k++) { int max = 0; for(int i = 0; i<=n; i++) { max = max(max, maxAr[i] + x*min(k, i) ); } ap(max+" "); } apn(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
056aaadb3d915cb6d8595ce9117f3146
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class C_Edu_Round_123 { public static long MOD = 998244353; static int[] dp; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int T = in.nextInt(); //System.out.println(cal(1608737403, 1000000000) - 923456789987654321L); for (int z = 0; z < T; z++) { int n = in.nextInt(); long x = in.nextInt(); int[] data = new int[n]; long[] pre = new long[n]; for (int i = 0; i < n; i++) { data[i] = in.nextInt(); pre[i] += data[i] + (i > 0 ? pre[i - 1] : 0); } long[] max = new long[n + 1]; Arrays.fill(max, Long.MIN_VALUE); for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int l = (j - i + 1); max[l] = Long.max(max[l], pre[j] - (i > 0 ? pre[i - 1] : 0)); } } for (int i = n - 1; i >= 0; i--) { max[i] = Long.max(max[i], max[i + 1]); } long cur = Long.max(0, max[0]); for (int i = 0; i <= n; i++) { cur = Long.max(cur, max[i] + i * x); out.print(cur + " "); } out.println(); } out.close(); } static boolean check(long hc, long dc, long hm, long dm) { long a = hm / dc; if (hm % dc != 0) { a++; } long b = hc / dm; if (hc % dm != 0) { b++; } return a <= b; } static int find(int index, int[] u) { if (u[index] != index) { return u[index] = find(u[index], u); } return index; } static int abs(int a) { return a < 0 ? -a : a; } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point { int x; int y; public Point(int start, int end) { this.x = start; this.y = end; } public String toString() { return x + " " + y; } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return (val * val) % MOD; } else { return (val * ((val * a) % MOD)) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
e6a183091018a2c01198001849677dde
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class MyClass { static FastReader in=new FastReader(); static final Random random=new Random(); static long mod=1000000007L; public static void main (String[] args) throws java.lang.Exception { int t=in.nextInt(); StringBuilder res=new StringBuilder(); while(t-->0) { int n = in.nextInt(); int x = in.nextInt(); String[] arr = in.nextLine().split(" "); int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(arr[i]); int[] temp = new int[n+1]; Arrays.fill(temp, Integer.MIN_VALUE); temp[0] = 0; for(int i=0; i<n; i++) { int sum = 0; for(int j=i; j<n; j++) { sum += a[j]; temp[j-i+1] = Math.max(sum, temp[j-i+1]); } } for(int k=0; k<n+1; k++) { int maxVal = 0; for(int j=0; j<n+1; j++) { if(k>=j) { maxVal = Math.max(maxVal, temp[j]+(j*x)); } else { maxVal = Math.max(maxVal, temp[j]+(k*x)); } } res.append(maxVal).append(" "); } res.append("\n"); } System.out.println(res); } static int max(int a, int b) { if(a<b) return b; return a; } static void ruffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static < E > void print(E res) { System.out.println(res); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if(a<0) return -1*a; return a; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int [] readintarray(int n) { int res [] = new int [n]; for(int i = 0; i<n; i++)res[i] = nextInt(); return res; } long [] readlongarray(int n) { long res [] = new long [n]; for(int i = 0; i<n; i++)res[i] = nextLong(); return res; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
37c50afe7fb3addca2fa99cafc3b59d7
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; public class IncreaseSubarraySums { public static PrintWriter out; private static Scanner sc; public IncreaseSubarraySums(){ out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new Scanner(System.in); } public static void main(String[] args) { IncreaseSubarraySums ans = new IncreaseSubarraySums(); ans.solve(); out.close(); } // hard work wins over skill private void solve() { int tests = sc.nextInt(); while(tests-->0){ int N = sc.nextInt(); int X = sc.nextInt(); int[]array = new int[N]; // why here N+1 int[] result = new int[N+1]; // why is this required ? Arrays.fill(result, Integer.MIN_VALUE); for(int i=0;i<N;i++){ array[i] = sc.nextInt(); } for(int i=0;i<N;i++) { int sum=0; for(int j=i;j<N;j++){ sum += array[j]; result[j-i+1] = Math.max(result[j-i+1], sum); } } // why k<=N here for(int k=0;k<=N;k++){ int ans=0; for(int len=0;len<=N;len++){ ans = Math.max(ans, (result[len]+ Math.min(len,k)*X)); } out.printf("%d ", ans); } out.printf("\n"); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
26278d511675239234258aef037e67f2
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; // BEFORE 31ST MARCH 2022 !! //MAX RATING EVER ACHIEVED-1622(LETS SEE WHEN WILL I GET TO CHANGE THIS) ////*************************************************************************** /* public class E_Gardener_and_Tree implements Runnable{ public static void main(String[] args) throws Exception { new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start(); } public void run(){ WRITE YOUR CODE HERE!!!! JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!! } } */ /////************************************************************************** public class C_Increase_Subarray_Sums{ public static void main(String[] args) { FastScanner s= new FastScanner(); //PrintWriter out=new PrintWriter(System.out); //end of program //out.println(answer); //out.close(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p<t){ int n=s.nextInt(); long x=s.nextLong(); long array[]= new long[n]; for(int i=0;i<n;i++){ array[i]=s.nextLong(); } long dp[][]= new long[n][n+1];//[starting at this pos][num of operations] dp[n-1][0]=Math.max(array[n-1],0); dp[n-1][1]=Math.max((array[n-1]+x),0); for(int i=2;i<=n;i++){ dp[n-1][i]=Integer.MIN_VALUE; } for(int i=n-2;i>=0;i--){//iterating over pos int range=n-i; for(int j=0;j<=n;j++){//iterating over num of operations if(j>range){ dp[i][j]=Integer.MIN_VALUE; continue; } if(j==0){ dp[i][j]=Math.max(array[i],(array[i]+dp[i+1][0])); dp[i][j]=Math.max(dp[i][j],0); } else{ long op1=-1; { //operation applied op1=Math.max((array[i]+x),Math.max(array[i]+x+dp[i+1][j-1],0)); } long op2=-1; { //opearion not applied op2=Math.max((array[i]),Math.max(array[i]+dp[i+1][j],0)); } dp[i][j]=Math.max(op1,op2); } } } // { // //printinhg // for(int i=0;i<n;i++){ // for(int j=0;j<=n;j++){ // System.out.print(dp[i][j]+" "); // } // System.out.println(); // } // } long ans[]= new long[n+1]; for(int i=0;i<=n;i++){ long best=0; for(int j=0;j<n;j++){ best=Math.max(best,dp[j][i]); } if(i>0){ ans[i]=Math.max(ans[i-1],best); } else{ ans[i]=best; } } for(int i=0;i<=n;i++){ res.append(ans[i]+" "); } res.append(" \n"); p++; } System.out.println(res); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } static long modpower(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // SIMPLE POWER FUNCTION=> static long power(long x, long y) { long res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = res * x; // y must be even now y = y >> 1; // y = y/2 x = x * x; // Change x to x^2 } return res; } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
75fd36c20fbf287824b836a3634bc57d
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
// Sliding window + implementation import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while(t-- > 0){ int n = sc.nextInt(); int k = sc.nextInt(); int a[] = new int[n]; for(int i = 0; i < n; i++){ a[i] = sc.nextInt(); } int maxSubArray[] = new int[n+1]; for(int i = 1; i <= n; i++){ int sum = 0; for(int j = 0; j < i; j++){ sum += a[j]; } int max = sum; for(int j = 1; j <= (n-i); j++){ sum-=a[j-1]; sum+=a[j+i-1]; max = Math.max(max, sum); } maxSubArray[i] = max; } // for(int i = 1; i <= n; i++){ // System.out.print(maxSubArray[i]+" "); // } // System.out.println(); int dp[] = new int[n+1]; for(int i = 0; i <= n; i++){ int max = Integer.MIN_VALUE; for(int j = i; j <= n; j++){ max = Math.max(max, maxSubArray[j]); } int val = 0; if(i!=0) val = dp[i-1]; dp[i] = Math.max(val, max+k*i); } StringBuilder temp = new StringBuilder(); for(int i = 0; i <= n; i++){ temp.append(dp[i]+" "); } sb.append(temp+"\n"); } System.out.println(sb); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
6e05b80023a91176a4b96135eb310686
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-- != 0){ int n = s.nextInt(); long x = s.nextLong(); long arr[] = new long[n+1]; for(int i = 1; i <= n; i++){ arr[i] = s.nextLong(); } long dp[] = new long[n+1]; for(int i = 1; i <= n; i++){ long curr = 0; for(int j = 1; j <= i; j++){ curr += arr[j]; } long max = curr; for(int j = i + 1; j <= n; j++){ curr += arr[j] - arr[j-i]; max = Math.max(max,curr); } dp[i] = max; } for(int i = n - 1; i >= 1; i--){ dp[i] = Math.max(dp[i],dp[i+1]); } long best = Math.max(dp[1],0); for(int i = 0; i <= n; i++){ best = Math.max(best,dp[i]+x*i); System.out.print(best + " "); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
55d17edec4ee9d11b0f1aca43a90dcfb
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.awt.image.AreaAveragingScaleFilter; import java.io.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; public class Pratice { static final long mod = 1000000007; static StringBuilder sb = new StringBuilder(); static int xn = (int) (2e5 + 10); static long ans; static boolean prime[] = new boolean[1000001]; // calculate sqrt and cuberoot // static Set<Long> set=new TreeSet<>(); // static // { // long n=1000000001; // // // for(int i=1;i*i<=n;i++) // { // long x=i*i; // set.add(x); // } // for(int i=1;i*i*i<=n;i++) // { // long x=i*i*i; // set.add(x); // } // } static void sieveOfEratosthenes() { for (int i = 0; i <= 1000000; i++) prime[i] = true; prime[0] = false; prime[1] = false; for (int p = 2; p * p <= 1000000; p++) { if (prime[p] == true) { for (int i = p * p; i <= 1000000; i += p) prime[i] = false; } } } public static void main(String[] args) throws IOException { Reader reader = new Reader(); int t = reader.nextInt(); while (t-- > 0) { int n=reader.nextInt(); int x=reader.nextInt(); int a[]=new int[n]; for (int i = 0; i < n; i++) { a[i]=reader.nextInt(); } int ans[]=new int[n+1]; Arrays.fill(ans,Integer.MIN_VALUE); ans[0]=0; for(int i=0;i<n;i++) { int temp=0; for(int j=i;j<n;j++) { temp+=a[j]; ans[j-i+1]=Math.max(ans[j-i+1],temp); } } //for(int i=0;i<=n;i++) cout<<ans[i]<<" "; //cout<<endl; for(int i=0;i<=n;i++) { int ans1=0; for(int j=0;j<=n;j++) { ans1=Math.max(ans1,Math.min(j,i)*x + ans[j]); } System.out.print(ans1+ " "); } System.out.println(); } } // static void SieveOfEratosthenes(int n, boolean prime[], // boolean primesquare[], int a[]) // { // // Create a boolean array "prime[0..n]" and // // initialize all entries it as true. A value // // in prime[i] will finally be false if i is // // Not a prime, else true. // for (int i = 2; i <= n; i++) // prime[i] = true; // // /* Create a boolean array "primesquare[0..n*n+1]" // and initialize all entries it as false. // A value in squareprime[i] will finally // be true if i is square of prime, // else false.*/ // for (int i = 0; i < ((n * n) + 1); i++) // primesquare[i] = false; // // // 1 is not a prime number // prime[1] = false; // // for (int p = 2; p * p <= n; p++) { // // If prime[p] is not changed, // // then it is a prime // if (prime[p] == true) { // // Update all multiples of p // for (int i = p * 2; i <= n; i += p) // prime[i] = false; // } // } // // int j = 0; // for (int p = 2; p <= n; p++) { // if (prime[p]) { // // a[j] = p; // // // primesquare[p * p] = true; // j++; // } // } // } // // // static int countDivisors(int n) // { // // if (n == 1) // return 1; // // boolean prime[] = new boolean[n + 1]; // boolean primesquare[] = new boolean[(n * n) + 1]; // // int a[] = new int[n]; // // // SieveOfEratosthenes(n, prime, primesquare, a); // // // int ans = 1; // // // Loop for counting factors of n // for (int i = 0;; i++) { // // a[i] is not less than cube root n // if (a[i] * a[i] * a[i] > n) // break; // // int cnt = 1; // // // if a[i] is a factor of n // while (n % a[i] == 0) { // n = n / a[i]; // // // incrementing power // cnt = cnt + 1; // } // // // ans = ans * cnt; // } // // // if (prime[n]) // ans = ans * 2; // // // Second case // else if (primesquare[n]) // ans = ans * 3; // // // Third case // else if (n != 1) // ans = ans * 4; // // return ans; // Total divisors // } public static long[] inarr(long n) throws IOException { Reader reader = new Reader(); long arr[]=new long[(int) n]; for (long i = 0; i < n; i++) { arr[(int) i]=reader.nextLong(); } return arr; } public static boolean checkPerfectSquare(int number) { int x=number % 10; if (x==2 || x==3 || x==7 || x==8) { return false; } for (int i=0; i<=number/2 + 1; i++) { if (i*i==number) { return true; } } return false; } // check number is prime or not public static boolean isPrime(int n) { return BigInteger.valueOf(n).isProbablePrime(1); } // return the gcd of two numbers public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } // return lcm of number static long lcm(int a, int b) { return (a / gcd(a, b)) * b; } // number of digits in the given number public static long numberOfDigits(long n) { long ans= (long) (Math.floor((Math.log10(n)))+1); return ans; } // return most significant bit in the number public static long mostSignificantNumber(long n) { double k=Math.log10(n); k=k-Math.floor(k); int ans=(int)Math.pow(10,k); return ans; } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
2f3a3b95f08f9c2b334dee5fbb357ae1
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; public class CodeForces{ public static void main(String[] args) throws FileNotFoundException { FastScanner fs = new FastScanner(); int t = fs.nextInt(); while(t-- > 0) { int n = fs.nextInt(), x = fs.nextInt(); int[] a = fs.readArray(n); int[] pref = new int[n + 1]; for(int i = 0; i < n; i++) { pref[i + 1] = pref[i] + a[i]; } int[] max = new int[n + 1]; max[0] = 0; for(int k = 1; k <= n; k++) { int max_sum = Integer.MIN_VALUE; for(int j = 0; j + k <= n; j++) { max_sum = Math.max(max_sum, pref[j + k] - pref[j]); } max[k] = max_sum; } StringBuilder sb = new StringBuilder(); // System.out.println(Arrays.toString(max)); for(int k = 0; k <= n; k++) { long max_sum = Integer.MIN_VALUE; for(int i = 0; i <= n; i++) { max_sum = Math.max(max_sum, max[i] + Math.min(k, i)*x*1l); } sb.append(max_sum + " "); } System.out.println(sb.toString()); } } static int gcd(int a, int b) { if(b == 0) return a; return gcd(b, a%b); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for(int i = 0; i < n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
6c9a80bbbd5cec6c94435df858683e0b
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { static AReader scan = new AReader(); static int MOD = (int)1e9+7; static void slove() { int n = scan.nextInt(); int x = scan.nextInt(); long[][] dp = new long[n+10][n+10]; int[] a = new int[n+10]; long[] ans = new long[n+10]; for(int i = 1;i<=n;i++) a[i] = scan.nextInt(); for(int i = 1;i<=n;i++){ for(int j = 0;j<=n;j++){ dp[i][j] = Math.max(dp[i-1][j],0) + a[i]; if(j > 0){ dp[i][j] = Math.max(dp[i][j],Math.max(0,dp[i-1][j-1]) + a[i] + x); } ans[j] = Math.max(ans[j],dp[i][j]); } } for(int i = 0;i<=n;i++){ System.out.print(ans[i]+" "); } System.out.println(); } public static void main(String[] args) { int T = scan.nextInt(); while (T-- > 0) { slove(); } } } class AReader { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine(); if (nextLine == null) { return false; } tokenizer = new StringTokenizer(nextLine); } return true; } public String nextLine() { tokenizer = new StringTokenizer(""); return innerNextLine(); } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
50d6f9beeb5acc229be2091655cc4b55
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class DifferentialSorting { public static PrintWriter out; public static void main(String[] args)throws IOException{ Scanner sc=new Scanner(); out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int x=sc.nextInt(); int a[]=new int[n+1]; for(int i=1;i<=n;i++) { a[i]=a[i-1]+sc.nextInt(); } int ans[]=new int[n+1]; for(int i=0;i<=n;++i){ for(int j=i-1;j>=0;j--){ ans[i-j]=Math.max(ans[i-j],a[i]-a[j]+(i-j)*x); } } for(int i=ans.length-2;i>=0;i--) { ans[i]=Math.max(ans[i],ans[i+1]-x); } for(int i=1;i<ans.length;i++) { ans[i]=Math.max(ans[i],ans[i-1]); } for(int i=0;i<=n;i++) { out.print(ans[i]+" "); } out.println(); } out.close(); } public static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
684b35059181c38c1294f8e20c78389c
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class C { static class Pair { int f;int s; // Pair(){} Pair(int f,int s){ this.f=f;this.s=s;} } static class Fast { BufferedReader br; StringTokenizer st; public Fast() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArray1(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (IOException e) { e.printStackTrace(); } return str; } } /* static long noOfDivisor(long a) { long count=0; long t=a; for(long i=1;i<=(int)Math.sqrt(a);i++) { if(a%i==0) count+=2; } if(a==((long)Math.sqrt(a)*(long)Math.sqrt(a))) { count--; } return count; }*/ static boolean isPrime(long a) { for (long i = 2; i <= (long) Math.sqrt(a); i++) { if (a % i == 0) return false; } return true; } static void primeFact(int n) { int temp = n; HashMap<Integer, Integer> h = new HashMap<>(); for (int i = 2; i * i <= n; i++) { if (temp % i == 0) { int c = 0; while (temp % i == 0) { c++; temp /= i; } h.put(i, c); } } if (temp != 1) h.put(temp, 1); } static void reverseArray(int a[]) { int n = a.length; for (int i = 0; i < n / 2; i++) { a[i] = a[i] ^ a[n - i - 1]; a[n - i - 1] = a[i] ^ a[n - i - 1]; a[i] = a[i] ^ a[n - i - 1]; } } //Function to find the sum of contiguous subarray with maximum sum. static long maxSubarraySum(int arr[], int n){ // Your code here long s=0; long max=arr[0]; for(int i=0;i<arr.length;i++) { s=s+arr[i]; if(max<s) max=s; if(s<0) s=0; } return max; } public static void main(String args[]) throws IOException { Fast sc = new Fast(); PrintWriter out = new PrintWriter(System.out); int t1 = sc.nextInt(); outer: while (t1-- > 0) { int n=sc.nextInt(); int x=sc.nextInt(); int a[]=sc.readArray(n); int l1[]=new int[n+1]; // HashMap<Integer> Arrays.fill(l1, Integer.MIN_VALUE); l1[0]=0; for(int i=0;i<n;i++) { int sum=0; for(int j=i;j<n;j++) { int l=j-i+1; sum+=a[j]; if(l1[l]<sum) { l1[l]=sum; } } } // for(int ne:l1) // out.println(ne); for(int k=0;k<=n;k++) { int val=-1; for(int l=0;l<=n;l++) { val=Math.max(val,l1[l]+Math.min(k,l)*x); } out.print(val+" "); } out.println(); } out.close(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output