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 | 57448ff2c69c52eec6319d08aed0e979 | 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.*;
// Created by @thesupremeone on 22/02/22
public class C {
void solve() {
int ts = getInt();
for (int t = 1; t <= ts; t++) {
int n = getInt();
long x = getInt();
int[] arr = new int[n+1];
long[] prefix = new long[n+1];
for (int i = 1; i <= n; i++) {
arr[i] = getInt();
prefix[i] = prefix[i-1]+arr[i];
}
long maxSum = 0;
int length = 0;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
long sum = prefix[j]-prefix[i-1];
if(sum>maxSum){
maxSum = sum;
length = j-i+1;
}else if(sum==maxSum){
length = Math.max(j-i+1, length);
}
}
}
long[] sum = new long[n+1];
for (int k = 1; k <= n; k++) {
long s = Long.MIN_VALUE;
for (int l = 1; l <= n+1-k; l++) {
s = Math.max(s, prefix[l+k-1]-prefix[l-1]);
}
sum[k] = s;
}
long max = Integer.MIN_VALUE;
for (int i = n; i > 0; i--) {
max = Math.max(max, sum[i]);
sum[i] = Math.max(sum[i], max);
}
long[] ans = new long[n+1];
for (int k = 0; k <= n; k++) {
if(k>0){
ans[k] = Math.max(ans[k], ans[k-1]);
}
ans[k] = Math.max(ans[k], sum[k]+k*x);
if(k<=length){
ans[k] = Math.max(ans[k], maxSum+k*x);
}
}
for (int k = 0; k <= n; k++) {
print(ans[k]);
print(" ");
}
println("");
}
}
public static void main(String[] args) throws Exception {
if (isOnlineJudge()) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new BufferedWriter(new OutputStreamWriter(System.out));
new C().solve();
out.flush();
} else {
localJudge = new Thread();
in = new BufferedReader(new FileReader("input.txt"));
out = new BufferedWriter(new FileWriter("output.txt"));
localJudge.start();
new C().solve();
out.flush();
localJudge.suspend();
}
}
static boolean isOnlineJudge() {
try {
return System.getProperty("ONLINE_JUDGE") != null
|| System.getProperty("LOCAL") == null;
} catch (Exception e) {
return true;
}
}
// Fast Input & Output
static Thread localJudge = null;
static BufferedReader in;
static StringTokenizer st;
static BufferedWriter out;
static String getLine() {
try {
return in.readLine();
} catch (Exception ignored) {
return "";
}
}
static String getToken() {
if (st == null || !st.hasMoreTokens())
st = new StringTokenizer(getLine());
return st.nextToken();
}
static int getInt() {
return Integer.parseInt(getToken());
}
static long getLong() {
return Long.parseLong(getToken());
}
static void print(Object s) {
try {
out.write(String.valueOf(s));
} catch (Exception ignored) {
}
}
static void println(Object s) {
try {
out.write(String.valueOf(s));
out.newLine();
} catch (Exception ignored) {
}
}
} | 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 | 2d4948250195429593b7cc922fdfd48c | 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.*;
public class C {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(reader.readLine());
while (t > 0) {
t--;
StringTokenizer st = new StringTokenizer(reader.readLine());
int n = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int[] a = new int[n];
st = new StringTokenizer(reader.readLine());
int[] prefixSum = new int[n + 1];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st.nextToken());
prefixSum[i + 1] = prefixSum[i] + a[i];
}
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 0);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
int subArraySum = prefixSum[j] - prefixSum[i];
if (map.containsKey(j - i)) {
map.put(j - i, Math.max(map.get(j - i), subArraySum));
} else {
map.put(j - i, subArraySum);
}
}
}
for (int k = 0; k <= n; k++) {
int max = Integer.MIN_VALUE;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
max = Math.max(max, entry.getValue() + (Math.min(k, entry.getKey()) * x));
}
out.print(max + " ");
}
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 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 | 7e2816e77bfc95e71bdf39b2ea6cd773 | 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.*;
public class C {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(reader.readLine());
while (t > 0) {
t--;
StringTokenizer st = new StringTokenizer(reader.readLine());
int n = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int[] a = new int[n];
int maxSum = Integer.MIN_VALUE;
int currSum = 0;
st = new StringTokenizer(reader.readLine());
int[] prefixSum = new int[n + 1];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st.nextToken());
currSum = Math.max(currSum + a[i], a[i]);
maxSum = Math.max(maxSum, currSum);
prefixSum[i + 1] = prefixSum[i] + a[i];
}
int[] maxSegment = new int[n + 1];
for (int i = 1; i < n + 1; i++) {
maxSegment[i] = Integer.MIN_VALUE;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
int subArraySum = prefixSum[j] - prefixSum[i];
maxSegment[j - i] = Math.max(maxSegment[j - i], subArraySum);
}
}
for (int k = 0; k <= n; k++) {
int max = Integer.MIN_VALUE;
for (int i = 0; i <= n; i++) {
max = Math.max(max, maxSegment[i] + (Math.min(k, i) * x));
}
out.print(max + " ");
}
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 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 | 97f4530d1a680d17663992dbe9ce7bf2 | 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 new1{
static long mod = 1000000007;
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static long func(long[][] dp , int x, int k, int[] arr, long x1) {
int n = arr.length;
if(x >= n) return 0;
if(dp[x][k] != Long.MIN_VALUE) return dp[x][k];
long aa = 0;
if(k - 1 >= 0) aa = func(dp, x + 1, k - 1, arr, x1) + arr[x] + x1;
long bb = 0;
if(k - 1 >= 0) bb = arr[x] + x1;
long cc = func(dp, x + 1, k, arr, x1) + arr[x];
long ans = Math.max(cc, Math.max(aa, bb));
dp[x][k] = ans;
return ans;
}
public static void main(String[] args) throws IOException{
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader s = new FastReader();
int t = s.nextInt();
for(int z = 0; z < t; z++) {
int n = s.nextInt();
int x1 = s.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = s.nextInt();
long[][] dp = new long[n][n + 1];
for(int i = 0; i < n; i++) Arrays.fill(dp[i], Long.MIN_VALUE);
for(int i = 0; i <= n; i++) {
long ans = 0;
for(int j = 0; j < n; j++) {
ans = Math.max(ans, func(dp, j, i, arr, x1));
//long aa = func(dp, i, j, arr, x1);
//System.out.println(aa + " " + i + " " + j);
}
output.write(ans + " ");
}
output.write("\n");
}
output.flush();
}
}
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();
}
public 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 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 | 10a439506f93cf5aa63ce436255a8a42 | 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 new1{
static long mod = 1000000007;
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static long func(long[][] dp , int x, int k, int[] arr, long x1) {
int n = arr.length;
if(x >= n) return 0;
if(dp[x][k] != Long.MIN_VALUE) return dp[x][k];
long aa = 0;
if(k - 1 >= 0) aa = func(dp, x + 1, k - 1, arr, x1) + arr[x] + x1;
long bb = 0;
if(k - 1 >= 0) bb = arr[x] + x1;
long cc = func(dp, x + 1, k, arr, x1) + arr[x];
long ans = Math.max(cc, Math.max(aa, bb));
dp[x][k] = ans;
return ans;
}
public static void main(String[] args) throws IOException{
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader s = new FastReader();
int t = s.nextInt();
for(int z = 0; z < t; z++) {
int n = s.nextInt();
int x1 = s.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = s.nextInt();
long[][] dp = new long[n][n + 1];
for(int i = 0; i < n; i++) Arrays.fill(dp[i], Long.MIN_VALUE);
for(int i = n - 1; i >= 0; i--) {
for(int j = n; j >= 0; j--) func(dp, i, j, arr, x1);
}
for(int i = 0; i <= n; i++) {
long ans = 0;
for(int j = 0; j < n; j++) {
ans = Math.max(ans, func(dp, j, i, arr, x1));
}
output.write(ans + " ");
}
output.write("\n");
}
output.flush();
}
}
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();
}
public 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 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 | df335fc36a6967dd0fca058c7fdb9a55 | 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 new1{
static long mod = 1000000007;
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static long func(long[][] dp , int x, int k, int[] arr, long x1) {
int n = arr.length;
if(x >= n) return 0;
if(dp[x][k] != Long.MIN_VALUE) return dp[x][k];
long aa = 0;
if(k - 1 >= 0) aa = func(dp, x + 1, k - 1, arr, x1) + arr[x] + x1;
long bb = 0;
if(k - 1 >= 0) bb = arr[x] + x1;
long cc = func(dp, x + 1, k, arr, x1) + arr[x];
long ans = Math.max(cc, Math.max(aa, bb));
dp[x][k] = ans;
return ans;
}
public static void main(String[] args) throws IOException{
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader s = new FastReader();
int t = s.nextInt();
for(int z = 0; z < t; z++) {
int n = s.nextInt();
int x1 = s.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = s.nextInt();
long[][] dp = new long[n][n + 1];
for(int i = 0; i < n; i++) Arrays.fill(dp[i], Long.MIN_VALUE);
for(int i = n - 1; i >= 0; i--) {
for(int j = n; j >= 0; j--) func(dp, i, j, arr, x1);
}
for(int i = 0; i <= n; i++) {
long ans = 0;
for(int j = 0; j < n; j++) {
ans = Math.max(ans, func(dp, j, i, arr, x1));
}
output.write(ans + " ");
}
output.write("\n");
}
output.flush();
}
}
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();
}
public 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 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 | fdfa2437b540e3a29b2feb84b7136f1c | 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 new1{
static long mod = 1000000007;
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static long func(long[][] dp , int x, int k, int[] arr, long x1) {
int n = arr.length;
if(x >= n) return 0;
if(dp[x][k] != Long.MIN_VALUE) return dp[x][k];
long aa = 0;
if(k - 1 >= 0) aa = func(dp, x + 1, k - 1, arr, x1) + arr[x] + x1;
long bb = 0;
if(k - 1 >= 0) bb = arr[x] + x1;
long cc = func(dp, x + 1, k, arr, x1) + arr[x];
long ans = Math.max(cc, Math.max(aa, bb));
dp[x][k] = ans;
return ans;
}
public static void main(String[] args) throws IOException{
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader s = new FastReader();
int t = s.nextInt();
for(int z = 0; z < t; z++) {
int n = s.nextInt();
int x1 = s.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = s.nextInt();
long[][] dp = new long[n][n + 1];
for(int i = 0; i < n; i++) Arrays.fill(dp[i], Long.MIN_VALUE);
for(int i = n - 1; i >= 0; i--) {
for(int j = 0; j < n + 1; j++) func(dp, i, j, arr, x1);
}
for(int i = 0; i <= n; i++) {
long ans = 0;
for(int j = 0; j < n; j++) {
ans = Math.max(ans, func(dp, j, i, arr, x1));
}
output.write(ans + " ");
}
output.write("\n");
}
output.flush();
}
}
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();
}
public 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 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 | 292ed00f4318b3b18399e4a0809d0bc3 | 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 new1{
static long mod = 1000000007;
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static long func(long[][] dp , int x, int k, int[] arr, long x1) {
int n = arr.length;
if(x >= n) return 0;
if(dp[x][k] != Long.MIN_VALUE) return dp[x][k];
long aa = 0;
if(k - 1 >= 0) aa = func(dp, x + 1, k - 1, arr, x1) + arr[x] + x1;
long bb = 0;
if(k - 1 >= 0) bb = arr[x] + x1;
long cc = func(dp, x + 1, k, arr, x1) + arr[x];
long ans = Math.max(cc, Math.max(aa, bb));
dp[x][k] = ans;
return ans;
}
public static void main(String[] args) throws IOException{
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader s = new FastReader();
int t = s.nextInt();
for(int z = 0; z < t; z++) {
int n = s.nextInt();
int x1 = s.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = s.nextInt();
long[][] dp = new long[n][n + 1];
for(int i = 0; i < n; i++) Arrays.fill(dp[i], Long.MIN_VALUE);
for(int i = n - 1; i >= 0; i--) {
for(int j = 0; j < n + 1; j++) func(dp, i, j, arr, x1);
}
for(int i = 0; i <= n; i++) {
long ans = 0;
for(int j = 0; j < n; j++) {
ans = Math.max(ans, dp[j][i]);
}
output.write(ans + " ");
}
output.write("\n");
}
output.flush();
}
}
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();
}
public 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 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 | d792edc4e55c26b50ceab3c0d5adae80 | 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 new1{
static long mod = 1000000007;
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static long func(long[][] dp , int x, int k, int[] arr, long x1) {
int n = arr.length;
if(x >= n) return 0;
if(dp[x][k] != Long.MIN_VALUE) return dp[x][k];
long aa = 0;
if(k - 1 >= 0) aa = func(dp, x + 1, k - 1, arr, x1) + arr[x] + x1;
long bb = 0;
if(k - 1 >= 0) bb = arr[x] + x1;
long cc = func(dp, x + 1, k, arr, x1) + arr[x];
long ans = Math.max(cc, Math.max(aa, bb));
dp[x][k] = ans;
return ans;
}
public static void main(String[] args) throws IOException{
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader s = new FastReader();
int t = s.nextInt();
for(int z = 0; z < t; z++) {
int n = s.nextInt();
int x1 = s.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = s.nextInt();
long[][] dp = new long[n][n + 1];
for(int i = 0; i < n; i++) Arrays.fill(dp[i], Long.MIN_VALUE);
for(int i = 0; i <= n; i++) {
long ans = 0;
for(int j = 0; j < n; j++) {
ans = Math.max(ans, func(dp, j, i, arr, x1));
//long aa = func(dp, i, j, arr, x1);
//System.out.println(aa + " " + i + " " + j);
}
output.write(ans + " ");
}
output.write("\n");
}
output.flush();
}
}
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();
}
public 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 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 | 31254cf562c9e1783ba45bf5fe676833 | 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.codeforces.ERound123;
import java.io.*;
import java.util.Arrays;
public class pr04 {
public static void main(String[] args)throws IOException {
Reader scan=new Reader();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t=scan.nextInt();
while (t-->0) {
int n=scan.nextInt();
int x=scan.nextInt();
int[] arr=new int[n+1];
long[] prefix=new long[n+1];
arr[1]=scan.nextInt();
prefix[1]=arr[1];
for (int i = 2; i <= n; i++) {
arr[i]=scan.nextInt();
prefix[i]=prefix[i-1]+arr[i];
}
long[] sum=new long[n+1];
Arrays.fill(sum,-Long.MIN_VALUE);
// when len 3 we have to include at least 3 we can not include maxsum less than 3 because
// when we multiply x * 3 then come problem if length is less than 3
// but we can get maxSum of contiguous string of len less than 3 after processing x *
for (int i = n; i >=0; i--) {
long sum1=Long.MIN_VALUE;
for (int j = 1; j <= (n-i)+1; j++) {
sum1=Math.max(sum1,(prefix[j+i-1]-prefix[j-1]));
}
if(i!=n){
sum[i]=Math.max(sum1,sum[i]);
sum[i]=Math.max(sum[i],sum[i+1]);
}
else sum[i]=sum1;
}
long[] ans=new long[n+1];
ans[0]=Math.max(sum[1],0);
for (int i = 1; i <= n; i++) {
ans[i]=Math.max(ans[i],ans[i-1]);
ans[i]=Math.max(ans[i],sum[i]+(x*1L*i));
}
for (int i = 0; i <= n; i++) {
bw.write(ans[i]+" ");
}
bw.newLine();
bw.flush();
}
}
//FAST READER
static class Reader {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public Reader() {
in = new BufferedInputStream(System.in, BS);
}
public Reader(String s) throws FileNotFoundException {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
public char nextChar(){
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() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+(cur < 0 ? -1*nextLong()/num : nextLong()/num);
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.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 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 | 086d013abe76b129ba32a9d88c28aa6b | 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 Q1 {
static class Pair{
long size;
long sum;
Pair(long sum, long size){
this.size = size;
this.sum = sum;
}
}
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){
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int x = Integer.parseInt(str[1]);
str = br.readLine().split(" ");
long[] arr = new long[n];
for(int i=0; i<n; i++){
arr[i] = Long.parseLong(str[i]);
}
int[] maxSums = new int[n+1];
Arrays.fill(maxSums, Integer.MIN_VALUE);
for(int i=0; i<n; i++){
int sum = 0;
for(int j=i; j<n; j++){
sum += arr[j];
maxSums[j-i+1] = Math.max(maxSums[j-i+1], sum);
}
}
long[] ans = new long[n+1];
for(int i = 0; i < n+1; i++){
long max = Long.MIN_VALUE;
for(int j=1; j <= n; j++) {
max = Math.max(max, maxSums[j] + Math.min(i, j)*x);
}
ans[i] = Math.max(0, max);
}
printArray(ans);
}
}
public static void printArray(int[] arr){
int n = arr.length;
for(int i = 0; i < n; i++){
if(i == n-1) System.out.print(arr[i]);
else System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void printArray(long[] arr){
int n = arr.length;
for(int i = 0; i < n; i++){
if(i == n-1) System.out.print(arr[i]);
else System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void printRevArray(int[] arr){
int n = arr.length;
for(int i = n-1; i >= 0; i--){
if(i == 0) System.out.print(arr[i]);
else System.out.print(arr[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 | 14c2d05e5a9759479395a0e1ce5efa1a | 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.time.*;
import static java.lang.Math.*;
@SuppressWarnings("unused")
public class C {
static boolean DEBUG = false;
static Reader fs;
static PrintWriter pw;
static void solve() throws IOException{
int n = fs.nextInt(), x = fs.nextInt();
long a[] = new long[n];
for(int i = 0 ; i < n ; i++) {
a[i] = fs.nextLong();
}
SegTree st = new SegTree(0, n - 1, a);
long map[] = new long[n + 1];
Arrays.fill(map, Long.MIN_VALUE);
map[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int k = j - i + 1;
map[k] = max(map[k], st.rangeSum(i, j));
}
}
for(int j = n - 1 ; j >= 0 ; j--) {
map[j] = max(map[j], map[j+1]);
}
for (int j = 1; j <= n; j++) {
map[j] = max(map[j-1], map[j] + (x * j));
}
for (long pp : map) {
pw.print(pp + " ");
}
pw.println();
}
static class SegTree {
int leftmost, rightmost;
long ans = 0;
SegTree lchild, rchild;
SegTree(int l, int r, long a[]) {
this.leftmost = l;
this.rightmost = r;
if (l == r) {
ans = a[l];
} else {
int mid = (l + r) / 2;
lchild = new SegTree(l, mid, a);
rchild = new SegTree(mid + 1, r, a);
}
recalc();
}
void recalc() {
if (leftmost == rightmost)
return;
ans = lchild.ans + rchild.ans;
}
void update(int x, int n_val) {
if (x == rightmost && leftmost == rightmost) {
ans = 1;
return;
}
if (x <= lchild.rightmost) {
lchild.update(x, n_val);
} else {
rchild.update(x, n_val);
}
recalc();
}
long rangeSum(int l, int r) {
if (l > rightmost || r < leftmost) {
return 0;
}
if (l <= leftmost && r >= rightmost) {
return ans;
}
return lchild.rangeSum(l, r) + rchild.rangeSum(l, r);
}
}
public static void main(String[] args) throws IOException {
if (args.length == 2) {
System.setErr(new PrintStream("D:\\program\\javaCPEclipse\\CodeForces\\src\\error.txt"));
DEBUG = true;
}
Instant start = Instant.now();
fs = new Reader();
pw = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-- > 0) {
solve();
}
Instant end = Instant.now();
if (DEBUG) {
pw.println(Duration.between(start, end));
}
pw.close();
}
static void sort(int a[]) {
ArrayList<Integer> l = new ArrayList<Integer>();
for (int x : a)
l.add(x);
Collections.sort(l);
for (int i = 0; i < a.length; i++) {
a[i] = l.get(i);
}
}
public static void print(long a, long b, long c, PrintWriter pw) {
pw.println(a + " " + b + " " + c);
return;
}
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 next() throws IOException {
byte[] buf = new byte[100009];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c <= ' ') {
if (cnt != 0) {
break;
} else {
continue;
}
}
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 int[] readArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public int[] readArray(int n, int x) throws IOException {
int[] arr = new int[n + x];
for (int i = x; i < n + x; i++) {
arr[i] = nextInt();
}
return arr;
}
public ArrayList<Integer> readList(int n) throws IOException {
ArrayList<Integer> a = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
a.add(nextInt());
}
return a;
}
public int[][] read2Array(int n, int m) throws IOException {
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;
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static int pow(int base, int index) {
if (index == 0) {
return 1;
} else if (index % 2 == 0) {
return pow(base * base, index / 2);
} else {
return base * pow(base * base, ((index - 1) / 2));
}
}
public static long pow(long base, long index) {
if (index == 0) {
return 1;
} else if (index % 2 == 0) {
return pow(base * base, index / 2);
} else {
return base * pow(base * base, ((index - 1) / 2));
}
}
} | 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 | b98aefc6d4d750c9ba92017bb665f3c0 | 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.time.*;
import static java.lang.Math.*;
@SuppressWarnings("unused")
public class C {
static boolean DEBUG = false;
static Reader fs;
static PrintWriter pw;
static void solve() {
int n = fs.nextInt(), x = fs.nextInt();
long a[] = new long[n];
for(int i = 0 ; i < n ; i++) {
a[i] = fs.nextLong();
}
SegTree st = new SegTree(0, n - 1, a);
long map[] = new long[n + 1];
Arrays.fill(map, Long.MIN_VALUE);
map[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int k = j - i + 1;
map[k] = max(map[k], st.rangeSum(i, j));
}
}
// long maxxi = Long.MIN_VALUE;
// for (long i : map)
// maxxi = max(maxxi, i);
// int i;
// for (i = 0; i<=n; i++) {
// if (maxxi == map[i])
// break;
// }
// for (int j = i; j >= 0; j--) {
// map[j] = maxxi;
// }
for(int j = n - 1 ; j >= 0 ; j--) {
map[j] = max(map[j], map[j+1]);
}
for (int j = 1; j <= n; j++) {
map[j] = max(map[j-1], map[j] + (x * j));
}
for (long pp : map) {
pw.print(pp + " ");
}
pw.println();
}
static void lg(Object o) {
if (DEBUG)
System.err.println(o);
}
static void lg(int a[]) {
if (DEBUG)
System.err.println(Arrays.toString(a));
}
static void lg(char a[]) {
if (DEBUG)
System.err.println(Arrays.toString(a));
}
static void lg(String a) {
if (DEBUG)
System.err.print(a + " -> ");
}
static <T> void lg(T a[]) {
if (DEBUG) {
System.err.print("[");
for (T x : a)
System.err.print(x + " ");
System.err.println("]");
}
}
static <T> void lg(ArrayList<T> a) {
if (DEBUG) {
System.err.print("[");
for (T x : a) {
System.err.print(x + " ");
}
System.err.println("]");
}
}
static <T, V> void lg(Map<T, V> mp) {
if (DEBUG) {
System.err.print(mp);
}
}
static class pair {
int x, y, z;
int cnt = 0;
pair(int x, int y, int a) {
this.x = x;
this.y = y;
this.z = a;
}
}
static class SegTree {
int leftmost, rightmost;
long ans = 0;
SegTree lchild, rchild;
SegTree(int l, int r, long a[]) {
this.leftmost = l;
this.rightmost = r;
if (l == r) {
ans = a[l];
} else {
int mid = (l + r) / 2;
lchild = new SegTree(l, mid, a);
rchild = new SegTree(mid + 1, r, a);
}
recalc();
}
void recalc() {
if (leftmost == rightmost)
return;
ans = lchild.ans + rchild.ans;
}
void update(int x, int n_val) {
if (x == rightmost && leftmost == rightmost) {
ans = 1;
return;
}
if (x <= lchild.rightmost) {
lchild.update(x, n_val);
} else {
rchild.update(x, n_val);
}
recalc();
}
long rangeSum(int l, int r) {
if (l > rightmost || r < leftmost) {
return 0;
}
if (l <= leftmost && r >= rightmost) {
return ans;
}
return lchild.rangeSum(l, r) + rchild.rangeSum(l, r);
}
}
public static void main(String[] args) throws IOException {
if (args.length == 2) {
System.setErr(new PrintStream("D:\\program\\javaCPEclipse\\CodeForces\\src\\error.txt"));
DEBUG = true;
}
Instant start = Instant.now();
fs = new Reader();
pw = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-- > 0) {
solve();
}
Instant end = Instant.now();
if (DEBUG) {
pw.println(Duration.between(start, end));
}
pw.close();
}
static void sort(int a[]) {
ArrayList<Integer> l = new ArrayList<Integer>();
for (int x : a)
l.add(x);
Collections.sort(l);
for (int i = 0; i < a.length; i++) {
a[i] = l.get(i);
}
}
public static void print(long a, long b, long c, PrintWriter pw) {
pw.println(a + " " + b + " " + c);
return;
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
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;
}
int[][] read2Array(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;
}
}
} | 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 | 346156edd98adb1a9a21067478498b28 | 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.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
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 testNum = in.nextInt();
solver.solve(testNum, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
for (int z = 0; z < testNumber; z++) {
int n = in.nextInt();
int x = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int[][] dp = new int[n+1][n];
int[] maxPoss = new int[n+1];
for (int k = 1; k <= n; k++) { // find max subarray sum with k elements
int sum = 0;
for (int j = 0; j < k; j++) {
sum += a[j];
}
dp[k][k-1] = sum;
maxPoss[k] = dp[k][k-1];
for (int j = k; j < n; j++) {
dp[k][j] = dp[k][j-1] - a[j-k] + a[j];
maxPoss[k] = Math.max(maxPoss[k], dp[k][j]);
}
}
for (int k = 0; k <= n; k++) {
int sol = 0;
// out.println("new: k = " + k);
for (int j = 1; j < maxPoss.length; j++) {
//out.println("old: " +sol);
sol = Math.max(sol, maxPoss[j] + Math.min(j, k)*x);
//out.println(maxPoss[j] + " " + Math.min(j, k)*x);
}
out.print(sol + " ");
}
out.println();
/*
for (int i = 0; i < dp.length; i++) {
out.println();
for (int k = 0; k < dp[0].length; k++) {
out.print(dp[i][k] + " ");
}
}
out.println();
out.println("===============");
for (int i = 0; i < maxPoss.length; i++) {
out.println(maxPoss[i]);
}*/
}
}
}
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());
}
}
}
| 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 | 048edeaa3544ef1e5e072904d1364e46 | 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 void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int numCases = Integer.parseInt(br.readLine());
for (int i = 0; i < numCases; i ++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int length = Integer.parseInt(st.nextToken());
int increase = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
long[] pSum = new long[length];
pSum[0] = Integer.parseInt(st.nextToken());
for (int j = 1; j < length; j++) {
int num = Integer.parseInt(st.nextToken());
pSum[j] = pSum[j - 1] + num;
}
long[] dp = new long[length + 1];
Arrays.fill(dp, Long.MIN_VALUE);
dp[0] = 0;
for (int x = 0; x < length; x++) {
for (int y = x; y < length; y++) {
int leftBound = x - 1;
long sum = pSum[y];
if (leftBound >= 0)
sum -= pSum[leftBound];
int segLength = y - x + 1;
dp[segLength] = Math.max(dp[segLength], sum);
}
}
long[] ret = new long[length + 1];
for (int j = 0; j <= length; j++) {
for (int idx = 0; idx <= length; idx++) {
long numAdd = Math.min(idx, j);
ret[j] = Math.max(ret[j], dp[idx] + increase * numAdd);
}
}
for (int j = 0; j <= length; j++)
pw.print(ret[j] + " ");
pw.println();
}
br.close();
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 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 | 2a228f6f0282bedba245d9dad6c64e0f | 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 Main{
static final int MOD = (int) 1e9 + 7;
static FastScanner fs = new FastScanner();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int t = fs.nextInt();
while(t-- >0){
solve();
}
out.close();
}
private static void solve(){
int n = fs.nextInt(),x=fs.nextInt();
int []a = fs.readArray(n);
int sum=0;
int []maxSum = new int[n+1],ans = new int[n+1];
Arrays.fill(maxSum,Integer.MIN_VALUE);
for (int i = 0; i < n; i++) {
sum = 0;
for (int j = i; j < n; j++) {
sum += a[j];
maxSum[j - i] = Math.max(maxSum[j - i], sum);
}
}
ans[0]= maxSubArray(a);
for (int k = 1; k <= n; k++) {
ans[k] = Math.max(ans[k - 1], 0);
for (int i = k; i <= n; i++) {
ans[k] = Math.max(ans[k], maxSum[i - 1] + k * x);
}
}
for(int j:ans)
out.print(j+" ");
out.println();
}
private static int maxSubArray(int[] a) {
int curr = 0, max = 0;
for (int ai : a) {
curr = Math.max(curr, 0) + ai;
max = Math.max(max, curr);
}
return max;
}
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[] 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 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 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 | 644daaf55bc1c67e9efd72c2fc936bc2 | 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.math.*;
import java.io.*;
public class Solution {
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 void sort(int a[]){ // int -> long
ArrayList<Integer> arr=new ArrayList<>(); // Integer -> Long
for(int i=0;i<a.length;i++)
arr.add(a[i]);
Collections.sort(arr);
for(int i=0;i<a.length;i++)
a[i]=arr.get(i);
}
private static long gcd(long a, long b){
if(b==0)return a;
return gcd(b,a%b);
}
private static long pow(long x,long y){
if(y==0)return 1;
long temp = pow(x, y/2);
if(y%2==1){
return x*temp*temp;
}
else{
return temp*temp;
}
}
static long powM(long a, long b) {
if (b == 0) return 1;
long res = pow(a, b / 2);
res = (res%mod * res%mod) % 1_000_000_007;
if (b % 2 == 1) {
res = (res%mod * a%mod) % 1_000_000_007;
}
return res%mod;
}
static int log(long n){
int res = 0;
while(n>0){
res++;
n/=2;
}
return res;
}
static int mod = (int)1e9+7;
static PrintWriter out;
static FastReader sc ;
public static void main(String[] args) throws IOException {
sc = new FastReader();
out = new PrintWriter(System.out);
// primes();
// ________________________________
// int test = 1;
StringBuilder output = new StringBuilder();
int test =sc.nextInt();
while (test-- > 0) {
//System.out.println(mdh+" "+nhc);
int n = sc.nextInt();
int a [] = new int[n];
int x =sc.nextInt();
for(int i =0;i<n;i++)
{
a[i] = sc.nextInt();
}
solver(n,a,x);
}
// solver(s);
// int n = sc.nextInt();
// out.println(solver());
// ________________________________
out.flush();
}
static class Edge
{
int u, v;
Edge(int u, int v)
{
this.u = u;
this.v = v;
}
}
static class Pair implements Comparable <Pair>{
int l, r;
Pair(int l, int r)
{
this.l = l;
this.r = r;
}
public int compareTo(Pair o)
{
return this.l-o.l;
}
}
static ArrayList<Integer>adj [] ;
static boolean [] vst ;
public static void solver( int n, int [] a , int x) {
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(temp,ans[j-i+1]);
}
}
for(int i =0;i<=n;i++)
{
int res =0;
for(int j =0;j<=n;j++)
{
res = Math.max(Math.min(i, j)*x+ans[j], res);
}
System.out.print(res+" ");
}
System.out.println();
}
static long maxSubArraySum(long a[])
{
long size = a.length;
long 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 void dfs(int cur) {
if(vst[cur]) return ;
vst[cur]=true;
for(int c:adj[cur]) dfs(c);
}
public static int Mex(int []a, int i, int j)
{
boolean [] vis = new boolean[a.length+2];
int mex =0;
for(int k=i;k<j;k++)
{
if(a[k]<vis.length)
{
vis[a[k]] = true;
}
}
while(vis[mex]==true)
{
mex++;
}
return mex;
}
public static long log2(long N)
{
// calculate log2 N indirectly
// using log() method
long result = (long)(Math.log(N) / Math.log(2));
return result;
}
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);
}
public
static void rotate(int [] arr, int s, int n)
{
int x = arr[n], i;
for (i = n; i > s; i--)
arr[i] = arr[i-1];
arr[s] = x;
// for(int j=s;j<=n;j++)
// System.out.print(arr[j]+" ");
// System.out.println();
}
static int lower_bound(int[] a , long x)
{
int i = 0;
int j = a.length-1;
//if(arr[i] > key)return -1;
if(a[j] < x)return a.length;
while(i<j)
{
int mid = (i+j)/2;
if(a[mid] == x)
{
j = mid;
}
else if(a[mid] < x)
{
i = mid+1;
}
else
j = mid-1;
}
return i;
}
int upper_bound(int [] arr , int key)
{
int i = 0;
int j = arr.length-1;
if(arr[j] <= key)return j+1;
while(i<j)
{
int mid = (i+j)/2;
if(arr[mid] <= key)
{
i = mid+1;
}
else
j = mid;
}
return i;
}
static void reverseArray(int arr[], int start,
int end)
{
while (start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
} | 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 | 86774df8746c2519532307e58050869c | 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 | /***** ---> :) Vijender Srivastava (: <--- *****/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static FastReader sc =new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=(long)32768;
static StringBuilder sb = new StringBuilder();
/* start */
public static void main(String [] args)
{
int testcases = 1;
testcases = i();
// calc();
while(testcases-->0)
{
solve();
}
out.flush();
out.close();
}
static void solve()
{
int n = i(), x = i();
long arr[] = inputL(n);
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);
length++;
}
}
for (int i=0; i<=n; i++) {
long best = Long.MIN_VALUE;
for (int length=0; length<=n; length++) {
best = max(best, dp[length] + (min(length, i) * x));
}
p(best+" ");
}
pl();
}
/* end */
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;
}
}
// print code start
static void p(Object o)
{
out.print(o);
}
static void pl(Object o)
{
out.println(o);
}
static void pl()
{
out.println("");
}
// print code end //
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static char[] inputC()
{
String s = sc.nextLine();
return s.toCharArray();
}
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 long[] putL(long a[]) {
long A[]=new long[a.length];
for(int i=0;i<a.length;i++) {
A[i]=a[i];
}
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 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 int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
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 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 long mod(long x) {
return ((x%mod + mod)%mod);
}
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 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 long[] sort(long a[]) {
ArrayList<Long> arr = new ArrayList<>();
for(long i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
static int[] sort(int a[])
{
ArrayList<Integer> arr = new ArrayList<>();
for(Integer i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
//pair class
private static class Pair implements Comparable<Pair> {
long first, second;
public Pair(long f, long s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair p) {
if (first < p.first)
return 1;
else if (first > p.first)
return -1;
else {
if (second > p.second)
return 1;
else if (second < p.second)
return -1;
else
return 0;
}
}
}
// segment t start
static long seg[] ;
static void build(long a[], int v, int tl, int tr) {
if (tl == tr) {
seg[v] = a[tl];
} else {
int tm = (tl + tr) / 2;
build(a, v*2, tl, tm);
build(a, v*2+1, tm+1, tr);
seg[v] = Math.min(seg[v*2] , seg[v*2+1]);
}
}
static long query(int v, int tl, int tr, int l, int r) {
if (l > r || tr < tl)
return Integer.MAX_VALUE;
if (l == tl && r == tr) {
return seg[v];
}
int tm = (tl + tr) / 2;
return (query(v*2, tl, tm, l, min(r, tm))+ query(v*2+1, tm+1, tr, max(l, tm+1), r));
}
static void update(int v, int tl, int tr, int pos, long new_val) {
if (tl == tr) {
seg[v] = new_val;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v*2, tl, tm, pos, new_val);
else
update(v*2+1, tm+1, tr, pos, new_val);
seg[v] = Math.min(seg[v*2] , seg[v*2+1]);
}
}
// segment t end //
} | 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 | 90a7c3312b7df2db1476554b1a1e088f | 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.Arrays;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int T = in.nextInt();
while (T-- > 0) {
solveOne(in, out);
}
}
private void solveOne(Scanner in, PrintWriter out) {
int N = in.nextInt();
int X = in.nextInt();
int A[] = L.readIntArray(N, in);
long sums[] = new long[N + 1];
Arrays.fill(sums, Long.MIN_VALUE);
sums[0] = 0;
for (int i = 0; i < N; i++) {
long sum = 0;
for (int j = i; j < N; j++) {
sum += A[j];
int valuesCnt = j - i + 1;
sums[valuesCnt] = Math.max(sums[valuesCnt], sum);
}
}
for (int i = 0; i <= N; i++) {
long bst = 0;
for (int j = 0; j <= N; j++) {
bst = Math.max(bst, sums[j] + Math.min(j, i) * (long) X);
}
out.print(bst + " ");
}
out.println();
}
}
static class L {
public static int[] readIntArray(int size, Scanner in) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = in.nextInt();
}
return array;
}
}
}
| 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 | a8139786c60b6232d16eb0092e83ba52 | 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 {
static StringBuilder sb;
static int n;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter wr = new PrintWriter(System.out);
sb = new StringBuilder();
long tc = Long.parseLong(br.readLine());
while(tc-->0){
String inp = br.readLine();
String split[] = inp.split(" ");
int n = Integer.parseInt(split[0]);
long x = Long.parseLong(split[1]);
long arr[] = new long[n];
split = br.readLine().split(" ");
for(int i =0;i<n;i++) arr[i] = Long.parseLong(split[i]);
long best[] = new long[n+1];
Arrays.fill(best,Long.MIN_VALUE);
for(int i =0;i<arr.length;i++){
long sum = 0;
for(int j =i;j<arr.length;j++){
sum+=arr[j];
best[j-i+1] = Math.max(best[j-i+1],sum);
}
}
long suffixSum[] = new long[n+1];
for(int i = n;i>=0;i--){
suffixSum[i] = best[i];
if(i<n)
suffixSum[i] = Math.max(suffixSum[i],suffixSum[i+1]);
}
long max = 0;
for(int k =0;k<=n;k++){
max = Math.max(max,suffixSum[k]+k*x);
sb.append(max).append(" ");
}
sb.append("\n");
}
wr.println(sb);
wr.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 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 | 849f94023da569969e1f906a857feefc | 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 {
static StringBuilder sb;
static int n;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter wr = new PrintWriter(System.out);
sb = new StringBuilder();
long tc = Long.parseLong(br.readLine());
while(tc-->0){
String inp = br.readLine();
String split[] = inp.split(" ");
int n = Integer.parseInt(split[0]);
long x = Long.parseLong(split[1]);
PriorityQueue<Pair> pq = new PriorityQueue();
split = br.readLine().split(" ");
long arr[] = new long[n];
HashMap<Integer,Long> map = new HashMap();
for(int i =0;i<n;i++) arr[i] = Long.parseLong(split[i]);
long maxSum = 0;
for(int i =0;i<n;i++){
long sum = 0;
for(int j=i;j<n;j++){
sum+=arr[j];
// pq.add(new Pair(j-i+1,sum));
int len = j-i+1;
if(map.containsKey(len)){
long prev = map.get(len);
map.put(len,Math.max(prev,sum));
}else map.put(len,sum);
maxSum = Math.max(maxSum,sum);
}
}
for(int key : map.keySet()){
int len = key;
long sum = map.get(key);
pq.add(new Pair(len,sum));
}
long currMax = maxSum;
for(int i =0;i<=n;i++){
while(pq.size()>0&&pq.peek().len<i){
Pair pair = pq.remove();
currMax = Math.max(currMax,pair.sum+(((long)pair.len)*x));
}
if(pq.size()>0){
currMax = Math.max(currMax,pq.peek().sum+((long)(i)*x));
}
sb.append(currMax).append(" ");
}
sb.append("\n");
}
wr.println(sb);
wr.close();
}
}
class Pair implements Comparable<Pair>{
int len;
long sum;
public Pair(int len , long sum){
this.len = len;
this.sum = sum;
}
public int compareTo(Pair other){
if(this.sum>=other.sum) return -1;
return 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 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 | 314b613b59bf51f341dda44091dc4ddf | 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 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 | 0eea70d7ef88ec869fcf7e32da004ccf | 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 on: 8:20:43 PM | 22-Feb-2022
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
public class Main {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static void solve() {
int caseNo = 1;
for (int T = sc.nextInt(); T > 0; T--, caseNo++) {
solveIt(caseNo);
}
}
// Solution
static void solveIt(int t) {
int n = sc.nextInt();
int x = sc.nextInt();
long arr[] = sc.readLongArray(n);
long dp[] = new long[n + 1];
Arrays.fill(dp, Long.MIN_VALUE);
for (int i = 0; i < n; i++) {
long val = 0;
for (int j = i; j < n; j++) {
val += arr[j];
dp[j - i + 1] = Math.max(dp[j - i + 1], val);
}
}
for (int i = 0; i <= n; i++) {
long res = 0;
for (int j = 1; j <= n; j++) {
res = Math.max(res, dp[j] + (Math.min(i, j) * x));
}
System.out.print(res + " ");
}
System.out.println();
}
static void revArray(int a[], int i, int j) {
while (i <= j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}
static boolean isSorted(int a[], int s, int e, boolean strict) {
for (int i = s + 1; i <= e; i++) {
if (strict && a[i - 1] >= a[i])
return false;
if (!strict && a[i - 1] > a[i])
return false;
}
return true;
}
static class DSU {
int rank[];
int parent[];
DSU(int n) {
rank = new int[n + 1];
parent = new int[n + 1];
for (int i = 1; i <= n; i++) {
parent[i] = i;
}
}
int findParent(int node) {
if (parent[node] == node)
return node;
return parent[node] = findParent(parent[node]);
}
boolean union(int x, int y) {
int px = findParent(x);
int py = findParent(y);
if (px == py)
return false;
if (rank[px] < rank[py]) {
parent[px] = py;
} else if (rank[px] > rank[py]) {
parent[py] = px;
} else {
parent[px] = py;
rank[py]++;
}
return true;
}
}
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[] seiveOfEratosthenes(int n) {
boolean[] isPrime = new boolean[n + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n; i++) {
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
return isPrime;
}
public static int[] seiveOnlyPrimes(int n) {
if (n <= 32) {
int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
for (int i = 0; i < primes.length; i++) {
if (n < primes[i]) {
return Arrays.copyOf(primes, i);
}
}
return primes;
}
int u = n + 32;
double lu = Math.log(u);
int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)];
ret[0] = 2;
int pos = 1;
int[] isp = new int[(n + 1) / 32 / 2 + 1];
int sup = (n + 1) / 32 / 2 + 1;
int[] tprimes = { 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
for (int tp : tprimes) {
ret[pos++] = tp;
int[] ptn = new int[tp];
for (int i = (tp - 3) / 2; i < tp << 5; i += tp)
ptn[i >> 5] |= 1 << (i & 31);
for (int i = 0; i < tp; i++) {
for (int j = i; j < sup; j += tp)
isp[j] |= ptn[i];
}
}
int[] magic = { 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17,
9, 6, 16, 5, 15, 14 };
int h = n / 2;
for (int i = 0; i < sup; i++) {
for (int j = ~isp[i]; j != 0; j &= j - 1) {
int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27];
int p = 2 * pp + 3;
if (p > n)
break;
ret[pos++] = p;
for (int q = pp; q <= h; q += p)
isp[q >> 5] |= 1 << (q & 31);
}
}
return Arrays.copyOf(ret, pos);
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean isPrime(long n) {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
public static void main(String[] args) throws Exception {
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
solve();
out.flush();
long G = System.currentTimeMillis();
sc.tr(G - S + "ms");
}
static class sc {
private static boolean endOfFile() {
if (bufferLength == -1)
return true;
int lptr = ptrbuf;
while (lptr < bufferLength)
if (!isThisTheSpaceCharacter(inputBufffferBigBoi[lptr++]))
return false;
try {
is.mark(1000);
while (true) {
int b = is.read();
if (b == -1) {
is.reset();
return true;
} else if (!isThisTheSpaceCharacter(b)) {
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inputBufffferBigBoi = new byte[1024];
static int bufferLength = 0, ptrbuf = 0;
private static int justReadTheByte() {
if (bufferLength == -1)
throw new InputMismatchException();
if (ptrbuf >= bufferLength) {
ptrbuf = 0;
try {
bufferLength = is.read(inputBufffferBigBoi);
} catch (IOException e) {
throw new InputMismatchException();
}
if (bufferLength <= 0)
return -1;
}
return inputBufffferBigBoi[ptrbuf++];
}
private static boolean isThisTheSpaceCharacter(int c) {
return !(c >= 33 && c <= 126);
}
private static int skipItBishhhhhhh() {
int b;
while ((b = justReadTheByte()) != -1 && isThisTheSpaceCharacter(b))
;
return b;
}
private static double nextDouble() {
return Double.parseDouble(next());
}
private static char nextChar() {
return (char) skipItBishhhhhhh();
}
private static String next() {
int b = skipItBishhhhhhh();
StringBuilder sb = new StringBuilder();
while (!(isThisTheSpaceCharacter(b))) {
sb.appendCodePoint(b);
b = justReadTheByte();
}
return sb.toString();
}
private static char[] readCharArray(int n) {
char[] buf = new char[n];
int b = skipItBishhhhhhh(), p = 0;
while (p < n && !(isThisTheSpaceCharacter(b))) {
buf[p++] = (char) b;
b = justReadTheByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] readCharMatrix(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = readCharArray(m);
return map;
}
private static long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
private static int nextInt() {
int num = 0, b;
boolean minus = false;
while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = justReadTheByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = justReadTheByte();
}
}
private static long nextLong() {
long num = 0;
int b;
boolean minus = false;
while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = justReadTheByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = justReadTheByte();
}
}
private static void tr(Object... o) {
if (INPUT.length() != 0)
System.out.println(Arrays.deepToString(o));
}
}
}
| 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 | c44d3140b30b573434ddd4b4e568f6b5 | 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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Test{
static FastReader scan;
static void solve(){
int n=scan.nextInt();
int k=scan.nextInt();
int[]arr=new int[n];
Map<Integer,Integer>map=new HashMap<>();
for(int i=0;i<n;i++){
arr[i]=scan.nextInt();
}
for(int i=0;i<n;i++){
int sum=0;
for(int j=i;j<n;j++){
sum+=arr[j];
int max=Math.max(map.getOrDefault(j-i+1,Integer.MIN_VALUE),sum);
map.put(j-i+1,max);
}
}
for(int i=0;i<=n;i++){
int max=0;
for(int j=1;j<=n;j++){
max=Math.max(max,map.get(j)+k*Math.min(i,j));
}
System.out.print(max+" ");
}
System.out.println();
}
public static void main (String[] args) throws java.lang.Exception{
scan=new FastReader();
int t=scan.nextInt();
while(t-->0){
solve();
}
}
static class Pair implements Comparable<Pair>{
int row;
int col;
int dis;
Pair(int x,int y,int z){
this.row=x;
this.col=y;
this.dis=z;
}
@Override
public int compareTo(Pair x){
return this.dis-x.dis;
}
public String toString(){
return "["+row+" "+col+" "+dis+"]";
}
}
static boolean sq(int i){
int temp=(int)Math.sqrt(i);
return temp*temp==i;
}
static boolean cb(int i){
int temp=(int)Math.cbrt(i);
return temp*temp*temp==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;
}
}
static void printLong(long []arr){
for(long x:arr)System.out.print(x+" ");
}
static void printInt(int []arr){
for(int x:arr)System.out.print(x+" ");
}
static void scanInt(int []arr){
for(int i=0;i<arr.length;i++){
arr[i]=scan.nextInt();
}
}
static void scanLong(long []arr){
for(int i=0;i<arr.length;i++){
arr[i]=scan.nextLong();
}
}
static long gcd(long a, long b){
if (b == 0)
return a;
return gcd(b, a % b);
}
static long power(long x, long y, long mod){
long 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;
}
static long add(long a,long b,long mod){
a = a % mod;
b = b % mod;
return (((a + b) % mod) + mod) % mod;
}
static long sub(long a, long b,long mod){
a = a % mod;
b = b % mod;
return (((a - b) % mod) + mod) % mod;
}
static long mul(long a, long b,long mod){
a = a % mod;
b = b % mod;
return (((a * b) % mod) + mod) % mod;
}
static long mminvprime(long a, long b,long mod) {
return power(a, b - 2,mod);
}
} | 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 | 079f756fb2e9e82218fb3db14070392f | 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();
for(int test=1;test<=t;test++){
int n= sc.nextInt();
int x=sc.nextInt();
int[] arr=new int[n];
boolean paisi=false;
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
if(arr[i]<0){
paisi=true;
}
if(i!=0){
arr[i]+=arr[i-1];
}
}
if(!paisi){
for(int i=0;i<=n;i++){
System.out.print(arr[n-1]+i*x+" ");
}
System.out.println();
continue;
}
int[] v=new int[n+1];
int ans1=0;
for(int i=1;i<=n;i++){
int ans=-999999999;
for(int j=i-1;j<n;j++){
if(j-i<0){
ans=Math.max(ans,arr[j]);
}else{
ans=Math.max(ans,arr[j]-arr[j-i]);
}
}
v[i]=ans;
ans1=Math.max(ans1,ans);
}
v[0]=ans1;
for(int i=0;i<=n;i++){
int rans=0;
for(int j=0;j<=n;j++){
rans=Math.max(rans,Math.min(i,j)*x+v[j]);
}
System.out.print(rans+" ");
}
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 | 7f81ed5180f9440963003c3548d7a4a4 | 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;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 0; tc < t; ++tc) {
int n = sc.nextInt();
int x = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < a.length; ++i) {
a[i] = sc.nextInt();
}
System.out.println(solve(a, x));
}
sc.close();
}
static String solve(int[] a, int x) {
int[] prefixSums = new int[a.length];
for (int i = 0; i < prefixSums.length; ++i) {
prefixSums[i] = ((i == 0) ? 0 : prefixSums[i - 1]) + a[i];
}
int[] result = new int[a.length + 1];
int maxSum = Integer.MIN_VALUE;
for (int i = result.length - 1; i >= 1; --i) {
for (int beginIndex = 0; beginIndex + i - 1 < a.length; ++beginIndex) {
int endIndex = beginIndex + i - 1;
maxSum = Math.max(maxSum, computeRangeSum(prefixSums, beginIndex, endIndex));
}
result[i] = maxSum + i * x;
}
result[0] = Math.max(0, maxSum);
for (int i = 1; i < result.length; ++i) {
result[i] = Math.max(result[i - 1], result[i]);
}
return Arrays.stream(result).mapToObj(String::valueOf).collect(Collectors.joining(" "));
}
static int computeRangeSum(int[] prefixSums, int beginIndex, int endIndex) {
return prefixSums[endIndex] - ((beginIndex == 0) ? 0 : prefixSums[beginIndex - 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 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 | e2b8a22ae0a1ca8f685136bb89c22ad1 | 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 boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private FastWriter wr;
private Reader rd;
public final int MOD = 1000000007;
/************************************************** FAST INPUT IMPLEMENTATION *********************************************/
class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
public Reader(String path) throws FileNotFoundException {
br = new BufferedReader(
new InputStreamReader(new FileInputStream(path)));
}
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 int ni() throws IOException {
return rd.nextInt();
}
public long nl() throws IOException {
return rd.nextLong();
}
public void yOrn(boolean flag){
if(flag){
wr.println("YES");
}else {
wr.println("NO");
}
}
char nc() throws IOException {
return rd.next().charAt(0);
}
public String ns() throws IOException {
return rd.nextLine();
}
public Double nd() throws IOException {
return rd.nextDouble();
}
public int[] nai(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
public long[] nal(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
/************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map) write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
/********************************************************* USEFUL CODE **************************************************/
boolean[] SAPrimeGenerator(int n) {
// TC-N*LOG(LOG N)
//Create Prime Marking Array and fill it with true value
boolean[] primeMarker = new boolean[n + 1];
Arrays.fill(primeMarker, true);
primeMarker[0] = false;
primeMarker[1] = false;
for (int i = 2; i <= n; i++) {
if (primeMarker[i]) {
// we start from 2*i because i*1 must be prime
for (int j = 2 * i; j <= n; j += i) {
primeMarker[j] = false;
}
}
}
return primeMarker;
}
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
class Pair<F, S> {
private F first;
private S second;
Pair(F first, S second) {
this.first = first;
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<F, S> pair = (Pair<F, S>) o;
return first == pair.first && second == pair.second;
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
}
class PairThree<F, S, X> {
private F first;
private S second;
private X third;
PairThree(F first, S second,X third) {
this.first = first;
this.second = second;
this.third=third;
}
public F getFirst() {
return first;
}
public void setFirst(F first) {
this.first = first;
}
public S getSecond() {
return second;
}
public void setSecond(S second) {
this.second = second;
}
public X getThird() {
return third;
}
public void setThird(X third) {
this.third = third;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PairThree<?, ?, ?> pair = (PairThree<?, ?, ?>) o;
return first.equals(pair.first) && second.equals(pair.second) && third.equals(pair.third);
}
@Override
public int hashCode() {
return Objects.hash(first, second, third);
}
}
public static void main(String[] args) throws IOException {
new Main().run();
}
public void run() throws IOException {
if (oj) {
rd = new Reader();
wr = new FastWriter(System.out);
} else {
File input = new File("input.txt");
File output = new File("output.txt");
if (input.exists() && output.exists()) {
rd = new Reader(input.getPath());
wr = new FastWriter(output.getPath());
} else {
rd = new Reader();
wr = new FastWriter(System.out);
oj = true;
}
}
long s = System.currentTimeMillis();
solve();
wr.flush();
tr(System.currentTimeMillis() - s + "ms");
}
/***************************************************************************************************************************
*********************************************************** MAIN CODE ******************************************************
****************************************************************************************************************************/
boolean[] sieve;
public void solve() throws IOException {
int t = 1;
t = ni();
while (t-- > 0) {
go();
}
}
/********************************************************* MAIN LOGIC HERE ****************************************************/
long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lower_bound(int array[], int key)
{
int low = 0, high = array.length;
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;
}
public boolean getHealth(long[] arr,long k,long h){
long health=0;
health+=k;
for(int i=1;i<arr.length;i++){
if(arr[i]-arr[i-1]<=k){
health+=arr[i]-arr[i-1];
}else {
health+=k;
}
}
return health>=h;
}
public int Substr(String s2, String s1){
int counter = 0; //pointing s2
int i = 0;
for(;i<s1.length();i++){
if(counter==s2.length())
break;
if(s2.charAt(counter)==s1.charAt(i)){
counter++;
}else{
//Special case where character preceding the i'th character is duplicate
if(counter>0){
i -= counter;
}
counter = 0;
}
}
return counter < s2.length()?-1:i-counter;
}
public void go() throws IOException {
int n=ni();
int x=ni();
int[] arr=nai(n);
int[] maxSum=new int[n];
for(int i=0;i<n;i++){
int start=0,end=i;
int sum=0;
for(int j=start;j<=end;j++){
sum+=arr[j];
}
int maxSumVal=sum;
while (end<n-1){
sum-=arr[start];
start++;
end++;
sum+=arr[end];
maxSumVal=Math.max(maxSumVal,sum);
}
maxSum[i]=maxSumVal;
}
int ans=0;
for(int k=0;k<=n;k++){
int max=0;
for(int i=1;i<=n;i++){
int temp=maxSum[i-1]+(Math.min(i,k)*x);
max=Math.max(temp,max);
}
wr.print(max+" ");
}
wr.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 | f5a35efde20c1558fef99739c91b2fdc | 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 Hello {
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 arr[] = new int[N];
for (int index = 0; index < N; index++) {
arr[index] = sc.nextInt();
}
int max[] = new int[N + 1];
Arrays.fill(max, Integer.MIN_VALUE);
max[0] = 0;
for (int left = 0; left < N; left++) {
int sum = 0;
for (int right = left; right < N; right++) {
sum += arr[right];
max[right - left + 1] = Math.max(max[right - left + 1], sum);
}
}
for (int index = 0; index <= N; index++) {
int val = 0;
for (int ind = 0; ind <= N; ind++) {
val = Math.max(val, max[ind] + Math.min(index, ind) * X);
}
System.out.print(val + " ");
}
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 | ee67b23ebc1cb77b96052a563b7f2997 | 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 Hello {
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 arr[] = new int[N];
for (int index = 0; index < N; index++) {
arr[index] = sc.nextInt();
}
int max[] = new int[N + 1];
Arrays.fill(max, Integer.MIN_VALUE);
max[0] = 0;
for (int left = 0; left < N; left++) {
int sum = 0;
for (int right = left; right < N; right++) {
sum += arr[right];
max[right - left + 1] = Math.max(max[right - left + 1], sum);
}
}
int result[] = new int[N + 1];
for (int index = 0; index <= N; index++) {
int val = 0;
for (int ind = 0; ind <= N; ind++) {
val = Math.max(val, max[ind] + Math.min(index, ind) * X);
}
result[index] = val;
System.out.print(val + " ");
}
/*for (int index = 0; index <= N; index++) {
System.out.print(result[index] + " ");
}*/
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 | 63542a453fda104da4fccb2f4b6e9675 | 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 Hello {
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 arr[] = new int[N];
for (int index = 0; index < N; index++) {
arr[index] = sc.nextInt();
}
int max[] = new int[N + 1];
Arrays.fill(max, Integer.MIN_VALUE);
max[0] = 0;
for (int left = 0; left < N; left++) {
int sum = 0;
for (int right = left; right < N; right++) {
sum += arr[right];
max[right - left + 1] = Math.max(max[right - left + 1], sum);
}
}
int result[] = new int[N + 1];
for (int index = 0; index <= N; index++) {
int val = 0;
for (int ind = 0; ind <= N; ind++) {
val = Math.max(val, max[ind] + Math.min(index, ind) * X);
}
result[index] = val;
}
for (int index = 0; index <= N; index++) {
System.out.print(result[index] + " ");
}
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 | bf7c7a201cf264d47a5c7215d9ca0e54 | 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.*;
/**
*
* @author eslam
*/
public class IncreaseSubarraySums {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() throws FileNotFoundException {
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 static void main(String[] args) throws IOException {
FastReader input = new FastReader();
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
int t = input.nextInt();
while(t-->0){
int n = input.nextInt();
long x = input.nextInt();
long sum[] = new long[n];
Arrays.fill(sum, Integer.MIN_VALUE);
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
}
for (int i = 0; i < n; i++) {
long s = a[i];
sum[0] = Math.max(sum[0], s);
for (int j = i+1; j < n; j++) {
s+=a[j];
sum[j-i] = Math.max(sum[j-i], s);
}
}
for (int i = 0; i < n+1; i++) {
long ans = Integer.MIN_VALUE;
for (int j = 0; j < n; j++) {
ans =Math.max(sum[j]+Math.min(i, j+1)*x, ans);
}
ans = Math.max(ans, 0);
log.write(ans+" ");
}
log.write("\n");
}
log.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 | 68a78de02e6a0bcb8fb5ca2a4246a9e2 | 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 {
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main (String[] args) throws java.lang.Exception {
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int val = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i <n; i++){
arr[i] = sc.nextInt();
}
int max[] = new int[n+1];
for(int i = 1; i <= n; i++){
max[i] = Integer.MIN_VALUE;
}
for(int i = 0; i < n; i++){
int curSum = 0;
for(int j = i; j< n; j++){
curSum += arr[j];
max[j-i+1] = Math.max(max[j-i+1],curSum);
}
}
for(int i = 0; i <= n; i++){
int ans = 0 ;
for(int j = 1; j <= n; j++){
int mul = Math.min(j,i);
ans = Math.max(max[j]+mul*val,ans);
//out.println(max[j]);
//out.println(max[j]+mul*val);
}
out.print(ans+" ");
}
out.println();
}
// discipline is doing what needs to be done even if you don't want to do it.
out.flush();
}
}
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 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 | e95c3956529fd0f3dd9b57036a428947 | 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 | // Working program using Reader Class
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
public class Main
{
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new 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[1000000]; // 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();
}
}
static final int MAXN = 100001;
static int spf[] = new int[MAXN];
public static void sieve()
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
public static Vector<Integer> getFactorization(int x)
{
Vector<Integer> ret = new Vector<>();
while (x != 1)
{
ret.add(spf[x]);
x = x / spf[x];
}
return ret;
}
public static void main(String[] args) throws IOException
{
Reader sc=new Reader();
int t=sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int x = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
int[] max = new int[n+1];
for(int i = 0; i <= n; i++) max[i] = Integer.MIN_VALUE;
for(int i = 0; i < n; i++) {
int sum = 0;
for(int j = i; j < n; j++) {
sum += arr[j];
max[j-i+1] = Math.max(max[j-i+1], sum);
}
}
int[] ans = new int[n+1];
for(int k = 0; k <= n; k++) {
int temp = 0;
for(int l = 0; l <= n; l++) {
temp = Math.max(temp, max[l] + Math.min(k, l) * x);
}
ans[k] = temp;
}
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 | c509b85ca226b3ab0e1a03b91b113bab | 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 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 void sort(int a[]){ // int -> long
ArrayList<Integer> arr=new ArrayList<>(); // Integer -> Long
for(int i=0;i<a.length;i++)
arr.add(a[i]);
Collections.sort(arr);
for(int i=0;i<a.length;i++)
a[i]=arr.get(i);
}
private static long gcd(long a, long b){
if(b==0)return a;
return gcd(b,a%b);
}
private static long pow(long x,long y){
if(y==0)return 1;
long temp = pow(x, y/2);
if(y%2==1){
return x*temp*temp;
}
else{
return temp*temp;
}
}
static int log(long n){
if(n<=1)return 0;
long temp = n;
int res = 0;
while(n>1){
res++;
n/=2;
}
return (1<<res)==temp?res:res+1;
}
static int mod = (int)1e9+7;
static int INF = Integer.MAX_VALUE;
static PrintWriter out;
static FastReader sc ;
public static void main(String[] args) throws IOException {
sc = new FastReader();
out = new PrintWriter(System.out);
// primes();
// ================================ //
int test = sc.nextInt();
while (test-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
int[]arr =new int[n];
for(int i=0;i<n;i++)arr[i] = sc.nextInt();
solver(arr, n, k);
}
// ================================ //
// int n = sc.nextInt();
// solver();
// ================================ //
out.flush();
}
public static void solver(int[]arr, int n, int k) {
int[]pre =new int[n+1];
for(int i=0;i<n;i++){
pre[i+1] = pre[i]+arr[i];
}
int[]dp = new int[n+1];
Arrays.fill(dp, Integer.MIN_VALUE);
for(int j=1;j<=n;j++){
for(int i=j;i<=n;i++){
dp[j] = Math.max(dp[j], pre[i]-pre[i-j]);
}
}
for(int i=n-1;i>=1;i--)dp[i] = Math.max(dp[i], dp[i+1]);
// Console.log( dp);
int res = 0;
int len = 0;
for(int j=0;j<=n;j++){
int cur = 0;
if(j==0){
int start = 0;
for(int i=0;i<n;i++){
cur+=arr[i];
if(cur<0){
cur = 0;
start = i+1;
}
if(cur>=res){
if(cur==res)
len = Math.max(len, i-start+1);
else
len = i-start+1;
}
res = Math.max(res, cur);
}
out.print( res+" ");
}
else{
if(len>=j){
res = res+k;
out.print( res+" ");
continue;
}
res = Math.max(res, dp[j]+j*k);
out.print( res+" ");
}
}
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 | b5375aec58890ce7880691e09dedbf22 | 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 edu_123;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class c{
public static void main(String[] args){
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int x=sc.nextInt();
int a[]=sc.fastArray(n);
int prefix[]=new int [n+1];prefix[1]=a[0];
prefix[0]=0;
for(int i=1;i<=n;i++)
prefix[i]=prefix[i-1]+a[i-1];
int max_sum[]=new int [n+1];
Arrays.fill(max_sum, Integer.MIN_VALUE);
for(int l=1;l<=n;l++) {
for(int i=l;i<=n;i++){
int sum=prefix[i]-prefix[i-l];
max_sum[l]=Math.max(max_sum[l], sum);
}
}
int max=Math.max(0,max_sum[n]);
for(int i=n-1;i>=1;i--) {
max_sum[i]=Math.max(max_sum[i], max_sum[i+1]);
max=Math.max(max,max_sum[i]);
}
System.out.print(max+" ");
for(int i=1;i<=n;i++) {
max=Math.max(max, max_sum[i]+(i*x));
System.out.print(max+" ");
}
System.out.println();
}
}
static int find_sub(int a[],int n) {
int currmax=a[0];
int max=0;
for(int j=1;j<n;j++){
currmax+=a[j];
if(a[j]>currmax) currmax=a[j];
if(max<currmax) max=currmax;
}
// ans.add(max);
return max;
}
static class pair {
int x;int y;
pair(int x,int y){
this.x=x;
this.y=y;
}
}
static void sort(int a[]) {
ArrayList<Integer> aa= new ArrayList<>();
for(int i:a)aa.add(i);
Collections.sort(aa);
int j=0;
for(int i:aa)a[j++]=i;
}
static ArrayList<Integer> primeFac(int n){
ArrayList<Integer>ans = new ArrayList<Integer>();
int lp[]=new int [n+1];
Arrays.fill(lp, 0); //0-prime
for(int i=2;i<=n;i++) {
if(lp[i]==0) {
for(int j=i;j<=n;j+=i) {
if(lp[j]==0) lp[j]=i;
}
}
}
int fac=n;
while(fac>1) {
ans.add(lp[fac]);
fac=fac/lp[fac];
}
print(ans);
return ans;
}
static ArrayList<Long> prime_in_given_range(long l,long r){
ArrayList<Long> ans= new ArrayList<>();
int n=(int)Math.sqrt(r)+1;
int prime[]=sieve_of_Eratosthenes(n);
long res[]=new long [(int)(r-l)+1];
for(int i=0;i<=r-l;i++) {
res[i]=i+l;
}
for(int i=0;i<prime.length;i++) {
if(prime[i]==1) {
System.out.println(2);
for(int j=Math.max((int)i*i, (int)(l+i-1)/i*i);j<=r;j+=i) {
res[j-(int)l]=0;
}
}
}
for(long i:res) if(i!=0)ans.add(i);
return ans;
}
static int [] sieve_of_Eratosthenes(int n) {
int prime[]=new int [n];
Arrays.fill(prime, 1); // 1-prime | 0-not prime
prime[0]=prime[1]=0;
for(int i=2;i<n;i++) {
if(prime[i]==1) {
for(int j=i*i;j<n;j+=i) {
prime[j]=0;
}
}
}
return prime;
}
static long binpow(long a,long b) {
long res=1;
if(b==0)return a;
if(a==0)return 1;
while(b>0) {
if((b&1)==1) {
res*=a;
}
a*=a;
b>>=1;
}
return res;
}
static void print(int a[]) {
System.out.println(a.length);
for(int i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long a[]) {
System.out.println(a.length);
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(ArrayList<Integer> a) {
System.out.println(a.size());
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
long [] fastArray(int n,long x) {
long a[]=new long [n];
for(int i=0;i<n;i++) {
a[i]=nextLong();
}
return a;
}
int [] fastArray(int n) {
int a[]=new int [n];
for(int i=0;i<n;i++) {
a[i]=nextInt();
}
return a;
}
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 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 | 1a0fbabbf80721e994871934690512eb | 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 Solution {
static int M = 1_000_000_007;
static Random rng = new Random();
private static int[] testCase(int n, int x, int[] a) {
int[] ans = new int[n + 1], maxSubArrayWithSize = new int[n];
int sum;
Arrays.fill(maxSubArrayWithSize, Integer.MIN_VALUE);
for (int i = 0; i < n; i++) {
sum = 0;
for (int j = i; j < n; j++) {
sum += a[j];
maxSubArrayWithSize[j - i] = Math.max(maxSubArrayWithSize[j - i], sum);
}
}
ans[0] = maxSubArray(a);
for (int k = 1; k <= n; k++) {
ans[k] = Math.max(ans[k - 1], 0);
for (int i = k; i <= n; i++) {
ans[k] = Math.max(ans[k], maxSubArrayWithSize[i - 1] + k * x);
}
}
return ans;
}
private static int maxSubArray(int[] a) {
int curr = 0, max = 0;
for (int ai : a) {
curr = Math.max(curr, 0) + ai;
max = Math.max(max, curr);
}
return max;
}
public static void main(String[] args) {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
//in.nextLine();
for (int tt = 1; tt <= t; ++tt) {
int n = in.nextInt(), x = in.nextInt();
int[] a = in.readArray(n);
for (int an : testCase(n, x, a)) {
out.print(an);
out.print(' ');
}
out.println();
}
out.close();
}
private 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();
}
boolean hasNext() {
return st.hasMoreTokens();
}
char[] readCharArray(int n) {
char[] arr = new char[n];
try {
br.read(arr);
br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return arr;
}
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());
}
}
private static void sort(int[] arr) {
int temp, idx;
for (int i = arr.length - 1; i > 0; i--) {
idx = rng.nextInt(i + 1);
temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
private static void sort(long[] arr) {
long temp;
int idx;
for (int i = arr.length - 1; i > 0; i--) {
idx = rng.nextInt(i + 1);
temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
private static <T> void sort(T[] arr) {
T temp;
int idx;
for (int i = arr.length - 1; i > 0; i--) {
idx = rng.nextInt(i + 1);
temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
private static <T> void sort(T[] arr, Comparator<? super T> cmp) {
T temp;
int idx;
for (int i = arr.length - 1; i > 0; i--) {
idx = rng.nextInt(i + 1);
temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr, cmp);
}
private static <T extends Comparable<? super T>> void sort(List<T> list) {
T temp;
int idx;
for (int i = list.size() - 1; i > 0; i--) {
idx = rng.nextInt(i + 1);
temp = list.get(i);
list.set(i, list.get(idx));
list.set(idx, temp);
}
Collections.sort(list);
}
private static <T> void sort(List<T> list, Comparator<? super T> cmp) {
T temp;
int idx;
for (int i = list.size() - 1; i > 0; i--) {
idx = rng.nextInt(i + 1);
temp = list.get(i);
list.set(i, list.get(idx));
list.set(idx, temp);
}
Collections.sort(list, cmp);
}
class DSU {
int[] parent, rank;
public DSU(int n) {
parent = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
public int find(int i) {
if (parent[i] == i) {
return i;
} else {
int res = find(parent[i]);
parent[i] = res;
return res;
}
}
public boolean isSameSet(int i, int j) {
return find(i) == find(j);
}
public void union(int i, int j) {
int iParent = find(i), jParent = find(j);
if (iParent != jParent) {
if (rank[iParent] > rank[jParent]) {
parent[jParent] = iParent;
} else {
parent[iParent] = jParent;
if (rank[iParent] == rank[jParent]) {
rank[jParent]++;
}
}
}
}
}
}
| 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 | 8c66439be29e38cd382af5416491afc4 | 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 codeforces_Edu123_C {
private static void solve(FastIOAdapter in, PrintWriter out) {
int n = in.nextInt();
int x = in.nextInt();
int[] a = in.readArray(n);
var m = new int[n + 1];
Arrays.fill(m, Integer.MIN_VALUE);
m[0] = 0;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = i; j < n; j++) {
sum += a[j];
int len = j - i + 1;
m[len] = Math.max(sum, m[len]);
}
}
for (int i = 0; i <= n; i++) {
int max = Integer.MIN_VALUE;
for (int j = 0; j < m.length; j++) {
max = Math.max(max, m[j] + Math.min(j, i) * x);
}
out.print(max + " ");
}
out.println();
}
public static void main(String[] args) throws Exception {
try (FastIOAdapter ioAdapter = new FastIOAdapter()) {
int count = 1;
count = ioAdapter.nextInt();
while (count-- > 0) {
solve(ioAdapter, ioAdapter.out);
}
}
}
static void ruffleSort(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static class FastIOAdapter implements AutoCloseable {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out))));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
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());
}
@Override
public void close() throws Exception {
out.flush();
out.close();
br.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 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 | f3bdfa6925c08a8f96869ef3dc5b3e6f | 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 codeforces_Edu123_C {
private static void solve(FastIOAdapter in, PrintWriter out) {
int n = in.nextInt();
int x = in.nextInt();
int[] a = in.readArray(n);
var m = new HashMap<Integer, Integer>();
m.put(0, 0);
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = i; j < n; j++) {
sum += a[j];
int len = j - i + 1;
m.put(len, Math.max(sum, m.getOrDefault(len, Integer.MIN_VALUE)));
}
}
for (int i = 0; i <= n; i++) {
int max = Integer.MIN_VALUE;
for (var e : m.entrySet()) {
max = Math.max(max, e.getValue() + Math.min(e.getKey(), i) * x);
}
out.print(max + " ");
}
out.println();
}
public static void main(String[] args) throws Exception {
try (FastIOAdapter ioAdapter = new FastIOAdapter()) {
int count = 1;
count = ioAdapter.nextInt();
while (count-- > 0) {
solve(ioAdapter, ioAdapter.out);
}
}
}
static void ruffleSort(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static class FastIOAdapter implements AutoCloseable {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out))));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
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());
}
@Override
public void close() throws Exception {
out.flush();
out.close();
br.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 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 | 683157b2168ab93225b0040a6958ddaa | 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 |
// Working program with FastReader
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.lang.*;
public class C_Increase_Subarray_Sums {
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;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int d = sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
long sum[]=new long[n+1];
Arrays.fill(sum,Integer.MIN_VALUE);
sum[0]=0;
for(int i=0;i<n;i++){
long curr=0;
for(int j=i;j<n;j++){
curr+=arr[j];
sum[j-i+1]=Math.max(sum[j-i+1],curr);
}
}
for(int k=0;k<=n;k++){
long temp=0;
for(int i=0;i<=n;i++){
long f=(Math.min(k,i)*d)+sum[i];
temp=Math.max(temp,f);
}
System.out.print(temp+" ");
}
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 | 03c513246b8d0c22275474f8647a9d06 | 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.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.io.*;
import java.util.*;
public class C {
static int mod=(int)1e9+7;
public static void main(String[] args) {
var io = new Copied(System.in, System.out);
// int k=1;
int t = 1;
t = io.nextInt();
for (int i = 0; i < t; i++) {
// io.print("Case #" + k + ": ");
solve(io);
// k++;
}
io.close();
}
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;
}
// 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 solve(Copied io) {
int n=io.nextInt(),x=io.nextInt();
int a[]=io.readArray(n),sums[]=new int[n+1],prev=0,mx=Integer.MIN_VALUE;
StringBuilder ans=new StringBuilder();
for(int i=n;i>=0;i--){
mx=max(mx,maxSum(a,n,i));
sums[i]=mx;
// io.println(i+" "+sums[i]);
}
for(int i=0;i<=n;i++){
int k=max(prev,sums[i]+i*x);
ans.append(k+" ");
prev=k;
}
io.println(ans);
}
static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static void binaryOutput(boolean ans, Copied io){
String a=new String("YES"), b=new String("NO");
if(ans){
io.println(a);
}
else{
io.println(b);
}
}
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
Collections.reverse(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void printArr(int[] arr) {
for (int x : arr)
System.out.print(x + " ");
System.out.println();
}
static int[] listToInt(ArrayList<Integer> a){
int n=a.size();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=a.get(i);
}
return arr;
}
static int mod_mul(int a, int b){ a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;}
static int mod_add(int a, int b){ a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;}
static int mod_sub(int a, int b){ a = a % mod; b = b % mod; return (((a - b) % mod) + mod) % mod;}
}
class Pair implements Cloneable, Comparable<Pair>
{
int x,y;
Pair(int a,int b)
{
this.x=a;
this.y=b;
}
Pair()
{
this.x=-1;
this.y=-1;
}
@Override
public boolean equals(Object obj)
{
if(obj instanceof Pair)
{
Pair p=(Pair)obj;
return p.x==this.x && p.y==this.y;
}
return false;
}
@Override
public int hashCode()
{
return Math.abs(x)+101*Math.abs(y);
}
@Override
public String toString()
{
return "("+x+" "+y+")";
}
@Override
protected Pair clone() throws CloneNotSupportedException {
return new Pair(this.x,this.y);
}
@Override
public int compareTo(Pair a)
{
int t= this.x-a.x;
if(t!=0)
return t;
else
return this.y-a.y;
}
}
// class DescendingOrder<T> implements Comparator<T>{
// @Override
// public int compare(Object o1,Object o2){
// // -1 if want to swap and (1,0) otherwise.
// int addingNumber=(Integer) o1,existingNumber=(Integer) o2;
// if(addingNumber>existingNumber) return -1;
// else if(addingNumber<existingNumber) return 1;
// else return 0;
// }
// }
class Copied extends PrintWriter {
public Copied(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Copied(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public String next() {
return nextToken();
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return 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 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 | 2646faabb06e9d9d9ec8eaa06ea7be74 | 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 Practice {
static boolean multipleTC = true;
final static int mod = 1000000007;
final static int mod2 = 998244353;
final double E = 2.7182818284590452354;
final double PI = 3.14159265358979323846;
int MAX = 10000005;
void pre() throws Exception {
}
// All the best
void solve(int TC) throws Exception {
int n = ni(), x = ni();
int arr[] = readArr(n);
long prefix[] = new long[n];
for (int i = 0; i < n; i++) {
if (i > 0) {
prefix[i] = prefix[i - 1];
}
prefix[i] += arr[i];
}
long mx = Integer.MIN_VALUE;
long mp[] = new long[n + 1];
Arrays.fill(mp, Integer.MIN_VALUE);
long mxLen = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
long sum = prefix[j];
if (i > 0) {
sum -= prefix[i - 1];
}
long len = j - i + 1;
if (mp[(int) len] != Integer.MAX_VALUE) {
long old = mp[(int) len];
if (old < sum) {
mp[(int) len] = sum;
}
} else {
mp[(int) len] = sum;
}
if (mx < sum) {
mx = sum;
mxLen = len;
} else if (mx == sum) {
mxLen = max(len, mxLen);
}
}
}
StringBuilder ans = new StringBuilder();
long max = Integer.MIN_VALUE;
for (int i = 0; i <= n; i++) {
for (int j = i; j <= n; j++) {
max = max(max, mp[j] + ((long) (i) * (long) (x)));
}
if (max < 0) {
ans.append("0 ");
} else {
ans.append(max + " ");
}
}
pn(ans);
}
void leftRotatebyOne(int arr[], int n) {
int temp = arr[0], i;
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n - 1] = temp;
}
boolean isFibo(int arr[]) {
int n = arr.length;
for (int i = 0; i + 2 < n; i++) {
if (arr[i] + arr[i + 1] == arr[i + 2]) {
return true;
}
}
return false;
}
public void permute(int[] arr) {
permuteHelper(arr, 0);
}
ArrayList<int[]> perm = new ArrayList<>();
private void permuteHelper(int[] arr, int index) {
if (index >= arr.length - 1) {
perm.add(arr.clone());
return;
}
if (perm.size() > 3 * arr.length) {
return;
}
for (int i = index; i < arr.length; i++) {
int t = arr[index];
arr[index] = arr[i];
arr[i] = t;
permuteHelper(arr, index + 1);
t = arr[index];
arr[index] = arr[i];
arr[i] = t;
}
}
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();
}
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 Practice().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 | 80464a0d3511db430e10259b6de7d767 | 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 SubSums {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static int k, n;
public static void main(String[] args) throws IOException {
int tests = Integer.parseInt(br.readLine());
for (int i = 0; i < tests; i++) {
System.out.println(solve());
}
}
public static String solve() throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
int[] nums = new int[n];
st = new StringTokenizer(br.readLine());
int max = 0;
for (int i = 0; i < n; i++) {
nums[i] = Integer.parseInt(st.nextToken());
}
// for (int i:nums) {
// if (i > max) max = i;
// }
for (int i = 1; i < n; i++) {
nums[i] = nums[i-1] + nums[i];
}
// for (int i:nums) {
// if (i > max) max = i;
// }
int[] maxPrefixes = new int[n];
for (int i = 1; i <= n; i++) {
maxPrefixes[i-1] = maxPrefix(nums, i);
}
for (int i:maxPrefixes) {
if (i > max) max = i;
}
String out = max + " ";
int prev = max;
for (int i = 0; i < n; i++) {
max = Integer.MIN_VALUE;
for (int j = i; j < n; j++) {
maxPrefixes[j] += k;
if (maxPrefixes[j] > max) max = maxPrefixes[j];
}
if (i == n) max = Math.max(max, maxPrefixes[maxPrefixes.length-1] + k);
out += Math.max(max, prev) + " ";
prev = Math.max(max, prev);
}
return out;
}
public static int maxPrefix(int[] prefix, int len) {
int max = prefix[len-1];
for (int i = len; i < n; i++) {
if (prefix[i] - prefix[i-len] > max) max = prefix[i] - prefix[i-len];
}
return max;
}
}
| 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 | b8a897992e4b0b76258babba652e53bb | 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.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.Math.sqrt;
import static java.lang.Math.pow;
import static java.lang.System.out;
import static java.lang.System.err;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
static FastReader sc;
static long mod=((long)1e9)+7;
// static FastWriter out;
public static void main(String hi[]){
initializeIO();
sc=new FastReader();
int t=sc.nextInt();
// boolean[] seave=sieveOfEratosthenes((int)(1e7));
// int[] seave2=sieveOfEratosthenesInt((int)(1e4));
// int tp=1;
while(t--!=0){
int n=sc.nextInt();
// res=new ArrayList<>();
int x=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
List<Integer> li=new ArrayList<>(n+1);
for(int i=0;i<=n;i++)li.add(Integer.MIN_VALUE);
for(int i=0;i<n;i++){
int sum=0;
for(int j=i;j<n;j++){
sum+=arr[j];
li.set(j-i+1,max(li.get(j-i+1),sum));
}
}
// int j=n;
// debug(nums);
for(int i=0;i<=n;i++){
int max=0;
for(int j=0;j<=n;j++){
int sum=li.get(j);
max=max(max,(min(j,i)*x)+sum);
}
out.print(max+" ");
}
out.println();
}
// System.out.println(String.format("%.9f", max));
}
private static int sizeOfSubstring(int st,int e){
int s=e-st+1;
return (s*(s+1))/2;
}
private static int lessThen(long[] nums,long val){
int i=0,l=0,r=nums.length-1;
while(l<=r){
int mid=l+((r-l)/2);
if(nums[mid]<=val){
i=mid;
l=mid+1;
}else{
r=mid-1;
}
}
return i;
}
private static int lessThen(List<Long> nums,long val){
int i=0,l=0,r=nums.size()-1;
while(l<=r){
int mid=l+((r-l)/2);
if(nums.get(mid)<=val){
i=mid;
l=mid+1;
}else{
r=mid-1;
}
}
return i;
}
private static int greaterThen(long[] nums,long val){
int i=nums.length-1,l=0,r=nums.length-1;
while(l<=r){
int mid=l+((r-l)/2);
if(nums[mid]>=val){
i=mid;
r=mid-1;
}else{
l=mid+1;
}
}
return i;
}
private static long sumOfAp(long a,long n,long d){
long val=(n*( 2*a+((n-1)*d)));
return val/2;
}
//geometrics
private static double areaOftriangle(double x1,double y1,double x2,double y2,double x3,double y3){
double[] mid_point=midOfaLine(x1,y1,x2,y2);
// debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3);
double height=distanceBetweenPoints(mid_point[0],mid_point[1],x3,y3);
double wight=distanceBetweenPoints(x1,y1,x2,y2);
// debug(height+" "+wight);
return (height*wight)/2;
}
private static double distanceBetweenPoints(double x1,double y1,double x2,double y2){
double x=x2-x1;
double y=y2-y1;
return sqrt(Math.pow(x,2)+Math.pow(y,2));
}
private static double[] midOfaLine(double x1,double y1,double x2,double y2){
double[] mid=new double[2];
mid[0]=(x1+x2)/2;
mid[1]=(y1+y2)/2;
return mid;
}
private static long sumOfN(long n){
return (n*(n+1))/2;
}
private static long power(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;
}
/* Function to calculate x raised to the power y in O(logn)*/
static long power(long x, long y){
long temp;
if( y == 0)
return 1l;
temp = power(x, y / 2);
if (y % 2 == 0)
return (temp*temp);
else
return (x*temp*temp);
}
private static StringBuilder reverseString(String s){
StringBuilder sb=new StringBuilder(s);
int l=0,r=sb.length()-1;
while(l<=r){
char ch=sb.charAt(l);
sb.setCharAt(l,sb.charAt(r));
sb.setCharAt(r,ch);
l++;
r--;
}
return sb;
}
// private static void swap(long arr[],int i,int j){
// long t=arr[i];
// arr[i]=arr[j];
// arr[j]=t;
// }
// private static void swap(int arr[],int i,int j){
// int t=arr[i];
// arr[i]=arr[j];
// arr[j]=t;
// }
private static void swap(double arr[],int i,int j){
double t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
private static void swap(float arr[],int i,int j){
float t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static long lcm(long a, long b){
return (a / gcd(a, b)) * b;
}
private static String decimalToString(int x){
return Integer.toBinaryString(x);
}
private static boolean isPallindrome(String s){
int l=0,r=s.length()-1;
while(l<r){
if(s.charAt(l)!=s.charAt(r))return false;
l++;
r--;
}
return true;
}
private static StringBuilder removeLeadingZero(StringBuilder sb){
int i=0;
while(i<sb.length()&&sb.charAt(i)=='0')i++;
// debug("remove "+i);
if(i==sb.length())return new StringBuilder();
return new StringBuilder(sb.substring(i,sb.length()));
}
private static int stringToDecimal(String binaryString){
// debug(decimalToString(n<<1));
return Integer.parseInt(binaryString,2);
}
private static int stringToInt(String s){
return Integer.parseInt(s);
}
private static String toString(int val){
return String.valueOf(val);
}
private static void debug(int[][] arr){
for(int i=0;i<arr.length;i++){
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(long[][] arr){
for(int i=0;i<arr.length;i++){
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(List<int[]> arr){
for(int[] a:arr){
err.println(Arrays.toString(a));
}
}
private static void debug(float[][] arr){
for(int i=0;i<arr.length;i++){
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(double[][] arr){
for(int i=0;i<arr.length;i++){
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(boolean[][] arr){
for(int i=0;i<arr.length;i++){
err.println(Arrays.toString(arr[i]));
}
}
private static void print(String s){
out.println(s);
}
private static void debug(String s){
err.println(s);
}
private static int charToIntS(char c){
return ((((int)(c-'0'))%48));
}
private static void print(double s){
out.println(s);
}
private static void print(float s){
out.println(s);
}
private static void print(long s){
out.println(s);
}
private static void print(int s){
out.println(s);
}
private static void debug(double s){
err.println(s);
}
private static void debug(float s){
err.println(s);
}
private static void debug(long s){
err.println(s);
}
private static void debug(int s){
err.println(s);
}
private static boolean isPrime(int n){
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
//read graph
private static List<List<Integer>> readUndirectedGraph(int n){
List<List<Integer>> graph=new ArrayList<>();
for(int i=0;i<=n;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<n;i++){
int x=sc.nextInt();
int y=sc.nextInt();
graph.get(x).add(y);
graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readUndirectedGraph(int[][] intervals,int n){
List<List<Integer>> graph=new ArrayList<>();
for(int i=0;i<=n;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<intervals.length;i++){
int x=intervals[i][0];
int y=intervals[i][1];
graph.get(x).add(y);
graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readDirectedGraph(int[][] intervals,int n){
List<List<Integer>> graph=new ArrayList<>();
for(int i=0;i<=n;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<intervals.length;i++){
int x=intervals[i][0];
int y=intervals[i][1];
graph.get(x).add(y);
// graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readDirectedGraph(int n){
List<List<Integer>> graph=new ArrayList<>();
for(int i=0;i<=n;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<n;i++){
int x=sc.nextInt();
int y=sc.nextInt();
graph.get(x).add(y);
// graph.get(y).add(x);
}
return graph;
}
static String[] readStringArray(int n){
String[] arr=new String[n];
for(int i=0;i<n;i++){
arr[i]=sc.next();
}
return arr;
}
private static Map<Character,Integer> freq(String s){
Map<Character,Integer> map=new HashMap<>();
for(char c:s.toCharArray()){
map.put(c,map.getOrDefault(c,0)+1);
}
return map;
}
static boolean[] sieveOfEratosthenes(long n){
boolean prime[] = new boolean[(int)n + 1];
for (int i = 2; i <= n; i++)
prime[i] = true;
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 * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
static int[] sieveOfEratosthenesInt(long n){
boolean prime[] = new boolean[(int)n + 1];
Set<Integer> li=new HashSet<>();
for (int i = 1; i <= n; i++){
prime[i] = true;
li.add(i);
}
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true){
for (int i = p * p; i <= n; i += p){
li.remove(i);
prime[i] = false;
}
}
}
int[] arr=new int[li.size()+1];
int i=0;
for(int x:li){
arr[i++]=x;
}
return arr;
}
public static long Kadens(List<Long> prices) {
long sofar=0;
long max_v=0;
for(int i=0;i<prices.size();i++){
sofar+=prices.get(i);
if (sofar<0) {
sofar=0;
}
max_v=Math.max(max_v,sofar);
}
return max_v;
}
public static int Kadens(int[] prices) {
int sofar=0;
int max_v=0;
for(int i=0;i<prices.length;i++){
sofar+=prices[i];
if (sofar<0) {
sofar=0;
}
max_v=Math.max(max_v,sofar);
}
return max_v;
}
static boolean isMemberAC(int a, int d, int x){
// If difference is 0, then x must
// be same as a.
if (d == 0)
return (x == a);
// Else difference between x and a
// must be divisible by d.
return ((x - a) % d == 0 && (x - a) / d >= 0);
}
private static void sort(int[] arr){
int n=arr.length;
List<Integer> li=new ArrayList<>();
for(int x:arr){
li.add(x);
}
Collections.sort(li);
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sortReverse(int[] arr){
int n=arr.length;
List<Integer> li=new ArrayList<>();
for(int x:arr){
li.add(x);
}
Collections.sort(li,Collections.reverseOrder());
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sort(double[] arr){
int n=arr.length;
List<Double> li=new ArrayList<>();
for(double x:arr){
li.add(x);
}
Collections.sort(li);
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sortReverse(double[] arr){
int n=arr.length;
List<Double> li=new ArrayList<>();
for(double x:arr){
li.add(x);
}
Collections.sort(li,Collections.reverseOrder());
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sortReverse(long[] arr){
int n=arr.length;
List<Long> li=new ArrayList<>();
for(long x:arr){
li.add(x);
}
Collections.sort(li,Collections.reverseOrder());
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sort(long[] arr){
int n=arr.length;
List<Long> li=new ArrayList<>();
for(long x:arr){
li.add(x);
}
Collections.sort(li);
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static long sum(int[] arr){
long sum=0;
for(int x:arr){
sum+=x;
}
return sum;
}
private static long evenSumFibo(long n){
long l1=0,l2=2;
long sum=0;
while (l2<n) {
long l3=(4*l2)+l1;
sum+=l2;
if(l3>n)break;
l1=l2;
l2=l3;
}
return sum;
}
private static void initializeIO(){
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
System.setErr(new PrintStream(new FileOutputStream("error.txt")));
} catch (Exception e) {
// System.err.println(e.getMessage());
}
}
private static int maxOfArray(int[] arr){
int max=Integer.MIN_VALUE;
for(int x:arr){
max=Math.max(max,x);
}
return max;
}
private static long maxOfArray(long[] arr){
long max=Long.MIN_VALUE;
for(long x:arr){
max=Math.max(max,x);
}
return max;
}
private static int[][] readIntIntervals(int n,int m){
int[][] arr=new int[n][m];
for(int j=0;j<n;j++){
for(int i=0;i<m;i++){
arr[j][i]=sc.nextInt();
}
}
return arr;
}
private static long gcd(long a,long b){
if(b==0)return a;
return gcd(b,a%b);
}
private static int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
private static int[] readIntArray(int n){
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
return arr;
}
private static double[] readDoubleArray(int n){
double[] arr=new double[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextDouble();
}
return arr;
}
private static long[] readLongArray(int n){
long[] arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextLong();
}
return arr;
}
private static void print(int[] arr){
out.println(Arrays.toString(arr));
}
private static void print(long[] arr){
out.println(Arrays.toString(arr));
}
private static void print(String[] arr){
out.println(Arrays.toString(arr));
}
private static void print(double[] arr){
out.println(Arrays.toString(arr));
}
private static void debug(String[] arr){
err.println(Arrays.toString(arr));
}
private static void debug(Boolean[][] arr){
for(int i=0;i<arr.length;i++)err.println(Arrays.toString(arr[i]));
}
private static void debug(int[] arr){
err.println(Arrays.toString(arr));
}
private static void debug(long[] arr){
err.println(Arrays.toString(arr));
}
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !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(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
static class Dsu {
int[] parent, size;
Dsu(int n) {
parent = new int[n + 1];
size = new int[n + 1];
for (int i = 0; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}
private int findParent(int u) {
if (parent[u] == u) return u;
return parent[u] = findParent(parent[u]);
}
private boolean union(int u, int v) {
// System.out.println("uf "+u+" "+v);
int pu = findParent(u);
// System.out.println("uf2 "+pu+" "+v);
int pv = findParent(v);
// System.out.println("uf3 " + u + " " + pv);
if (pu == pv) return false;
if (size[pu] <= size[pv]) {
parent[pu] = pv;
size[pv] += size[pu];
} else {
parent[pv] = pu;
size[pu] += size[pv];
}
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 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 | 15d2e3a8aa2f677bfc82408e7316b314 | 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.lang.*;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.*;
import java.util.LinkedList;
public class CEdu {
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 void sort(long[] arr) {
ArrayList<Long> a = new ArrayList<>();
for (long i : arr) {
a.add(i);
}
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
arr[i] = a.get(i);
}
}
static boolean get(char[] ch, int i, long k) {
long ope = 0;
for (int j = i; j >= 0; j--) {
long ele = ((ch[j] - '0') + ope) % 10;
long req = 10 - ele;
if (req == 10) {
req = 0;
}
if (ope + req <= k) {
ope = ope + req;
if (j == 0) {
return true;
}
} else {
return false;
}
}
return false;
}
static void solve(long a, long b, long c) {
System.out.println((a | b) & (b | c) & (c | a));
}
static long get(long[] arr, int low, int high) {
long len = high - low + 1;
for (int i = low; i <= high; i++) {
if (arr[i] == 0) {
len++;
}
}
return len;
}
static void leftRotatebyOne(long arr[], int n) {
int i;
long temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n - 1] = temp;
for (int ii = 0; ii < arr.length; ii++) {
System.out.print(arr[ii] + " ");
}
}
public static 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;
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
int t = sc.nextInt();
test:
while (t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
long[] arr = new long[n];
long sum = 0;
long pre = 0;
StringBuilder ans = new StringBuilder();
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
sum += arr[i];
pre = Math.max(arr[i], pre);
}
long[] sub = new long[n + 1];
int ind = 0;
int len = 0;
long subsum = pre;
for (int i = 0; i <= n; i++) {
sub[ind++]= maxSum(arr, n, i);
}
for(int i = 0; i <=n; i++){
long y = 0;
for(int j = 0; j <=n; j++){
y = Math.max(y, sub[j] + Math.min(i,j)*k);
}
ans.append(y+" ");
}
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 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 | f9b1501eb6dd2141b059bb0154f35368 | 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.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;
public class C {
private static StringTokenizer st;
private static BufferedReader br;
private static PrintWriter pw;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int t = nextInt();
while (t > 0) {
run();
t--;
}
pw.close();
}
private static void run() throws IOException {
int n = nextInt();
int x = nextInt();
int[] a = new int[n + 1];
int[] s = new int[n + 1];
int[] dp1 = new int[n + 1];
int[] dp2 = new int[2 * n + 5];
for (int i = 1; i <= n; i++) {
a[i] = nextInt();
s[i] = s[i - 1] + a[i];
}
int mn = 0;
for (int i = 1; i <= n; i++) {
mn = Math.min(mn, s[i]);
dp1[i] = s[i] - mn;
}
int mx = s[n];
for (int i = n; i >= 1; i--) {
mx = Math.max(mx, s[i]);
dp2[i] = Math.max(mx - s[i - 1], 0);
}
int[] res = new int[n + 1];
for (int k = 0; k <= n; k++) {
mx = 0;
for (int i = 1; i - 1 + k <= n; i++) {
int s1 = 0, s2 = 0, s3 = 0;
s1 = dp1[i - 1];
s2 = s[i - 1 + k] - s[i - 1] + k * x;
// int t = 0;
// for (int j = i + k; j <= n; j++) {
// t += a[j];
// s3 = Math.max(s3, t);
// }
s3 = dp2[i + k];
mx = Math.max(mx, s1 + s2 + s3);
}
if (k == 0) res[k] = mx;
else res[k] = Math.max(res[k - 1], mx);
}
for (int re : res) {
pw.print(re + " ");
}
pw.println("");
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
} | 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 | d7efe55e2f6b2a22ab8d5b2b3150dcfa | 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.math.BigInteger;
import java.io.*;
public class Main implements Runnable {
public static void main(String[] args) {
new Thread(null, new Main(), "whatever", 1 << 26).start();
}
//
private FastScanner sc;
private PrintWriter pw;
public void run() {
try {
boolean isSumitting = true;
// isSumitting = false;
if (isSumitting) {
pw = new PrintWriter(System.out);
sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
} else {
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
sc = new FastScanner(new BufferedReader(new FileReader("input.txt")));
}
} catch (Exception e) {
throw new RuntimeException();
}
int t = sc.nextInt();
// int t = 1;
while (t-- > 0) {
// sc.nextLine();
// System.out.println("for t=" + t);
solve();
}
pw.close();
}
public long mod = 1_000_000_007;
private class Pair {
int first, second;
Pair(int first, int second) {
this.first = first + 1;
this.second = second + 1;
}
}
public void solve() {
int n = sc.nextInt();
int k = sc.nextInt();
int[] arr = new int[n];
long[] pre = new long[n];
long[] subArrays = new long[n + 1];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Arrays.fill(subArrays, Integer.MIN_VALUE);
pre[0] = arr[0];
for (int i = 1; i < n; i++) pre[i] = pre[i - 1] + arr[i];
for (int q = 1; q <= n; q++) {
for (int i = 0; i + q - 1 < n; i++) {
subArrays[q] = Math.max(subArrays[q], pre[i + q - 1] - pre[i] + (long)arr[i]);
}
// System.out.println("q=" + q + " , subArrays[q]=" + subArrays[q]);
}
for (int q = 0; q < n + 1; q++) {
long max = 0;
for (int i = 1; i < q; i++) {
max = Math.max(max, subArrays[i] + i * k);
}
for (int i = q; i <= n; i++) {
max = Math.max(max, subArrays[i] + (long)(q * (long)k));
}
pw.print(max + " ");
}
pw.println();
}
class FastScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(BufferedReader bf) {
reader = bf;
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public float nextFloat() {
return Float.parseFloat(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String[] nextStringArray(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) {
a[i] = next();
}
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
}
private static class Sorter {
public static <T extends Comparable<? super T>> void sort(T[] arr) {
Arrays.sort(arr);
}
public static <T> void sort(T[] arr, Comparator<T> c) {
Arrays.sort(arr, c);
}
public static <T> void sort(T[][] arr, Comparator<T[]> c) {
Arrays.sort(arr, c);
}
public static <T extends Comparable<? super T>> void sort(ArrayList<T> arr) {
Collections.sort(arr);
}
public static <T> void sort(ArrayList<T> arr, Comparator<T> c) {
Collections.sort(arr, c);
}
public static void normalSort(int[] arr) {
Arrays.sort(arr);
}
public static void normalSort(long[] arr) {
Arrays.sort(arr);
}
public static void sort(int[] arr) {
timSort(arr);
}
public static void sort(int[] arr, Comparator<Integer> c) {
timSort(arr, c);
}
public static void sort(int[][] arr, Comparator<Integer[]> c) {
timSort(arr, c);
}
public static void sort(long[] arr) {
timSort(arr);
}
public static void sort(long[] arr, Comparator<Long> c) {
timSort(arr, c);
}
public static void sort(long[][] arr, Comparator<Long[]> c) {
timSort(arr, c);
}
private static void timSort(int[] arr) {
Integer[] temp = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(int[] arr, Comparator<Integer> c) {
Integer[] temp = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(int[][] arr, Comparator<Integer[]> c) {
Integer[][] temp = new Integer[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
}
private static void timSort(long[] arr) {
Long[] temp = new Long[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(long[] arr, Comparator<Long> c) {
Long[] temp = new Long[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(long[][] arr, Comparator<Long[]> c) {
Long[][] temp = new Long[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
}
}
public long fastPow(long x, long y, long mod) {
if (y == 0) return 1;
if (y == 1) return x % mod;
long temp = fastPow(x, y / 2, mod);
long ans = (temp * temp) % mod;
return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans;
}
public long fastPow(long x, long y) {
if (y == 0) return 1;
if (y == 1) return x;
long temp = fastPow(x, y / 2);
long ans = (temp * temp);
return (y % 2 == 1) ? (ans * x) : 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 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 | e01b07b0be5be12b0b3c0a6acaf9475a | 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.lang.*;
public class Solution{
static long mod=(long)1e9+7;
static int[] cost=new int[(int)1005];
static StringBuffer ans1=new StringBuffer("");
static FastScanner sc = new FastScanner();
public static void solve(){
int n=sc.nextInt(),x=sc.nextInt();
int[] a=sc.readArray(n);
int[] pre=new int[n+1];
pre[0]=0;
for (int i=1;i<=n;i++) {
pre[i]=pre[i-1]+a[i-1];
}
int[] dp=new int[n+1];
Arrays.fill(dp,Integer.MIN_VALUE);
for (int i=1;i<=n;i++) {
for (int j=i;j<=n;j++) {
dp[i]=Math.max(dp[i],pre[j]-pre[j-i]);
}
}
dp[0]=0;
for (int i=0;i<=n;i++) {
dp[0]=Math.max(dp[0],dp[i]);
}
// for (int i=0;i<=n; i++) {
// System.out.print(dp[i]+" ");
// }
// System.out.println("");
for (int i=1;i<=n;i++) {
for (int j=i;j<=n;j++ ) {
dp[i]=Math.max(dp[i],dp[j]+(i*x));
}
dp[i]=Math.max(dp[i],dp[i-1]);
}
for (int i=0;i<=n; i++) {
System.out.print(dp[i]+" ");
}
System.out.println("");
}
public static void main(String[] args) {
int t = sc.nextInt();
// int t=1;
for (int i=2;i<cost.length;i++)
cost[i]=cost[i-1]+1;
for (int i=2;i<cost.length;i++) {
// cost[i]=i-1;
for (int j=1;j<=i/2+1;j++) {
if((i+i/j)<=1004)
cost[i+i/j]=min(cost[i+i/j],cost[i]+1);
}
// if (i%2==0) {
// cost[i]=cost[i/2]+1;
// }else{
// cost[i]=cost[i-1];
// }
}
outer: for (int tt = 0; tt < t; tt++) {
solve();
}
System.out.println(ans1);
}
static boolean isSubSequence(String str1, String str2,
int m, int n)
{
int j = 0;
for (int i = 0; i < n && j < m; i++)
if (str1.charAt(j) == str2.charAt(i))
j++;
return (j == m);
}
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[] LPS(String s){
int[] lps=new int[s.length()];
int i=0,j=1;
while (j<s.length()) {
if(s.charAt(i)==s.charAt(j)){
lps[j]=i+1;
i++;
j++;
continue;
}else{
if (i==0) {
j++;
continue;
}
i=lps[i-1];
while(s.charAt(i)!=s.charAt(j) && i!=0) {
i=lps[i-1];
}
if(s.charAt(i)==s.charAt(j)){
lps[j]=i+1;
i++;
}
j++;
}
}
return lps;
}
static long getPairsCount(int n, double sum,int[] arr)
{
HashMap<Double, Integer> hm = new HashMap<>();
for (int i = 0; i < n; i++) {
if (!hm.containsKey((double)arr[i]))
hm.put((double)arr[i], 0);
hm.put((double)arr[i], hm.get((double)arr[i]) + 1);
}
long twice_count = 0;
for (int i = 0; i < n; i++) {
if (hm.get(sum - arr[i]) != null)
twice_count += hm.get(sum - arr[i]);
if (sum - (double)arr[i] == (double)arr[i])
twice_count--;
}
return twice_count / 2l;
}
static boolean[] 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;
}
}
prime[1]=false;
return prime;
}
static long power(long x, long y, long p)
{
long res = 1l;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y>>=1;
x = (x * x) % p;
}
return res;
}
public static int log2(int N)
{
int result = (int)(Math.log(N) / Math.log(2));
return result;
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////DO NOT READ AFTER THIS LINE //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
static long modFact(int n,
int p)
{
if (n >= p)
return 0;
long result = 1l;
for (int i = 3; i <= n; i++)
result = (result * i) % p;
return result;
}
static boolean isPalindrom(char[] arr, int i, int j) {
boolean ok = true;
while (i <= j) {
if (arr[i] != arr[j]) {
ok = false;
break;
}
i++;
j--;
}
return ok;
}
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 abs(int a) {
return Math.abs(a);
}
static long abs(long a) {
return Math.abs(a);
}
static void swap(long arr[], int i, int j) {
long temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static int maxArr(int arr[]) {
int maxi = Integer.MIN_VALUE;
for (int x : arr)
maxi = max(maxi, x);
return maxi;
}
static int minArr(int arr[]) {
int mini = Integer.MAX_VALUE;
for (int x : arr)
mini = min(mini, x);
return mini;
}
static long maxArr(long arr[]) {
long maxi = Long.MIN_VALUE;
for (long x : arr)
maxi = max(maxi, x);
return maxi;
}
static long minArr(long arr[]) {
long mini = Long.MAX_VALUE;
for (long x : arr)
mini = min(mini, x);
return mini;
}
static int lcm(int a,int b){
return (int)(((long)a*b)/(long)gcd(a,b));
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static void ruffleSort(int[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
int temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
public static int binarySearch(int a[], int target) {
int left = 0;
int right = a.length - 1;
int mid = (left + right) / 2;
int i = 0;
while (left <= right) {
if (a[mid] <= target) {
i = mid + 1;
left = mid + 1;
} else {
right = mid - 1;
}
mid = (left + right) / 2;
}
return i-1;
}
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;
}
int[][] read2dArray(int n, int m) {
int arr[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = nextInt();
}
}
return arr;
}
ArrayList<Integer> readArrayList(int n) {
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int a = nextInt();
arr.add(a);
}
return arr;
}
long nextLong() {
return Long.parseLong(next());
}
}
static class Pair {
int fr,sc;
Pair(int fr, int sc) {
this.fr = fr;
this.sc = sc;
}
}
} | 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 | 5747a1b98a1d20cdc3796a40d2698055 | 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 cf {
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException, InterruptedException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt(), x = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
int[] max = new int[n + 1];
Arrays.fill(max, Integer.MIN_VALUE);
for (int i = 0; i < arr.length; i++) {
int s = 0;
for (int j = i; j < arr.length; j++) {
s += arr[j];
max[j - i + 1] = Math.max(max[j - i + 1], s);
}
}
int[] suf = new int[n + 1];
suf[n] = max[n];
for (int i = n - 1; i >= 0; i--) {
suf[i] = Math.max(suf[i + 1], max[i]);
}
// pw.println(Arrays.toString(suf));
long[] ans = new long[n + 1];
for (int i = 0; i <= n; i++) {
ans[i] = Math.max(suf[i] + i * x, 0);
}
for (int i = 0; i < ans.length; i++) {
pw.print((ans[i] = Math.max(i == 0 ? 0 : ans[i - 1], ans[i])) + " ");
}
pw.println();
}
pw.close();
}
public static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y)
return this.z - other.z;
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
public static class pair implements Comparable<pair> {
int x;
int y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Integer(x).hashCode() * 31 + new Integer(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
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 {
return Double.parseDouble(next());
}
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 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 | 6d283307d783b7056af08226f916a5ad | 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 | // ceil using integer division: ceil(x/y) = (x+y-1)/y
import java.util.*;
import java.lang.*;
import java.io.*;
public class practice {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int t = Reader.nextInt();
while(t-- > 0) {
int n = Reader.nextInt();
int x = Reader.nextInt();
long[] arr = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Reader.nextLong();
}
long overall_max = 0;
long[] arr2 = new long[n+1];
for(int i = 1;i<=n;i++){
long sum = 0;
long max = Long.MIN_VALUE;
int st = 0, en = 0;
while(en<n){
sum+=arr[en];
if(en-st+1<i){
en++;
}
else{
max = Math.max(max,sum);
sum-=arr[st];
st++;
en++;
}
}
arr2[i] = max;
}
// System.out.println(Arrays.toString(arr2));
long left = 0;
long[] arr3 = new long[n+1];
arr3[n] = arr2[n];
for(int i =n-1;i>=1;i--){
arr3[i] = Math.max(Math.max(arr3[i+1],arr2[i]),arr2[i+1]);
}
// System.out.println(Arrays.toString(arr3));
System.out.print(Math.max(0,arr3[1])+" ");
for(int i = 1;i<=n;i++){
System.out.print(Math.max(0,Math.max(left,arr3[i]+ (long) i *x))+" ");
left = Math.max(left,arr2[i]+ (long) i *x);
}
System.out.println();
}
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static String nextLine() throws IOException {
return reader.readLine();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException{
return Long.parseLong(next() );
}
static double nextDouble() throws IOException {
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 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 | dd7f8377671ddf1f9b9e4387e32af5a6 | 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 Q1644C {
static int mod = (int) (1e9 + 7);
static StringBuilder sb = new StringBuilder();
static void solve() {
int n = i();
long x = l();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = l();
}
long[] sumOfSubarray = new long[n + 1];
for (int i = 0; i <= n; i++) {
long ans = Long.MIN_VALUE;
long sum = 0;
for (int j = 0; j < i; j++) {
sum += arr[j];
}
ans = Math.max(ans, sum);
int left = 0;
int right = i;
while (right < n) {
sum -= arr[left];
sum += arr[right];
left++;
right++;
ans = Math.max(ans, sum);
}
sumOfSubarray[i] = ans;
}
// System.out.println(Arrays.toString(sumOfSubarray));
for (int possiblek = 0; possiblek <= n; possiblek++) {
long ans=Long.MIN_VALUE;
for(int i=0;i<sumOfSubarray.length;i++){
ans=Math.max(ans,sumOfSubarray[i]+Math.min(possiblek, i)*x);
}
// System.out.println(ans);
sb.append(ans+" ");
}
sb.append("\n");
}
public static void main(String[] args) {
int test = i();
while (test-- > 0) {
solve();
}
System.out.println(sb);
}
// ----->segmentTree--> segTree as class
// ----->lazy_Seg_tree --> lazy_Seg_tree as class
// -----> Trie --->Trie as class
// ----->fenwick_Tree ---> fenwick_Tree
// -----> POWER ---> long power(long x, long y) <----
// -----> LCM ---> long lcm(long x, long y) <----
// -----> GCD ---> long gcd(long x, long y) <----
// -----> SIEVE --> ArrayList<Integer> sieve(int N) <-----
// -----> NCR ---> long ncr(int n, int r) <----
// -----> BINARY_SEARCH ---> int binary_Search(long[]arr,long x) <----
// -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<--
// ----> DFS ---> void dfs(ArrayList<ArrayList<Integer>>edges,int child,int
// parent)<---
// ---> NODETOROOT --> ArrayList<Integer>
// node2Root(ArrayList<ArrayList<Integer>>edges,int child,int parent,int tofind)
// <--
// ---> LEVELS_TREE -->levels_Trees(ArrayList<HashSet<Integer>> edges, int
// child, int parent,int[]level,int currLevel) <--
// ---> K_THPARENT --> int k_thparent(int node,int[][]parent,int k) <---
// ---> TWOPOWERITHPARENTFILL --> void twoPowerIthParentFill(int[][]parent) <---
// -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<-
// tempstart
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} 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 String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static InputReader in = new InputReader(System.in);
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
}
| 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 | 0c9bff4ae858e8fe12dc5ec7cee6b4b1 | 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 k=sc.nextInt();
int arr[]=new int[n];
long highest[]=new long[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
highest[i]=-1111111;
}
long sum=0;
for(int i=0;i<n;i++) {
for(int j=i,pos=0;j>=0;j--,pos++) {
sum+=arr[j];
// System.out.println(sum+" sum");
if(highest[pos]==-1111111) {
highest[pos]=sum;
}
else {
highest[pos]=Math.max(highest[pos], sum);
}
}
sum=0;
}
// System.out.println(Arrays.toString(highest));
for(int i=n-2;i>=0;i--) {
highest[i]=Math.max(highest[i],highest[i+1]);
}
// System.out.println(Arrays.toString(highest));
if(highest[0]<0) {
System.out.print(0+" ");
}
else {
System.out.print(highest[0]+" ");
}
for(int i=0;i<n;i++) {
if(i==0) {
highest[i]=highest[i]+(i+1*k);
}
else {
highest[i]=Math.max(highest[i-1],highest[i]+((i+1)*k));
}
}
for(int i=0;i<n;i++) {
if(highest[i]<0) {
highest[i]=0;
}
System.out.print(highest[i]+" ");
}
System.out.println();
// System.out.println(Arrays.toString(highest));
}
}
}
| 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 | 689812971294f7c7596cf57091fc28d6 | 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 {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int x = sc.nextInt();
int[] temp = sc.nextIntArray(n);
HashMap<Integer, Long> map = new HashMap<>();
map.put(0, 0l);
for (int i = 0; i < temp.length; i++) {
long sum = 0;
for (int j = i; j < temp.length; j++) {
sum += temp[j];
map.put(j - i + 1, Math.max(sum, map.getOrDefault(j - i + 1, Long.MIN_VALUE)));
}
}
long max = Long.MIN_VALUE;
for (int i = 0; i <= n; i++) {
for (int length : map.keySet()) {
long after = 1l * x * Math.min(i, length) + map.get(length);
max = Math.max(max, after);
}
pw.print(max + " ");
}
pw.println();
}
pw.close();
}
static int diffWords(char[] arr1, char[] arr2) {
int diff = 0;
for (int i = 0; i < arr2.length; i++) {
diff += Math.abs(arr1[i] - arr2[i]);
}
return diff;
}
static int countOnes(String s) {
int res = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1')
res++;
}
return res;
}
static long getMinDiff(long num, int[] arr) {
long min = Integer.MAX_VALUE;
for (int i = 1; i < arr.length - 1; i++) {
min = Math.min(min, getdiff(num, arr[i]));
}
return min;
}
static long getdiff(long start, int end) {
return Math.abs(start - end);
}
static HashMap Hash(int[] arr) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i : arr) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
return map;
}
static HashMap Hash(char[] arr) {
HashMap<Character, Integer> map = new HashMap<>();
for (char i : arr) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
return map;
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
public static long combination(long n, long r) {
return factorial(n) / (factorial(n - r) * factorial(r));
}
static long factorial(Long n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
static boolean isPalindrome(char[] str, int i, int j) {
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str[i] != str[j])
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
public static double log2(long l) {
double result = (Math.log(l) / Math.log(2));
return result;
}
public static double log4(int N) {
double result = (Math.log(N) / Math.log(4));
return result;
}
public static int setBit(int mask, int idx) {
return mask | (1 << idx);
}
public static int clearBit(int mask, int idx) {
return mask ^ (1 << idx);
}
public static boolean checkBit(int mask, int idx) {
return (mask & (1 << idx)) != 0;
}
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();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
public static String toString(int[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + " ");
}
return sb.toString().trim();
}
public static String toString(long[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + " ");
}
return sb.toString().trim();
}
public static String toString(ArrayList arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.size(); i++) {
sb.append(arr.get(i) + " ");
}
return sb.toString().trim();
}
public static String toString(int[][] arr) {
StringBuilder sb = new StringBuilder();
for (int[] i : arr) {
sb.append(toString(i) + "\n");
}
return sb.toString();
}
public static String toString(boolean[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + " ");
}
return sb.toString().trim();
}
public static String toString(Integer[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + " ");
}
return sb.toString().trim();
}
public static String toString(String[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + " ");
}
return sb.toString().trim();
}
public static String toString(char[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + " ");
}
return sb.toString().trim();
}
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(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 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 | f885e99029d13347e5efdfc67f9ffe6b | 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.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.math.BigInteger;
public final class Main{
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new 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') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
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();
}
}
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()); }
}
static Kattio sc = new Kattio();
static long mod = 998244353l;
static PrintWriter out =new PrintWriter(System.out);
//Heapify function to maintain heap property.
public static void swap(int i,int j,int arr[]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void swap(int i,int j,long arr[]) {
long temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void swap(int i,int j,char arr[]) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static String endl = "\n" , gap = " ";
public static void main(String[] args)throws IOException {
int t =ri();
// List<Integer> list = new ArrayList<>();
// int MAX = (int)4e4;
// for(int i =1;i<=MAX;i++) {
// if(isPalindrome(i + "")) list.add(i);
// }
// // System.out.println(list);
// long dp[] = new long[MAX +1];
// dp[0] = 1;
// long mod = (long)(1e9+7);
// for(int x : list) {
// for(int i =1;i<=MAX;i++) {
// if(i >= x) {
// dp[i] += dp[i-x];
// dp[i] %= mod;
// }
// }
// }
// int MAK = (int)1e6;
// boolean seive[] = new boolean[MAK];
// Arrays.fill(seive , true);
// seive[1] = false;
// seive[0] = false;
// for (int i = 1; i < MAK; i++) {
// if(seive[i]) {
// for (int j = i+i; j < MAK; j+=i) {
// seive[j] = false;
// }
// }
// }
// for (int i = 1; i <= MAK; i++) {
// seive[i] += seive[i - 1];
// }
int test_case = 1;
while(t-->0) {
// out.write("Case #"+(test_case++)+": ");
solve();
}
out.close();
}
static HashMap<Integer , Boolean>dp;
public static void solve()throws IOException {
int n = ri() , x = ri();
int a[] = rai(n);
int arr[] = new int[n+1];
Arrays.fill(arr , Integer.MIN_VALUE);
arr[0] = 0;
for(int i =0;i<n;i++) {
int sum = 0;
for(int r =i;r<n;r++) {
sum += a[r];
arr[r-i+1] = Math.max(sum , arr[r-i+1]);
}
}
for(int k =0;k<=n;k++) {
int ans = 0;
for(int len = 0;len <= n;len++) {
ans = Math.max(ans , arr[len] + Math.min(len ,k )*x);
}
System.out.print(ans + " ");
}
System.out.println();
}
public static void print(int a[]) {
for(int x : a) {
out.write(x + " ");
}
out.write(endl);
}
public static long callfun(int n) {
if(n == 1) return 1;
return n + n*callfun(n-1);
}
public static double getSlope(int a , int b,int x,int y) {
if(a-x == 0) return Double.MAX_VALUE;
if(b-y == 0) return 0.0;
return ((double)b-(double)y)/((double)a-(double)x);
}
public static int callfun(int a[], int []b) {
HashMap<Integer , Integer> map = new HashMap<>();
int n = a.length;
for(int i =0;i<b.length;i++) {
map.put(b[i] , i);
}
HashMap<Integer , Integer> cnt = new HashMap<>();
for(int i =0;i<n;i++) {
int move = (map.get(a[i])-i+n)%n;
cnt.put(move , cnt.getOrDefault(move,0) + 1);
}
int max =0;
for(int x : cnt.keySet()) max = Math.max(max , cnt.get(x));
return max;
}
public static boolean collinearr(long a[] , long b[] , long c[]) {
return (b[1]-a[1])*(b[0]-c[0]) == (b[0]-a[0])*(b[1]-c[1]);
}
public static boolean isSquare(int sum) {
int root = (int)Math.sqrt(sum);
return root*root == sum;
}
public static boolean canPlcae(int prev,int len , int sum,int arr[],int idx) {
// System.out.println(sum + " is sum prev " + prev);
if(len == 0) {
if(isSquare(sum)) return true;
return false;
}
for(int i = prev;i<=9;i++) {
arr[idx] = i;
if(canPlcae(i, len-1,sum + i*i,arr,idx + 1)) {
return true;
}
}
return false;
}
public static boolean isPalindrome(String s) {
int i =0 , j = s.length() -1;
while(i <= j && s.charAt(i) == s.charAt(j)) {
i++;
j--;
}
return i>j;
}
// digit dp hint;
public static long callfun(String num , int N, int last ,int secondLast ,int bound ,long dp[][][][]) {
if(N == 1) {
if(last == -1 || secondLast == -1) return 0;
long answer = 0;
int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9;
for(int i = 0;i<=max;i++) {
if(last > secondLast && last > i) {
answer++;
}
if(last < secondLast && last < i) {
answer++;
}
}
return answer;
}
if(secondLast == -1 || last == -1 ){
long answer = 0l;
int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9;
for(int i =0;i<=max;i++) {
int nl , nsl , newbound = bound==0?0:i==max?1:0;
if(last == - 1&& secondLast == -1 && i == 0) {
nl = -1 ; nsl = -1;
}
else {
nl = i;nsl = last;
}
long temp = callfun(num , N-1 , nl , nsl ,newbound, dp);
answer += temp;
if(last != -1 && secondLast != -1 &&((last > secondLast && last > i)||(last < secondLast && last < i))) answer++;
}
return answer;
}
if(dp[N][last][secondLast][bound] != -1) return dp[N][last][secondLast][bound];
long answer = 0l;
int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9;
for(int i =0;i<=max;i++) {
int nl , nsl , newbound = bound==0?0:i==max?1:0;
if(last == - 1&& secondLast == -1 && i == 0) {
nl = -1 ; nsl = -1;
}
else {
nl = i;nsl = last;
}
long temp = callfun(num , N-1 , nl , nsl ,newbound,dp);
answer += temp;
if(last != -1 && secondLast != -1 &&((last > secondLast && last > i)||(last < secondLast && last < i))) answer++;
}
return dp[N][last][secondLast][bound] = answer;
}
public static Long callfun(int index ,int pair,int arr[],Long dp[][]) {
long mod = (long)998244353l;
if(index >= arr.length) return 1l;
if(dp[index][pair] != null) return dp[index][pair];
Long sum = 0l , ans = 0l;
if(arr[index]%2 == pair) {
return dp[index][pair] = callfun(index + 1,pair^1 , arr,dp)%mod + callfun(index + 1 ,pair , arr , dp)%mod;
}
else {
return dp[index][pair] = callfun(index + 1,pair , arr,dp)%mod;
}
// for(int i =index;i<arr.length;i++) {
// sum += arr[i];
// if(sum%2 == pair) {
// ans = ans + callfun(i + 1,pair^1,arr , dp)%mod;
// ans%=mod;
// }
// }
// return dp[index][pair] = ans;
}
public static boolean callfun(int index , int n,int neg , int pos , String s) {
if(neg < 0 || pos < 0) return false;
if(index >= n) return true;
if(s.charAt(0) == 'P') {
if(neg <= 0) return false;
return callfun(index + 1,n , neg-1 , pos , s.charAt(1) + "N");
}
else {
if(pos <= 0) return false;
return callfun(index + 1 , n , neg , pos-1 , s.charAt(1) + "P");
}
}
public static void getPerm(int n , char arr[] , String s ,List<String>list) {
if(n == 0) {
list.add(s);
return;
}
for(char ch : arr) {
getPerm(n-1 , arr , s+ ch,list);
}
}
public static int getLen(int i ,int j , char s[]) {
while(i >= 0 && j < s.length && s[i] == s[j]) {
i--;
j++;
}
i++;
j--;
if(i>j) return 0;
return j-i + 1;
}
public static int getMaxCount(String x) {
char s[] = x.toCharArray();
int max = 0;
int n = s.length;
for(int i =0;i<n;i++) {
max = Math.max(max,Math.max(getLen(i , i,s) , getLen(i ,i+1,s)));
}
return max;
}
public static double getDis(int arr[][] , int x, int y) {
double ans = 0.0;
for(int a[] : arr) {
int x1 = a[0] , y1 = a[1];
ans += Math.sqrt((x-x1)*(x-x1) + (y-y1)*(y-y1));
}
return ans;
}
public static boolean valid(String x ) {
if(x.length() == 0) return true;
if(x.length() == 1) return false;
char s[] = x.toCharArray();
if(x.length() == 2) {
if(s[0] == s[1]) {
return false;
}
return true;
}
int r = 0 , b = 0;
for(char ch : x.toCharArray()) {
if(ch == 'R') r++;
else b++;
}
return (r >0 && b >0);
}
public static long callfun(int day , int k, int limit,int n) {
if(k > limit) return 0;
if(day > n) return 1;
long ans = callfun(day + 1 , k , limit, n)*k + callfun(day + 1,k+1,limit,n)*(k+1);
return ans;
}
public static void primeDivisor(HashMap<Long , Long >cnt , long num) {
for(long i = 2;i*i<=num;i++) {
while(num%i == 0) {
cnt.put(i ,(cnt.getOrDefault(i,0l) + 1));
num /= i;
}
}
if(num > 2) {
cnt.put(num ,(cnt.getOrDefault(num,0l) + 1));
}
}
public static boolean isSubsequene(char a[], char b[] ) {
int i =0 , j = 0;
while(i < a.length && j <b.length) {
if(a[i] == b[j]) {
j++;
}
i++;
}
return j >= b.length;
}
public static long fib(int n ,long M) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
long[][] mat = {{1, 1}, {1, 0}};
mat = pow(mat, n-1 , M);
return mat[0][0];
}
}
public static long[][] pow(long[][] mat, int n ,long M) {
if (n == 1) return mat;
else if (n % 2 == 0) return pow(mul(mat, mat , M), n/2 , M);
else return mul(pow(mul(mat, mat,M), n/2,M), mat , M);
}
static long[][] mul(long[][] p, long[][] q,long M) {
long a = (p[0][0]*q[0][0] + p[0][1]*q[1][0])%M;
long b = (p[0][0]*q[0][1] + p[0][1]*q[1][1])%M;
long c = (p[1][0]*q[0][0] + p[1][1]*q[1][0])%M;
long d = (p[1][0]*q[0][1] + p[1][1]*q[1][1])%M;
return new long[][] {{a, b}, {c, d}};
}
public static long[] kdane(long arr[]) {
int n = arr.length;
long dp[] = new long[n];
dp[0] = arr[0];
long ans = dp[0];
for(int i = 1;i<n;i++) {
dp[i] = Math.max(dp[i-1] + arr[i] , arr[i]);
ans = Math.max(ans , dp[i]);
}
return dp;
}
public static void update(int low , int high , int l , int r, int val , int treeIndex ,int tree[]) {
if(low > r || high < l || high < low) return;
if(l <= low && high <= r) {
System.out.println("At " +low + " and " + high + " ans ttreeIndex " + treeIndex);
tree[treeIndex] += val;
return;
}
int mid = low + (high - low)/2;
update(low , mid , l , r , val , treeIndex*2 + 1, tree);
update(mid + 1 , high , l , r , val , treeIndex*2 + 2 , tree);
}
static int colx[] = {1 ,-1, 0,0 , 1,1,-1,-1};
static int coly[] = {0 ,0, 1,-1,1,-1,1,-1};
public static void reverse(char arr[]) {
int i =0 , j = arr.length-1;
while(i < j) {
swap(i , j , arr);
i++;
j--;
}
}
public static long[] reverse(long arr[]) {
long newans[] = arr.clone();
int i =0 , j = arr.length-1;
while(i < j) {
swap(i , j , newans);
i++;
j--;
}
return newans;
}
public static void reverse(int arr[]) {
int i =0 , j = arr.length-1;
while(i < j) {
swap(i , j , arr);
i++;
j--;
}
}
public static long inverse(long x , long mod) {
return pow(x , mod -2 , mod);
}
public static int maxArray(int arr[]) {
int ans = arr[0] , n = arr.length;
for(int i =1;i<n;i++) {
ans = Math.max(ans , arr[i]);
}
return ans;
}
public static long maxArray(long arr[]) {
long ans = arr[0];
int n = arr.length;
for(int i =1;i<n;i++) {
ans = Math.max(ans , arr[i]);
}
return ans;
}
public static int minArray(int arr[]) {
int ans = arr[0] , n = arr.length;
for(int i =0;i<n;i++ ) {
ans = Math.min(ans ,arr[i]);
}
return ans;
}
public static long minArray(long arr[]) {
long ans = arr[0];
int n = arr.length;
for(int i =0;i<n;i++ ) {
ans = Math.min(ans ,arr[i]);
}
return ans;
}
public static int sumArray(int arr[]) {
int ans = 0;
for(int x : arr) {
ans += x;
}
return ans;
}
public static long sumArray(long arr[]) {
long ans = 0;
for(long x : arr) {
ans += x;
}
return ans;
}
public static long rl() {
return sc.nextLong();
}
public static char[] rac() {
return sc.next().toCharArray();
}
public static String rs() {
return sc.next();
}
public static char rc() {
return sc.next().charAt(0);
}
public static int [] rai(int n) {
int ans[] = new int[n];
for(int i =0;i<n;i++) {
ans[i] = sc.nextInt();
}
return ans;
}
public static long [] ral(int n) {
long ans[] = new long[n];
for(int i =0;i<n;i++) {
ans[i] = sc.nextLong();
}
return ans;
}
public static int ri() {
return sc.nextInt();
}
public static int getValue(int num ) {
int ans = 0;
while(num > 0) {
ans++;
num = num&(num-1);
}
return ans;
}
public static boolean isValid(int x ,int y , int n,char arr[][],boolean visited[][][][]) {
return x>=0 && x<n && y>=0 && y <n && !(arr[x][y] == '#');
}
// public static Pair join(Pair a , Pair b) {
// Pair res = new Pair(Math.min(a.min , b.min) , Math.max(a.max , b.max) , a.count + b.count);
// return res;
// }
// segment tree query over range
// public static int query(int node,int l , int r,int a,int b ,Pair tree[] ) {
// if(tree[node].max < a || tree[node].min > b) return 0;
// if(l > r) return 0;
// if(tree[node].min >= a && tree[node].max <= b) {
// return tree[node].count;
// }
// int mid = l + (r-l)/2;
// int ans = query(node*2 ,l , mid ,a , b , tree) + query(node*2 +1,mid + 1, r , a , b, tree);
// return ans;
// }
// // segment tree update over range
// public static void update(int node, int i , int j ,int l , int r,long value, long arr[] ) {
// if(l >= i && j >= r) {
// arr[node] += value;
// return;
// }
// if(j < l|| r < i) return;
// int mid = l + (r-l)/2;
// update(node*2 ,i ,j ,l,mid,value, arr);
// update(node*2 +1,i ,j ,mid + 1,r, value , arr);
// }
public static long pow(long a , long b , long mod) {
if(b == 1) return a;
if(b == 0) return 1;
long ans = pow(a , b/2 , mod)%mod;
if(b%2 == 0) {
return (ans*ans)%mod;
}
else {
return ((ans*ans)%mod*a)%mod;
}
}
public static long pow(long a , long b ) {
if(b == 1) return a;
if(b == 0) return 1;
long ans = pow(a , b/2);
if(b%2 == 0) {
return (ans*ans);
}
else {
return ((ans*ans)*a);
}
}
public static boolean isVowel(char ch) {
if(ch == 'a' || ch == 'e'||ch == 'i' || ch == 'o' || ch == 'u') return true;
if((ch == 'A' || ch == 'E'||ch == 'I' || ch == 'O' || ch == 'U')) return true;
return false;
}
public static int getFactor(int num) {
if(num==1) return 1;
int ans = 2;
int k = num/2;
for(int i = 2;i<=k;i++) {
if(num%i==0) ans++;
}
return Math.abs(ans);
}
public static int[] readarr()throws IOException {
int n = sc.nextInt();
int arr[] = new int[n];
for(int i =0;i<n;i++) {
arr[i] = sc.nextInt();
}
return arr;
}
public static boolean isPowerOfTwo (long x) {
return x!=0 && ((x&(x-1)) == 0);
}
public static boolean isPrime(long num) {
if(num==1) return false;
if(num<=3) return true;
if(num%2==0||num%3==0) return false;
for(long i =5;i*i<=num;i+=6) {
if(num%i==0 || num%(i+2) == 0) return false;
}
return true;
}
public static boolean isPrime(int num) {
// System.out.println("At pr " + num);
if(num==1) return false;
if(num<=3) return true;
if(num%2==0||num%3==0) return false;
for(int i =5;i*i<=num;i+=6) {
if(num%i==0 || num%(i+2) == 0) return false;
}
return true;
}
// public static boolean isPrime(long num) {
// if(num==1) return false;
// if(num<=3) return true;
// if(num%2==0||num%3==0) return false;
// for(int i =5;i*i<=num;i+=6) {
// if(num%i==0) return false;
// }
// return true;
// }
public static long gcd(long a , long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
public static int gcd(int a , int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
public static int get_gcd(int a , int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
public static long get_gcd(long a , long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
// public static long fac(long num) {
// long ans = 1;
// int mod = (int)1e9+7;
// for(long i = 2;i<=num;i++) {
// ans = (ans*i)%mod;
// }
// return 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 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 | dc3c3020cbf707a429dd2248e64d4902 | 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 | /*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
//package codeforces;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
*
* @author Kaiyuan
*/
public class GameOfBallPassing {
public static void main(String[] args) {
Kattio io = new Kattio();
int test = io.nextInt();
for (int tests = 0; tests < test; tests++) {
int n = io.nextInt();
int x = io.nextInt();
int[] sums = new int[n+1];
sums[0] = 0;
sums[1] = io.nextInt();
for (int i = 2; i < n+1; i++) {
sums[i] = io.nextInt() + sums[i-1];
}
int[] bests = new int[n + 1];
//
for (int i = 0; i <= n; i++) {
// I = length
int bst = 0;
for (int j = i; j <= n; j++) {
// (from 0, i -> n-i-1 -> n-i-1)
int sum = 0;
sum = sums[j] - sums[j-i];
if (sum < 0 && bst == 0) {
bst = sum;
}
bst = Math.max(sum, bst);
}
bests[i] = bst;
}
//
String line = "";
for (int k = 0; k <= n; k++) {
int out = 0;
for (int i = 0; i <= n; i++) {
int temp = bests[i] + Math.min(i, k) * x;
out = Math.max(out, temp);
}
line += out + " ";
}
System.out.println(line.trim());
}
}
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(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 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 | d63de737e5b8da942d9efe3f0035a12d | 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.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class Template {
public static void main(String[] args) {
FastScanner scan=new FastScanner();
int t = scan.nextInt();
while(t-->0) {
int n=scan.nextInt();
int k=scan.nextInt();
int[] arr = scan.readArray(n);
int[] prefixSum=new int[n+1];
prefixSum[0]=0;
for(int i=1;i<n+1;i++){
prefixSum[i]=arr[i-1]+prefixSum[i-1];
}
int[] lengths= new int[n];
for(int x=1;x<=n;x++){
int high=Integer.MIN_VALUE;
for(int i=0;i+x<=n;i++){
int sum=prefixSum[i+x]-prefixSum[i];
if(sum>high)
high=sum;
}
lengths[x-1]=high;
}
for(int x=0;x<=n;x++){
int high=0;
for(int i=0;i<n;i++){
int sum=lengths[i]+Math.min(i+1,x)*k;
if(sum>high) high=sum;
}
if(x>0) System.out.print(" ");
System.out.print(high);
}
System.out.println();
}
}
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n=a.length; //shuffle, then sort for
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 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 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 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 | 17afc76ddb439e5ea751b06d6d6c0756 | 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.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;
public class cses{
static int[] arr;
public static void main(String[] args) {
int t = io.nextInt();
for (int i = 0; i < t; i++) {
int n = io.nextInt();
int x = io.nextInt();
arr = new int[n];
long[] pre = new long[n];
for (int j = 0; j < n; j++) {
arr[j] = io.nextInt();
if(j > 0)
pre[j] = pre[j-1] + arr[j];
else
pre[j] = arr[j];
}
int[] sub = new int[n+1];//max sum of length i
sub[0] = 0;
for (int j = 1; j <= n; j++) {//length
sub[j] = (int) pre[j-1];
for (int k = j; k <= n-1; k++) {
sub[j] = (int) Math.max(sub[j], pre[k] - pre[k-j]);
}
}
int[] ans = new int[n + 1];
for (int j = 0; j < n + 1; j++) {//update j things
for (int k = 0; k < n + 1; k++) {//length of arrays
ans[j] = Math.max(Math.min(j, k) * x + sub[k], ans[j]);
}
}
for (int j = 0; j < ans.length; j++) {
if(j != ans.length -1){
System.out.print(ans[j] + " ");
}
else
System.out.println(ans[j]);
}
}
}
static Kattio29 io = new Kattio29();
static class Kattio29 extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio29() {
this(System.in, System.out);
}
public Kattio29(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio29(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 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 | 80586201797c18b6f26e3bf6cd5f2b03 | 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 java.math.BigInteger;
public final class code {
static class sortCond implements Comparator<Pair<Integer, Pair<Long, Long>>> {
@Override
public int compare(Pair<Integer, Pair<Long, Long>> p1, Pair<Integer, Pair<Long, Long>> p2) {
if (p1.b.b <= p2.b.b) {
return -1;
} else {
return 1;
}
}
}
static class sortCond1 implements Comparator<Pair<Integer, Pair<Long, Long>>> {
@Override
public int compare(Pair<Integer, Pair<Long, Long>> p1, Pair<Integer, Pair<Long, Long>> p2) {
if (p1.b.a <= p2.b.a) {
return -1;
} else {
return 1;
}
}
}
static class Pair<f, s> {
f a;
s b;
Pair(f a, s b) {
this.a = a;
this.b = b;
}
}
interface modOperations {
int mod(int a, int b, int mod);
}
static int findBinaryExponentian(int a, int pow, int mod) {
if (pow == 1) {
return a;
} else if (pow == 0) {
return 1;
} else {
int retVal = findBinaryExponentian(a, (int) pow / 2, mod);
return modMul.mod(modMul.mod(retVal, retVal, mod), (pow % 2 == 0) ? 1 : a, mod);
}
}
static int findPow(int a, int b) {
if (b == 1) {
return a;
} else if (b == 0) {
return 1;
} else {
int res = findPow(a, (int) b / 2);
return res * res * (b % 2 == 1 ? a : 1);
}
}
static int bleft(long ele, ArrayList<Long> sortedArr) {
int l = 0;
int h = sortedArr.size() - 1;
int ans = -1;
while (l <= h) {
int mid = l + (int) (h - l) / 2;
if (sortedArr.get(mid) < ele) {
l = mid + 1;
} else if (sortedArr.get(mid) >= ele) {
ans = mid;
h = mid - 1;
}
}
return ans;
}
static long gcd(long a, long b) {
long div = b;
long rem = a % b;
while (rem != 0) {
long temp = rem;
rem = div % rem;
div = temp;
}
return div;
}
static int log(long no) {
int i = 0;
while ((1 << i) <= no) {
i += 1;
}
return i - 1;
}
static modOperations modAdd = (int a, int b, int mod) -> {
return (a % mod + b % mod) % mod;
};
static modOperations modSub = (int a, int b, int mod) -> {
return (a % mod - b % mod + mod) % mod;
};
static modOperations modMul = (int a, int b, int mod) -> {
return (a % mod * b % mod) % mod;
};
static modOperations modDiv = (int a, int b, int mod) -> {
return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod);
};
static ArrayList<Integer> primeList(int MAXI) {
int[] prime = new int[MAXI + 1];
ArrayList<Integer> obj = new ArrayList<Integer>();
for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) {
if (prime[i] == 0) {
obj.add(i);
for (int j = i * i; j <= MAXI; j += i) {
prime[j] = 1;
}
}
}
for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) {
if (prime[i] == 0) {
obj.add(i);
}
}
return obj;
}
static int[] factorialList(int MAXI, int mod) {
int[] factorial = new int[MAXI + 1];
factorial[0] = 0 % mod;
factorial[1] = 1 % mod;
for (int i = 2; i < MAXI + 1; i++) {
factorial[i] = modMul.mod(factorial[i - 1], i, mod);
}
return factorial;
}
static long arrSum(ArrayList<Long> arr) {
long tot = 0;
for (int i = 0; i < arr.size(); i++) {
tot += arr.get(i);
}
return tot;
}
static int ord(char b) {
return (int) b - (int) 'a' + 1;
}
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int cases = Integer.parseInt(br.readLine()), n, x, i, j;
while (cases-- != 0) {
String a[] = br.readLine().split(" ");
n = Integer.parseInt(a[0]);
x = Integer.parseInt(a[1]);
int arr[] = new int[n];
int pre[] = new int[n];
int recs[] = new int[n + 1];
a = br.readLine().split(" ");
for (i = 0; i < n; i++) {
arr[i] = Integer.parseInt(a[i]);
}
for (i = 0; i < n + 1; i++) {
recs[i] = -500000000 + 1;
}
pre[0] = arr[0];
recs[0] = 0;
for (i = 1; i < n; i++) {
pre[i] = pre[i - 1] + arr[i];
}
for (i = 0; i < n; i++) {
for (j = i; j < n; j++) {
recs[j - i + 1] = Math.max(recs[j - i + 1], pre[j] - ((i - 1) >= 0 ? pre[i - 1] : 0));
}
}
for (i = n - 1; i >= 0; i--) {
recs[i] = Math.max(recs[i + 1], recs[i]);
}
int max = -500000000 - x - 1;
for (i = 0; i < n + 1; i++) {
if (max < recs[i] + i * x) {
bw.write(Integer.toString(recs[i] + i * x) + " ");
} else {
bw.write(Integer.toString(max) + " ");
}
max = Math.max(max, recs[i] + i * x);
}
bw.write("\n");
}
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 | 69d9a6eb6b55fd5080796a40e6403ab7 | 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 java.math.BigInteger;
public final class code {
static class sortCond implements Comparator<Pair<Integer, Pair<Long, Long>>> {
@Override
public int compare(Pair<Integer, Pair<Long, Long>> p1, Pair<Integer, Pair<Long, Long>> p2) {
if (p1.b.b <= p2.b.b) {
return -1;
} else {
return 1;
}
}
}
static class sortCond1 implements Comparator<Pair<Integer, Pair<Long, Long>>> {
@Override
public int compare(Pair<Integer, Pair<Long, Long>> p1, Pair<Integer, Pair<Long, Long>> p2) {
if (p1.b.a <= p2.b.a) {
return -1;
} else {
return 1;
}
}
}
static class Pair<f, s> {
f a;
s b;
Pair(f a, s b) {
this.a = a;
this.b = b;
}
}
interface modOperations {
int mod(int a, int b, int mod);
}
static int findBinaryExponentian(int a, int pow, int mod) {
if (pow == 1) {
return a;
} else if (pow == 0) {
return 1;
} else {
int retVal = findBinaryExponentian(a, (int) pow / 2, mod);
return modMul.mod(modMul.mod(retVal, retVal, mod), (pow % 2 == 0) ? 1 : a, mod);
}
}
static int findPow(int a, int b) {
if (b == 1) {
return a;
} else if (b == 0) {
return 1;
} else {
int res = findPow(a, (int) b / 2);
return res * res * (b % 2 == 1 ? a : 1);
}
}
static int bleft(long ele, ArrayList<Long> sortedArr) {
int l = 0;
int h = sortedArr.size() - 1;
int ans = -1;
while (l <= h) {
int mid = l + (int) (h - l) / 2;
if (sortedArr.get(mid) < ele) {
l = mid + 1;
} else if (sortedArr.get(mid) >= ele) {
ans = mid;
h = mid - 1;
}
}
return ans;
}
static long gcd(long a, long b) {
long div = b;
long rem = a % b;
while (rem != 0) {
long temp = rem;
rem = div % rem;
div = temp;
}
return div;
}
static int log(long no) {
int i = 0;
while ((1 << i) <= no) {
i += 1;
}
return i - 1;
}
static modOperations modAdd = (int a, int b, int mod) -> {
return (a % mod + b % mod) % mod;
};
static modOperations modSub = (int a, int b, int mod) -> {
return (a % mod - b % mod + mod) % mod;
};
static modOperations modMul = (int a, int b, int mod) -> {
return (a % mod * b % mod) % mod;
};
static modOperations modDiv = (int a, int b, int mod) -> {
return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod);
};
static ArrayList<Integer> primeList(int MAXI) {
int[] prime = new int[MAXI + 1];
ArrayList<Integer> obj = new ArrayList<Integer>();
for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) {
if (prime[i] == 0) {
obj.add(i);
for (int j = i * i; j <= MAXI; j += i) {
prime[j] = 1;
}
}
}
for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) {
if (prime[i] == 0) {
obj.add(i);
}
}
return obj;
}
static int[] factorialList(int MAXI, int mod) {
int[] factorial = new int[MAXI + 1];
factorial[0] = 0 % mod;
factorial[1] = 1 % mod;
for (int i = 2; i < MAXI + 1; i++) {
factorial[i] = modMul.mod(factorial[i - 1], i, mod);
}
return factorial;
}
static long arrSum(ArrayList<Long> arr) {
long tot = 0;
for (int i = 0; i < arr.size(); i++) {
tot += arr.get(i);
}
return tot;
}
static int ord(char b) {
return (int) b - (int) 'a' + 1;
}
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int cases = Integer.parseInt(br.readLine()), n, x, i, j;
while (cases-- != 0) {
String a[] = br.readLine().split(" ");
n = Integer.parseInt(a[0]);
x = Integer.parseInt(a[1]);
a = br.readLine().split(" ");
int arr[] = new int[n];
for (i = 0; i < n; i++) {
arr[i] = Integer.parseInt(a[i]);
}
int[] recs = new int[n + 1];
int[] pre = new int[n];
pre[0] = arr[0];
for (i = 1; i < n; i++) {
pre[i] = pre[i - 1] + arr[i];
}
for (i = 0; i < n + 1; i++) {
recs[i] = -500000000 - 1;
}
recs[0] = 0;
for (i = 0; i < n; i++) {
for (j = i; j < n; j++) {
recs[j - i + 1] = Math.max(recs[j - i + 1], pre[j] - ((i - 1) >= 0 ? pre[i - 1] : 0));
}
}
for (i = n - 1; i >= 0; i--) {
recs[i] = Math.max(recs[i + 1], recs[i]);
}
int max = -500000000 - x - 1;
for (i = 0; i < n + 1; i++) {
if (max < recs[i] + i * x) {
bw.write(Integer.toString(recs[i] + i * x) + " ");
} else {
bw.write(Integer.toString(max) + " ");
}
max = Math.max(max, recs[i] + i * x);
}
bw.write("\n");
}
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 | 8aed5444045dc1453eb8923df5cf0016 | 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 | // C. Increase Subarray Sums
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import static java.lang.Math.*;
public class Main {
static PrintWriter out;
public static void main(String[] args) {
FastReader in = new FastReader();
out = new PrintWriter(System.out);
long t = in.nextLong();
long test = 1;
while (test <= t) {
// out.println(solve(in));
solve(in);
test++;
}
out.close();
}
/*--------------------------------------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------------------------*/
private static void solve(FastReader in) {
int n = in.nextInt();
int x = in.nextInt();
int a[] = in.readArray(n);
long dp[] = new long[n + 1];
Arrays.fill(dp, -(long) 1e12);
for (int i = 0; i < n; i++) {
long val = 0;
for (int j = i; j < n; j++) {
val += a[j];
dp[j - i + 1] = max(dp[j - i + 1], val);
}
}
// out.println(Arrays.toString(dp));
for (int i = 0; i <= n; i++) {
long res = 0;
for (int j = 1; j <= n; j++) {
res = max(res, dp[j] + (min(i, j) * (long) x));
}
out.print(res + " ");
}
out.println();
}
// private static long solve(FastReader in) {
//
//
// }
// private static String solve(FastReader in) {
//
//
// }
/*--------------------------------------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------------------------*/
private static int gcd(int a, int b) {
return b == 0 ? a : (gcd(b, a % b));
}
static boolean[] sieveOfEratosthenes(int n) {
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
static void sort(int[] a) {
List<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 long modPow(long a, long b, long m) {
long res = 1;
a %= m;
while (b > 0) {
if ((b & 1) != 0) {
res = res * a;
res %= m;
}
b >>= 1;
a *= a;
a %= m;
}
return res;
}
private static class Pair implements Comparable<Pair> {
int ff, ss;
Pair(int x, int y) {
this.ff = x;
this.ss = y;
}
public int compareTo(Pair o) {
return this.ff == o.ff ? this.ss - o.ss : this.ff - o.ff;
}
}
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[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
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 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 | 6fdb9889c58008358d4d81f19e25806e | 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 C1644{
static int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
int k;
for (k = 0; ((a | b) & 1) == 0; ++k)
{
a >>= 1;
b >>= 1;
}
while ((a & 1) == 0)
a >>= 1;
do {
while ((b & 1) == 0)
b >>= 1;
if (a > b)
{
int temp = a;
a = b;
b = temp;
}
b = (b - a);
} while (b != 0);
return a << k;
}
static int lcm(int a,int b){
return a*b/gcd(a,b);
}
public static int binary(int arr[],int n, int a,int beg,int end){
while(beg<=end){
int mid=beg+(end-beg)/2;
if(arr[mid]==a)
return mid;
if(arr[mid]<a)
end=mid-1;
else
beg=mid+1;
}
return -1;
}
public static int binaryless(int arr[],int n, int a,int beg,int end){
while(beg<=end){
int mid=beg+(end-beg)/2;
if(arr[mid]>=a){
if(mid==1||arr[mid-1]<a)
return mid;
end=mid-1;
}
else{
beg=mid+1;
}
}
return -1;
}
public static void sieve(int t){
boolean srr[]=new boolean[t+1];
for(int i=2;i*i<=t;i++)
{
if(!srr[i])
{
for(int k=i*i;k<=t;k+=i)
srr[k]=true;
}
}
for(int i=2;i<t+1;i++){
if(!srr[i])
System.out.println(i);
}
}
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if((y & 1)==1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static class Custom implements Comparable<Custom>{
int d;
int f;
public Custom(int d,int f){
this.d=d;
this.f=f;
}
public int compareTo(Custom t){
return this.d-t.d;
}
}
void solve() throws IOException {
int t=nextInt();
while(t--!=0){
int n=nextInt();
int x=nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=nextInt();
}
int sum=0;
int mx[] = new int[n+1];
for(int i=0;i<n;i++){
sum+=arr[i];
mx[i+1]+=sum;
}
for(int i=1;i<=n;i++){
sum = mx[i];
for(int j=i;j<n;j++){
sum+=(arr[j]-arr[j-i]);
mx[i]=Math.max(mx[i],sum);
}
}
for(int i=0;i<=n;i++){
int val=0;
for(int j=0;j<=n;j++){
val = Math.max(val, mx[j] + x*Math.min(j,i));
}
pw.print(val+" ");
}
pw.println();
}
}
void disp(Object a){
System.out.print(a+" ");
}
void displn(Object a){
System.out.println(a);
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
String nextLine()throws IOException{
return br.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
StringTokenizer st;
BufferedReader br;
PrintWriter pw;
Scanner sc;
void run() throws IOException {
sc=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
long time = System.currentTimeMillis();
solve();
System.err.println("time = " + (System.currentTimeMillis() - time));
pw.close();
}
public static void main(String[] args) throws IOException {
new C1644().run();
}
} | 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 | 19bd055aae14286e1a662860220f8fe4 | 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 HelloWorld{
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[]arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int []ans=new int[n+1];
for(int i=0;i<=n;i++){
ans[i]=Integer.MIN_VALUE;
}
ans[0]=0;
for(int i=0;i<n;i++)
{
int temp=0;
for(int j=i;j<n;j++)
{
temp+=arr[j];
ans[j-i+1]=Math.max(ans[j-i+1],temp);
}
}
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+" ");
}
// for(int i=0;i<=n;i++){
// //for(int j=0;j<n;j++){
// 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 | a1836f9f18b2447d8d49f3ae16aa1901 | 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 MyScanner sc = new MyScanner();
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int x = sc.nextInt();
long a[] = new long[n];
for(int i = 0; i<n; i++) {
a[i] = sc.nextInt();
}
long b[] = new long[n+1];
Arrays.fill(b, Long.MIN_VALUE);
for(int i = 0; i<n; i++) {
long sum = 0;
for(int j = i; j<n; j++) {
sum += a[j];
int len = (j-i)+1;
b[len] = Math.max(b[len], sum);
}
}
// for(long e : b) {
// pw.print(e+" ");
// }
// pw.println();
for(int k = 0; k<=n; k++) {
long max = 0;
for(int i = 0; i<b.length; i++) {
if(i<k) {
max = Math.max(max, b[i]+i*x);
}
else {
max = Math.max(max, b[i]+k*x);
}
}
pw.print(max+" ");
}
pw.println();
}
pw.close();
}
static void ruffleSort(int a[]) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < n; i++) {
int oi = r.nextInt(n);
int temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long a[]) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < n; i++) {
int oi = r.nextInt(n);
long temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
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 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 | e73a89050d46db74325bd8283dd5df42 | 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 GFG {
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;
}
}
public static void main (String[] args) {
FastReader sc=new FastReader();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int x=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int dp[]=new int[n+1];
Arrays.fill(dp,Integer.MIN_VALUE);
for(int i = 0; i < n; i++) {
int val = 0;
for(int j = i; j < n; j++) {
val += arr[j];
dp[j - i + 1] = Math.max(dp[j - i + 1], val);
}
}
for(int i = 0; i <= n; i++) {
int ans = 0;
for(int j = 1; j <= n; j++) {
ans = Math.max(ans, dp[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 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 | d8d5f84d3a3250e4a29b38090e1b2525 | 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.text.*;
import java.math.*;
import java.util.regex.*;
public class JaiShreeRam{
static Scanner in=new Scanner();
static long mod = 1000000007;
static List<List<Integer>> adj;
static int seive[]=new int[1000001];
static long C[][];
public static void main(String[] args) throws Exception{
int z=in.readInt();
while(z-->0) {
solve();
}
}
static void solve() {
int n=in.readInt();
long x=in.readLong();
long a[]=nla(n);
long k[]=new long[n+1];
Arrays.fill(k, Integer.MIN_VALUE);
k[0]=0;
for(int i=0;i<n;i++) {
long sum=0;
for(int j=i;j<n;j++) {
sum+=a[j];
k[j-i+1]=Math.max(k[j-i+1], sum);
}
}
//print(k);
long ans[]=new long[n+1];
for(int i=0;i<n+1;i++) {
for(int j=0;j<=n;j++) {
ans[i]=Math.max(k[j]+x*Math.min(i, j),ans[i]);
}
}
print(ans);
}
static void ncr(int n, int k){
C= new long[n + 1][k + 1];
int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= Math.min(i, k); j++) {
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
}
static boolean isPalin(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;
}
static int knapsack(int W, int wt[],int val[], int n){
int []dp = new int[W + 1];
for (int i = 1; i < n + 1; i++) {
for (int w = W; w >= 0; w--) {
if (wt[i - 1] <= w) {
dp[w] = Math.max(dp[w],dp[w - wt[i - 1]] + val[i - 1]);
}
}
}
return dp[W];
}
static void seive() {
Arrays.fill(seive, 1);
seive[0]=0;
seive[1]=0;
for(int i=2;i*i<1000001;i++) {
if(seive[i]==1) {
for(int j=i*i;j<1000001;j+=i) {
if(seive[j]==1) {
seive[j]=0;
}
}
}
}
}
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 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 int[] nia(int n){
int[] arr= new int[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static long[] nla(int n){
long[] arr= new long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static int[] nia1(int n){
int[] arr= new int[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readInt();
}
return arr;
}
static Integer[] nIa(int n){
Integer[] arr= new Integer[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static Long[] nLa(int n){
Long[] arr= new Long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static long gcd(long a, long b) {
if (b==0) return a;
return gcd(b, a%b);
}
static void print(long i) {
System.out.println(i);
}
static void print(Object o) {
System.out.println(o);
}
static void print(int a[]) {
for(int i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long a[]) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(ArrayList<Long> a) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(Object a[]) {
for(Object i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static class Scanner{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String readString() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
double readDouble() {
return Double.parseDouble(readString());
}
int readInt() {
return Integer.parseInt(readString());
}
long readLong() {
return Long.parseLong(readString());
}
}
} | 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 | 8fad02726c5f263e0948e38112940aa0 | 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 | /*LoudSilence*/
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Solution {
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static FastScanner s = new FastScanner();
static FastWriter out = new FastWriter();
final static int mod = (int)1e9 + 7;
final static int INT_MAX = Integer.MAX_VALUE;
final static int INT_MIN = Integer.MIN_VALUE;
final static long LONG_MAX = Long.MAX_VALUE;
final static long LONG_MIN = Long.MIN_VALUE;
final static double DOUBLE_MAX = Double.MAX_VALUE;
final static double DOUBLE_MIN = Double.MIN_VALUE;
final static float FLOAT_MAX = Float.MAX_VALUE;
final static float FLOAT_MIN = Float.MIN_VALUE;
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static class FastScanner{BufferedReader br;StringTokenizer st;
public FastScanner() {if(System.getProperty("ONLINE_JUDGE") == null){try {br = new BufferedReader(new FileReader("E:\\Competitive Coding\\input.txt"));}
catch (FileNotFoundException e) {br = new BufferedReader(new InputStreamReader(System.in));}}else{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());}
List<Integer> readIntList(int n){List<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextInt()); return arr;}
List<Long> readLongList(int n){List<Long> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextLong()); return arr;}
int[] readIntArr(int n){int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.nextInt(); return arr;}
long[] readLongArr(int n){long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = s.nextLong(); return arr;}
String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}}
static class FastWriter{private BufferedWriter bw;public FastWriter(){if(System.getProperty("ONLINE_JUDGE") == null){try {this.bw = new BufferedWriter(new FileWriter("E:\\Competitive Coding\\output.txt"));}
catch (IOException e) {this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}}else{this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}}
public void print(Object object) throws IOException{bw.append(""+ object);}
public void println(Object object) throws IOException{print(object);bw.append("\n");}
public void debug(int object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");}
public void debug(long object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");}
public void close() throws IOException{bw.close();}}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
public static ArrayList<Integer> seive(int n){ArrayList<Integer> list = new ArrayList<>();int arr[] = new int[n+1];for(long i = 2; i <= n; i++) {if(arr[(int)i] == 1) {continue;}else {list.add((int)i);for(long j = i*i; j <= n; j = j + i) {arr[(int)j] = 1;}}}return list;}
public static long gcd(long a, long b){if(a > b) {a = (a+b)-(b=a);}if(a == 0L){return b;}return gcd(b%a, a);}
public static void swap(int[] arr, int i, int j) {arr[i] = arr[i] ^ arr[j]; arr[j] = arr[j] ^ arr[i]; arr[i] = arr[i] ^ arr[j];}
public static boolean isPrime(long n){if(n < 2){return false;}if(n == 2 || n == 3){return true;}if(n%2 == 0 || n%3 == 0){return false;}long sqrtN = (long)Math.sqrt(n)+1;for(long i = 6L; i <= sqrtN; i += 6) {if(n%(i-1) == 0 || n%(i+1) == 0) return false;}return true;}
public static long mod_add(long a, long b){ return (a%mod + b%mod)%mod;}
public static long mod_sub(long a, long b){ return (a%mod - b%mod + mod)%mod;}
public static long mod_mul(long a, long b){ return (a%mod * b%mod)%mod;}
public static long modInv(long a, long b){ return expo(a, b-2)%b;}
public static long mod_div(long a, long b){return mod_mul(a, modInv(b, mod));}
public static long expo (long a, long n){if(n == 0){return 1;}long recAns = expo(mod_mul(a,a), n/2);if(n % 2 == 0){return recAns;}else{return mod_mul(a, recAns);}}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// Pair class
public static class Pair<X extends Comparable<X>,Y extends Comparable<Y>> implements Comparable<Pair<X, Y>>{
X first;
Y second;
public Pair(X first, Y second){
this.first = first;
this.second = second;
}
public String toString(){
return "( " + first+" , "+second+" )";
}
@Override
public int compareTo(Pair<X, Y> o) {
int t = first.compareTo(o.first);
if(t == 0) return second.compareTo(o.second);
return t;
}
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// Code begins
public static void solve() throws IOException {
int n = s.nextInt(), x = s.nextInt();
long arr[] = s.readLongArr(n);
HashMap<Integer, Long> map = new HashMap<>();
for(int k = 1; k <= n; k++){
long mS = maxSum(arr, k);
map.put(k, mS);
}
long maxSum = INT_MIN;
for(int k = 0; k <= n; k++){
for(int len: map.keySet()){
long sum = map.get(len);
long curr = sum + (long)min(len, k) * (long)x;
maxSum = max(curr, maxSum);
}
out.print((maxSum < 0 ? 0 : maxSum)+" ");
maxSum = INT_MIN;
}
out.println("");
}
public static long maxSum(long arr[], int k) {
int n = arr.length, s = 0, e = 0;
long maxSum = INT_MIN;
int curr = 0;
while(e < n){
curr += arr[e];
if(e-s+1 < k){
e++;
}else{
maxSum = max(curr, maxSum);
curr -= arr[s];
s++;
e++;
}
}
return maxSum;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
public static void main(String[] args) throws IOException {
int test = s.nextInt();
for(int t = 1; t <= test; t++) {
solve();
}
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 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 | c3643f05e93c9909c163d50916a61a48 | 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 | /*LoudSilence*/
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Solution {
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static FastScanner s = new FastScanner();
static FastWriter out = new FastWriter();
final static int mod = (int)1e9 + 7;
final static int INT_MAX = Integer.MAX_VALUE;
final static int INT_MIN = Integer.MIN_VALUE;
final static long LONG_MAX = Long.MAX_VALUE;
final static long LONG_MIN = Long.MIN_VALUE;
final static double DOUBLE_MAX = Double.MAX_VALUE;
final static double DOUBLE_MIN = Double.MIN_VALUE;
final static float FLOAT_MAX = Float.MAX_VALUE;
final static float FLOAT_MIN = Float.MIN_VALUE;
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static class FastScanner{BufferedReader br;StringTokenizer st;
public FastScanner() {if(System.getProperty("ONLINE_JUDGE") == null){try {br = new BufferedReader(new FileReader("E:\\Competitive Coding\\input.txt"));}
catch (FileNotFoundException e) {br = new BufferedReader(new InputStreamReader(System.in));}}else{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());}
List<Integer> readIntList(int n){List<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextInt()); return arr;}
List<Long> readLongList(int n){List<Long> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextLong()); return arr;}
int[] readIntArr(int n){int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.nextInt(); return arr;}
long[] readLongArr(int n){long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = s.nextLong(); return arr;}
String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}}
static class FastWriter{private BufferedWriter bw;public FastWriter(){if(System.getProperty("ONLINE_JUDGE") == null){try {this.bw = new BufferedWriter(new FileWriter("E:\\Competitive Coding\\output.txt"));}
catch (IOException e) {this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}}else{this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}}
public void print(Object object) throws IOException{bw.append(""+ object);}
public void println(Object object) throws IOException{print(object);bw.append("\n");}
public void debug(int object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");}
public void debug(long object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");}
public void close() throws IOException{bw.close();}}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
public static ArrayList<Integer> seive(int n){ArrayList<Integer> list = new ArrayList<>();int arr[] = new int[n+1];for(long i = 2; i <= n; i++) {if(arr[(int)i] == 1) {continue;}else {list.add((int)i);for(long j = i*i; j <= n; j = j + i) {arr[(int)j] = 1;}}}return list;}
public static long gcd(long a, long b){if(a > b) {a = (a+b)-(b=a);}if(a == 0L){return b;}return gcd(b%a, a);}
public static void swap(int[] arr, int i, int j) {arr[i] = arr[i] ^ arr[j]; arr[j] = arr[j] ^ arr[i]; arr[i] = arr[i] ^ arr[j];}
public static boolean isPrime(long n){if(n < 2){return false;}if(n == 2 || n == 3){return true;}if(n%2 == 0 || n%3 == 0){return false;}long sqrtN = (long)Math.sqrt(n)+1;for(long i = 6L; i <= sqrtN; i += 6) {if(n%(i-1) == 0 || n%(i+1) == 0) return false;}return true;}
public static long mod_add(long a, long b){ return (a%mod + b%mod)%mod;}
public static long mod_sub(long a, long b){ return (a%mod - b%mod + mod)%mod;}
public static long mod_mul(long a, long b){ return (a%mod * b%mod)%mod;}
public static long modInv(long a, long b){ return expo(a, b-2)%b;}
public static long mod_div(long a, long b){return mod_mul(a, modInv(b, mod));}
public static long expo (long a, long n){if(n == 0){return 1;}long recAns = expo(mod_mul(a,a), n/2);if(n % 2 == 0){return recAns;}else{return mod_mul(a, recAns);}}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// Pair class
public static class Pair<X extends Comparable<X>,Y extends Comparable<Y>> implements Comparable<Pair<X, Y>>{
X first;
Y second;
public Pair(X first, Y second){
this.first = first;
this.second = second;
}
public String toString(){
return "( " + first+" , "+second+" )";
}
@Override
public int compareTo(Pair<X, Y> o) {
int t = first.compareTo(o.first);
if(t == 0) return second.compareTo(o.second);
return t;
}
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// Code begins
public static void solve() throws IOException {
int n = s.nextInt(), x = s.nextInt();
long arr[] = s.readLongArr(n);
HashMap<Integer, Long> map = new HashMap<>();
for(int k = 1; k <= n; k++){
long mS = maxSum(arr, k);
map.put(k, mS);
}
long maxSum = INT_MIN;
for(int k = 0; k <= n; k++){
for(int len: map.keySet()){
long sum = map.get(len);
long curr = sum + (long)min(len, k) * (long)x;
maxSum = max(curr, maxSum);
}
out.print((maxSum < 0 ? 0 : maxSum)+" ");
maxSum = INT_MIN;
}
out.println("");
}
public static long maxSum(long arr[], int k) {
int n = arr.length, s = 0, e = 0;
long maxSum = INT_MIN;
int curr = 0;
while(e < n){
curr += arr[e];
if(e-s+1 < k){
e++;
}else{
maxSum = max(curr, maxSum);
curr -= arr[s];
s++;
e++;
}
}
return maxSum;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
public static void main(String[] args) throws IOException {
int test = s.nextInt();
for(int t = 1; t <= test; t++) {
solve();
}
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 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 | 1abd4f8c032e050eefd713ad112e6cb1 | 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 : Ryan Ranaut
__Hope is a big word, never lose it__
------------- --------------*/
import java.io.*;
import java.util.*;
public class Codeforces2 {
static PrintWriter out = new PrintWriter(System.out);
static final int mod = 1_000_000_007;
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());
}
int[] readIntArray(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());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
/*-------------------------------------------------------------------------*/
//Try seeing general case
public static void main(String[] args) {
FastReader s = new FastReader();
int t = s.nextInt();
while(t-->0)
{
int n = s.nextInt();
long x = s.nextLong();
long[] a = s.readLongArray(n);
find(n, x, a);
}
out.close();
}
public static void find(int n, long x, long[] a)
{
long[][] aa = new long[n+1][n];
for(long[] r: aa)
Arrays.fill(r, Long.MIN_VALUE);
for(int j=0;j<n;j++)
{
long sum = 0;
int row = 1;
for(int i=j;i<n;i++)
{
sum += a[i];
aa[row++][j] = sum;
}
}
//debugMatrix(aa);
long[] dp = new long[n+1];
dp[n] = aa[n][0];
for(int i=n-1;i>=0;i--)
{
long max = Long.MIN_VALUE;
for(int j=0;j<n;j++)
max = Math.max(max, aa[i][j]);
dp[i] = Math.max(dp[i+1], max);
}
//debug(dp);
long res = Math.max(dp[0], 0);
out.print(res+" ");
for(int k=1;k<=n;k++)
{
res = Math.max(res, k*x + dp[k]);
out.print(res+" ");
}
out.println();
}
/*-----------------------------------End of the road--------------------------------------*/
static class DSU {
int[] parent;
int[] ranks;
int[] groupSize;
int size;
public DSU(int n) {
size = n;
parent = new int[n];//0 based
ranks = new int[n];
groupSize = new int[n];//Size of each component
for (int i = 0; i < n; i++) {
parent[i] = i;
ranks[i] = 1; groupSize[i] = 1;
}
}
public int find(int x)//Path Compression
{
if (parent[x] == x)
return x;
else
return parent[x] = find(parent[x]);
}
public void union(int x, int y)//Union by rank
{
int x_rep = find(x);
int y_rep = find(y);
if (x_rep == y_rep)
return;
if (ranks[x_rep] < ranks[y_rep])
{
parent[x_rep] = y_rep;
groupSize[y_rep] += groupSize[x_rep];
}
else if (ranks[x_rep] > ranks[y_rep])
{
parent[y_rep] = x_rep;
groupSize[x_rep] += groupSize[y_rep];
}
else {
parent[y_rep] = x_rep;
ranks[x_rep]++;
groupSize[x_rep] += groupSize[y_rep];
}
size--;//Total connected components
}
}
public static int gcd(int x,int y)
{
return y==0?x:gcd(y,x%y);
}
public static long gcd(long x,long y)
{
return y==0L?x:gcd(y,x%y);
}
public static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static long pow(long a,long b)
{
if(b==0L)
return 1L;
long tmp=1;
while(b>1L)
{
if((b&1L)==1)
tmp*=a;
a*=a;
b>>=1;
}
return (tmp*a);
}
public static long modPow(long a,long b,long mod)
{
if(b==0L)
return 1L;
long tmp=1;
while(b>1L)
{
if((b&1L)==1L)
tmp*=a;
a*=a;
a%=mod;
tmp%=mod;
b>>=1;
}
return (tmp*a)%mod;
}
static long mul(long a, long b) {
return a*b%mod;
}
static long fact(int n) {
long ans=1;
for (int i=2; i<=n; i++) ans=mul(ans, i);
return ans;
}
static long fastPow(long base, long exp) {
if (exp==0) return 1;
long half=fastPow(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static void debug(int ...a)
{
for(int x: a)
out.print(x+" ");
out.println();
}
static void debug(long ...a)
{
for(long x: a)
out.print(x+" ");
out.println();
}
static void debugMatrix(int[][] a)
{
for(int[] x:a)
out.println(Arrays.toString(x));
}
static void debugMatrix(long[][] a)
{
for(long[] x:a)
out.println(Arrays.toString(x));
}
static void reverseArray(int[] a) {
int n = a.length;
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void sort(int[] a)
{
ArrayList<Integer> ls = new ArrayList<>();
for(int x: a) ls.add(x);
Collections.sort(ls);
for(int i=0;i<a.length;i++)
a[i] = ls.get(i);
}
static void sort(long[] a)
{
ArrayList<Long> ls = new ArrayList<>();
for(long x: a) ls.add(x);
Collections.sort(ls);
for(int i=0;i<a.length;i++)
a[i] = ls.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 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 | a643ead38ff5961b799076a6e78489ab | 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 : Ryan Ranaut
__Hope is a big word, never lose it__
------------- --------------*/
import java.io.*;
import java.util.*;
public class Codeforces2 {
static PrintWriter out = new PrintWriter(System.out);
static final int mod = 1_000_000_007;
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());
}
int[] readIntArray(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());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
/*-------------------------------------------------------------------------*/
//Try seeing general case
public static void main(String[] args) {
FastReader s = new FastReader();
int t = s.nextInt();
while(t-->0)
{
int n = s.nextInt();
long x = s.nextLong();
long[] a = s.readLongArray(n);
find(n, x, a);
}
out.close();
}
public static void find(int n, long x, long[] a)
{
long[][] aa = new long[n+1][n];
for(long[] r: aa)
Arrays.fill(r, Long.MIN_VALUE);
for(int j=0;j<n;j++)
{
long sum = 0;
int row = 1;
for(int i=j;i<n;i++)
{
sum += a[i];
aa[row++][j] = sum;
}
}
//debugMatrix(aa);
long[] dp = new long[n+1];
dp[n] = aa[n][0];
for(int i=n-1;i>=0;i--)
{
long max = Long.MIN_VALUE;
for(int j=0;j<n;j++)
max = Math.max(max, aa[i][j]);
dp[i] = Math.max(dp[i+1], max);
}
//debug(dp);
long res = Math.max(dp[0], 0);
out.print(res+" ");
for(int k=1;k<=n;k++)
{
res = Math.max(res, k*x + dp[k]);
out.print(res+" ");
}
out.println();
}
/*-----------------------------------End of the road--------------------------------------*/
static class DSU {
int[] parent;
int[] ranks;
int[] groupSize;
int size;
public DSU(int n) {
size = n;
parent = new int[n];//0 based
ranks = new int[n];
groupSize = new int[n];//Size of each component
for (int i = 0; i < n; i++) {
parent[i] = i;
ranks[i] = 1; groupSize[i] = 1;
}
}
public int find(int x)//Path Compression
{
if (parent[x] == x)
return x;
else
return parent[x] = find(parent[x]);
}
public void union(int x, int y)//Union by rank
{
int x_rep = find(x);
int y_rep = find(y);
if (x_rep == y_rep)
return;
if (ranks[x_rep] < ranks[y_rep])
{
parent[x_rep] = y_rep;
groupSize[y_rep] += groupSize[x_rep];
}
else if (ranks[x_rep] > ranks[y_rep])
{
parent[y_rep] = x_rep;
groupSize[x_rep] += groupSize[y_rep];
}
else {
parent[y_rep] = x_rep;
ranks[x_rep]++;
groupSize[x_rep] += groupSize[y_rep];
}
size--;//Total connected components
}
}
public static int gcd(int x,int y)
{
return y==0?x:gcd(y,x%y);
}
public static long gcd(long x,long y)
{
return y==0L?x:gcd(y,x%y);
}
public static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static long pow(long a,long b)
{
if(b==0L)
return 1L;
long tmp=1;
while(b>1L)
{
if((b&1L)==1)
tmp*=a;
a*=a;
b>>=1;
}
return (tmp*a);
}
public static long modPow(long a,long b,long mod)
{
if(b==0L)
return 1L;
long tmp=1;
while(b>1L)
{
if((b&1L)==1L)
tmp*=a;
a*=a;
a%=mod;
tmp%=mod;
b>>=1;
}
return (tmp*a)%mod;
}
static long mul(long a, long b) {
return a*b%mod;
}
static long fact(int n) {
long ans=1;
for (int i=2; i<=n; i++) ans=mul(ans, i);
return ans;
}
static long fastPow(long base, long exp) {
if (exp==0) return 1;
long half=fastPow(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static void debug(int ...a)
{
for(int x: a)
out.print(x+" ");
out.println();
}
static void debug(long ...a)
{
for(long x: a)
out.print(x+" ");
out.println();
}
static void debugMatrix(int[][] a)
{
for(int[] x:a)
out.println(Arrays.toString(x));
}
static void debugMatrix(long[][] a)
{
for(long[] x:a)
out.println(Arrays.toString(x));
}
static void reverseArray(int[] a) {
int n = a.length;
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void sort(int[] a)
{
ArrayList<Integer> ls = new ArrayList<>();
for(int x: a) ls.add(x);
Collections.sort(ls);
for(int i=0;i<a.length;i++)
a[i] = ls.get(i);
}
static void sort(long[] a)
{
ArrayList<Long> ls = new ArrayList<>();
for(long x: a) ls.add(x);
Collections.sort(ls);
for(int i=0;i<a.length;i++)
a[i] = ls.get(i);
}
static class Pair{
int x, y;
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 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 | 1a82cc5b8f8d7c744333f28b460c7c2a | 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 {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
while (t-- > 0) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
int e = Integer.parseInt(st.nextToken());
arr[i] = e;
}
int max[] = new int[n+1];
Arrays.fill(max, Integer.MIN_VALUE);
max[0] = 0;
for(int i = 0; i < n; i++)
{
int sum = 0;
for(int j = i; j < n; j++)
{
sum+=arr[j];
max[j - i + 1] = Math.max(max[j - i + 1], sum);
}
}
// output.write(Arrays.toString(max) + "\n");
for(int k = 0; k<=n; k++)
{
int res = 0;
for(int l = 0; l<=n; l++)
res = Math.max(res, (max[l] + Math.min(k,l) * x));
output.write(res + " ");
}
output.write("\n");
// int k = Integer.parseInt(st.nextToken());
// char arr[] = br.readLine().toCharArray();
// output.write();
// int n = Integer.parseInt(st.nextToken());
// HashMap<Character, Integer> map = new HashMap<Character, Integer>();
// if
// output.write("YES\n");
// else
// output.write("NO\n");
// long a = Long.parseLong(st.nextToken());
// long b = Long.parseLong(st.nextToken());
// if(flag == 1)
// output.write("NO\n");
// else
// output.write("YES\n" + x + " " + y + " " + z + "\n");
// output.write(n+ "\n");
}
output.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 | 6350aef58ae29a1e1f84ddbda1bf43ea | 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.sound.midi.Soundbank;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.zip.InflaterInputStream;
public class Main {
static int N = 100001;
// Array to store inverse of 1 to N
static long[] factorialNumInverse = new long[N + 1];
// Array to precompute inverse of 1! to N!
static long[] naturalNumInverse = new long[N + 1];
// Array to store factorial of first N numbers
static long[] fact = new long[N + 1];
// Function to precompute inverse of numbers
public static void InverseofNumber(int p)
{
naturalNumInverse[0] = naturalNumInverse[1] = 1;
for(int i = 2; i <= N; i++)
naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p;
}
public static void InverseofFactorial(int p)
{
factorialNumInverse[0] = factorialNumInverse[1] = 1;
// Precompute inverse of natural numbers
for(int i = 2; i <= N; i++)
factorialNumInverse[i] = (naturalNumInverse[i] *
factorialNumInverse[i - 1]) % p;
}
// Function to calculate factorial of 1 to N
public static void factorial(int p)
{
fact[0] = 1;
// Precompute factorials
for(int i = 1; i <= N; i++)
{
fact[i] = (fact[i - 1] * (long)i) % p;
}
}
public static long ncp(int N, int R, int p)
{
// n C r = n!*inverse(r!)*inverse((n-r)!)
long ans = ((fact[N] * factorialNumInverse[R]) %
p * factorialNumInverse[N - R]) % p;
return ans;
}
public static void main(String[] args) throws IOException {
Reader.init(System.in);
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int t =Reader.nextInt();
while (t > 0){
int n = Reader.nextInt();
int x = Reader.nextInt();
int i = 0;
int [] array = new int[n];
long [][] dp = new long[n+1][n+1];
while ( i < n){
array[i] = Reader.nextInt();
i++;
}
i = 1;
while ( i <= n){
long max = 0;
int r = 0;
long sum = 0;
while (r <= i-1){
sum+=array[r];
r++;
}
max = sum;
int l = 0;
while (r < n){
sum -=array[l];
sum += array[r];
r++;
l++;
if ( sum > max){
max = sum;
}
}
dp[i][0] = max;
//1System.out.println(dp[i][0]+" "+i);
i++;
}
i = 1;
while ( i <= n){
int j = 1;
while ( j <= i){
dp[i][j] = dp[i][j-1]+x;
j++;
}
i++;
}
i = 0;
long pre= dp[0][0];
while ( i <= n){
int j = 0;
long max = dp[0][j];
while ( j <= n){
if ( dp[j][i] > max){
max = dp[j][i];
}
j++;
}
if ( pre < max){
pre = max;
}
output.write(pre+" ");
i++;
}
output.write("\n");
t--;
}
output.flush();
}
public static long log2(long N)
{
long result = (long)(Math.log(N) / Math.log(2));
return result;
}
private static int bs(int low,int high,int [] array,int find){
if ( low <= high ){
int mid = low + (high-low)/2;
if ( array[mid] > find){
high = mid -1;
return bs(low,high,array,find);
}
else if ( array[mid] < find){
low = mid+1;
return bs(low,high,array,find);
}
return mid;
}
return -1;
}
private static long max(long a, long b) {
return Math.max(a,b);
}
private static long min(long a,long b){
return Math.min(a,b);
}
public static long modularExponentiation(long a,long b,long mod){
if ( b == 1){
return a;
}
else{
long ans = modularExponentiation(a,b/2,mod)%mod;
if ( b%2 == 1){
return (a*((ans*ans)%mod))%mod;
}
return ((ans*ans)%mod);
}
}
public static long sum(long n){
return (n*(n+1))/2;
}
public static long abs(long a){
return a < 0 ? (-1*a) : a;
}
public static long gcd(long a,long b){
if ( a == 0){
return b;
}
else{
return gcd(b%a,a);
}
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
static long nextLong() throws IOException{
return Long.parseLong(next());
}
}
class NComparator implements Comparator<Node>{
@Override
public int compare(Node o1, Node o2) {
if ( o2.b > o1.b){
return 1;
}
else if ( o2.b < o1.b){
return -1;
}
else {
return 0;
}
}
}
class DComparator implements Comparator<Character>{
@Override
public int compare(Character o1, Character o2) {
if ( o2 < o1){
return 1;
}
else if ( o2 > o1){
return -1;
}
else{
return 0;
}
}
}
class Node{
String a;
int b;
Node(String A,int B){
a = A;
b = B;
}
}
/*
4 4
bbaa
abba
abaa
aaaa
*/
| 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 | e101670abc770fec46fff57ac1c0cfe5 | 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.Arrays;
import java.util.StringTokenizer;
/**
if all is positive all is max
-2 -7 -1
len = 1 -1
len = 2 -8
len = 3 -10
k = 1 len = 1 => -1 + x = 4 *
len = 2 => -8 + x = -3
len = 3 => -10 + x = -5
k = 2 len = 1 => -1 + 1 * x = 4 *
len = 2 => -8 + 2 * x = -4
len = 3 => -10 + 2 * x = 0
k = 3 len = 3 => -10 + 3 * x = 5 *
len = 2 => -8 + 2 * x = 2
len = 1 => -1 + x = 4
-2 -3 -7 -1
len = 1 -1
len = 2 -5
len = 3 -11
len = 4 -12
k = 2 len = 1 => -1 + x = 4
len = 2 => -5 + 2 * x = 5
*/
public class C {
static int INF = 0x3f3f3f3f;
static int N = 5010;
static int n, k, x;
static int[] a = new int[N];
static int[] s = new int[N];
static int[] lenMax = new int[N];
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int T = sc.nextInt();
while (T-- > 0) {
n = sc.nextInt();
x = sc.nextInt();
for(int i = 1; i <= n; i++) a[i] = sc.nextInt();
solver();
}
System.out.println();
}
static void solver() {
Arrays.fill(lenMax, 1, n + 1, -INF);
for(int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
// print(s);
int maxZero = -INF;
for(int i = 1; i <= n; i++){
for(int len = 1; i + len - 1 <= n; len++) {
int j = i + len - 1;
int curSum = s[j] - s[i - 1];
lenMax[len] = Math.max(lenMax[len], curSum);
maxZero = Math.max(maxZero, lenMax[len]);
}
}
// print(lenMax);
System.out.print(Math.max(0, maxZero) + " ");
for(int i = 1; i <= n; i++){ // k
int max = -INF;
for(int len = 1; len <= n; len++){
max = Math.max(max, lenMax[len] + Math.min(len, i) * x);
}
System.out.print(Math.max(0,max) + " ");
}
System.out.println();
}
static void print(int[] arr){
for(int i = 1; i <= n; i++) System.out.print(arr[i] + " ");
System.out.println();
}
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 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 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 | e7cc18ae32b293f1f3012bf7af38158f | 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 {
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;
}
}
public static FastReader obj = new FastReader();
public static PrintWriter out = new PrintWriter(System.out);
public static void sort(long[] a) {
ArrayList<Long> arr = new ArrayList<>();
for (int i = 0; i < a.length; i++)
arr.add(a[i]);
Collections.sort(arr);
for (int i = 0; i < arr.size(); i++)
a[i] = arr.get(i);
}
public static void revsort(long[] a) {
ArrayList<Long> arr = new ArrayList<>();
for (int i = 0; i < a.length; i++)
arr.add(a[i]);
Collections.sort(arr, Collections.reverseOrder());
for (int i = 0; i < arr.size(); i++)
a[i] = arr.get(i);
}
//Cover the small test cases like for n=1 .
public static long l() {
return obj.nextLong();
}
public static int i() {
return obj.nextInt();
}
public static String s() {
return obj.next();
}
public static long[] l(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = l();
return arr;
}
public static int[] i(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = i();
return arr;
}
public static long ceil(long a, long b) {
return (a + b - 1) / b;
}
public static void p(long val) {
out.println(val);
}
public static void p(String s) {
out.println(s);
}
public static void pl(long[] arr) {
for (int i = 0; i < arr.length; i++) {
out.print(arr[i] + " ");
}
out.println();
}
public static void p(int[] arr) {
for (int i = 0; i < arr.length; i++) {
out.print(arr[i] + " ");
}
out.println();
}
public static void main(String[] args) {
int len = i();
while (len-- != 0) {
int n = i();
int x=i();
long[] a=l(n);
long max=Integer.MIN_VALUE,sum=0;
long[] dp=new long[n];
for(int i=0;i<n;i++)
{
sum+=a[i];
max=Math.max(max, sum);
dp[i]=sum;
if(sum<0)sum=0;
}
long mm=Integer.MIN_VALUE;
for(int k=0;k<=n;k++) {
long ans=Integer.MIN_VALUE;
long tut=0;
for(int i=0;i<k;i++) tut+=a[i];
ans=Math.max(ans, tut);
for(int i=k;i<n;i++)
{
tut=tut+a[i]-a[i-k];
ans=Math.max(ans,tut);
ans=Math.max(ans, tut+dp[i-k]);
}
ans=ans+(k*x);
mm=Math.max(mm, ans);
out.print(Math.max(ans,mm)+" ");
}
out.println();
}
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 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 | f01c7d9db5268dfde77ca7398941e264 | 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.company;
import java.math.BigInteger;
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
//public static FastScanner fs = new FastScanner();
public static Scanner sc = new Scanner(System.in);
public static PrintWriter out = new PrintWriter(System.out);
public static int inf = 1000000007;
public static BigInteger infb = new BigInteger("1000000007");
public static long lmax= (long) 1e18;
public static boolean flag=false;
public static StringBuilder sb=new StringBuilder();
public static long[][] dp=new long[1000][1000];
public static long[] dis = new long[1000];
public static long[] vis = new long[10001];
public static long[] euler = new long[1000];
public static int index=0;
/////// For printing elements in 1D-array
public static void print1d(long[] arr) {
for (long x : arr) {
out.print(x + " ");
}
out.println();
}
/////// For printing elements in 2D-array
public static void print2d(long[][] arr) {
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[0].length;j++){
out.print(arr[i][j]+" ");
}
out.println();
}
out.println();
}
/////// For freq of elements in array
public static long[] freq(long[] freq_arr, long[] arr) {
for (long j : arr) {
freq_arr[(int) j]++;
}
return freq_arr;
}
/////// For sum elements in array
public static long sum(long[] arr) {
long sum = 0;
for (int i=0;i<arr.length;i++) {
sum += arr[i];
}
return sum;
}
/////// For storing elements in array 1D
public static long[] scan1d(long[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextLong();
}
return arr;
}
/////// For storing elements in array 2D
public static long[][] scan2d(long[][] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i][0] = sc.nextLong();
arr[i][1] = sc.nextLong();
}
return arr;
}
/////// For copying elements in array
public static long[] copy_arr(long[] arr, long[] arr1) {
for (int i = 0; i < arr.length; i++) {
arr1[i] = arr[i];
}
return arr;
}
/////// For GCD
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
/// gcd of array
static long gcd_arr(long arr[], long n)
{
long result = 0;
for (long element: arr){
result = gcd(result, element);
if(result == 1)
{
return 1;
}
}
return result;
}
//////// Forprime
public static boolean isPrime(long n) {
if (n < 2)
return false;
if (n == 2 || n == 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
long sqrtN = (long) Math.sqrt(n) + 1;
for (long i = 6L; i <= sqrtN; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0)
return false;
}
return true;
}
//////Using array as Set
public static boolean[] arr_set(boolean[] arr, int n) {
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
arr[x] = true;
}
return arr;
}
/// Fidning min in array
public static long min_arr(long[] arr){
long min=arr[0];
for(int i=0;i<arr.length;i++){
min=Math.min(min,arr[i]);
}
return min;
}
/// Fidning min in array
public static long max_arr(long[] arr){
long max=arr[0];
for(int i=0;i<arr.length;i++){
max=Math.max(max,arr[i]);
}
return max;
}
///copy array eles to another
public static long[] copy_arr(long[] arr){
long[] newarr=new long[arr.length];
newarr[0]=arr[0];
for(int i=0;i<arr.length;i++){
newarr[i]=arr[i];
}
return newarr;
}
///prefix sum
public static long[] pre_sum(long[] arr){
long[] prefix_sum=new long[arr.length];
prefix_sum[0]=arr[0];
for(int i=1;i<arr.length;i++){
prefix_sum[i]=prefix_sum[i-1]+arr[i];
}
return prefix_sum;
}
///sorted_arr
public static boolean sorted_arr(long[] arr,long[] arr_sorted){
for(int i=0;i<arr.length;i++){
if(arr[i]!=arr_sorted[i]){
return false;
}
}
return true;
}
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 nextLong() {
return Long.parseLong(next());
}
}
public static long fact(long n) {
long fact = 1;
for (long i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
public static long nCr(long n, long r) {
long res = 1;
for (long i = 0; i < r; i++) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
public static long pCr(long n, long r)
{
return fact(n) / (fact(n-r));
}
static boolean isSubSequence(String str1, String strm,
int m, int n)
{
int j = 0;
for (int i = 0; i < n && j < m; i++)
if (str1.charAt(j) == strm.charAt(i))
j++;
return (j == m);
}
public static boolean cmp(int a, int b)
{
return a > b;
}
static boolean isPowerOfTwo(long n)
{
if(n==0)
return false;
return (long)(Math.ceil((Math.log(n) / Math.log(2)))) ==
(long)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static boolean palindrome(String x){
String s="";
for(int i=x.length()-1;i>=0;i--){
s+=x.charAt(i);
}
if(s.equals(x)){
return true;
}
return false;
}
//// Max sum subarray
public static class pair {
int a;
int b;
pair(int a, int b){
this.a=a;
this.b=b;
}
}
public static int maxSum(long[] arr, int n, int k)
{
if (n < k)
{
System.out.println("Invalid");
return -1;
}
int res = 0;
for (int i=0; i<k; i++)
res += arr[i];
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;
}
static 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;
}
public static void main(String[] args) throws IOException {
// (fact(n)/(fact(i)*fact(n-i)))
// for(Map.Entry<String,Integer> entry : tree Map.entrySet()) {
// String key = entry.getKey();
// Integer value = entry.getValue();
//
// }
//File in = new File("input.txt");
//Writer wr= new FileWriter("output.txt");
//Scanner ssc=new Scanner(in);
//BigInteger n = new BigInteger(String.valueOf(sc.nextInt()));
//StringBuilder sb= new StringBuilder();
//Arrays.sort(myArr, (a, b) -> a[0] - b[0]); for index at 0
int t=sc.nextInt();
while (t-->0){
int n=sc.nextInt();
int x=sc.nextInt();
long[] arr = new long[n];
scan1d(arr);
long[] dp=new long[n+1];
Arrays.fill(dp, ((long) (-1*1e12)));
for(int i = 0; i < n; i++) {
long val = 0;
for(int j = i; j < n; j++) {
val += arr[j];
dp[j - i + 1] = Math.max(dp[j - i + 1], val);
}
}
for(long i = 0; i <= n; i++) {
long ans = 0;
for(long j = 1; j <= n; j++) {
ans = Math.max(ans, dp[(int) j] + (Math.min(i, j) * x));
}
out.print(ans+" ");
}
out.println();
}
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 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 | 78bc11bb2b533d5ed6f78056c089ee28 | 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.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.util.*;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cishu = sc.nextInt();
while (cishu-- != 0) {
int n = sc.nextInt();
int x = sc.nextInt();
int[] arr = new int[n];
long total = 0;
long[] maxsum = new long[n + 1];
long[] leftsum = new long[n];
long[] rightsum = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
leftsum[i] = total;
total += arr[i];
}
long rightau = 0;
for (int i = n - 1; i >= 0; i--) {
rightsum[i] = rightau;
rightau += arr[i];
}
maxsum[0] = 0;
for (int i = 1; i <= n; i++) {
long maxval = Long.MIN_VALUE;
for (int j = 0; j + i <= n; j++) {
long tmp = leftsum[j] + rightsum[i + j - 1];
maxval = Math.max(total-tmp, maxval);
}
maxsum[i] = maxval;
}
//System.out.println("maxsum: "+Arrays.toString(maxsum));
for (int i = 0; i <= n; i++) {
long re = 0;
for (int j = 1; j < n + 1; j++) {
int tmp = Math.min(i, j);
re = Math.max(re, maxsum[j] + (long) tmp * x);
}
System.out.print(re + " ");
}
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 | 03e719e165a0816471dfce01fde0d176 | 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 div2
{
public static void main(String[] args) throws IOException
{
Reader sc=new Reader();
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
for(int i=n-1;i>=0;i--)
{
a[i]=sc.nextInt();
}
long [] ans=new long[n+1];
Arrays.fill(ans, Integer.MIN_VALUE);
for(int i = 0; i < n; i++) {
long val = 0;
for(int j = i; j < n; j++) {
val += a[j];
ans[j - i + 1] = Math.max(ans[j - i + 1], val);
}
}
for(int i = 0; i <= n; i++) {
long cal = 0;
for(int j = 1; j <= n; j++) {
cal = Math.max(cal, ans[j] + (Math.min(i, j) * k));
}
System.out.print(cal+" ");
}
System.out.println();
}
}
private static void swap(int[] a, int i, int j) {
int t=a[i];
a[i]=a[j];
a[j]=t;
}
private static boolean pal(String con1) {
int i=0,j=con1.length()-1;
while(i<=j)
{
if(con1.charAt(i)!=con1.charAt(j))
{
return false;
}
i++;
j--;
}
return true;
}
}
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') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
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 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 | 032b0b084162573cb01945d2d7f41918 | 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.rajan.codeforces.contests.educationRound123;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class IncreaseSubarrySums {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int tt = Integer.parseInt(reader.readLine());
while (tt-- > 0) {
String[] temp = reader.readLine().split("\\s+");
int n = Integer.parseInt(temp[0]), x = Integer.parseInt(temp[1]);
int[] a = new int[n];
long[] prefixSum = new long[n];
temp = reader.readLine().split("\\s+");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(temp[i]);
if (i == 0)
prefixSum[i] = a[i];
else
prefixSum[i] = prefixSum[i - 1] + a[i];
}
Map<Integer, Long> maxSum = new HashMap<>();
maxSum.put(0, 0L);
long max = Long.MIN_VALUE;
for (int l = 1; l <= n; l++) {
for (int i = 0; i <= n - l; i++) {
int j = i + l - 1;
maxSum.put(j - i + 1, Math.max(maxSum.getOrDefault(j - i + 1, Long.MIN_VALUE), prefixSum[j] - (i > 0 ? prefixSum[i - 1] : 0L)));
max = Math.max(max, maxSum.get(j - i + 1));
}
}
for (int k = 0; k <= n; k++) {
if (k == 0)
writer.write(Math.max(max, maxSum.get(0)) + "");
else {
long ans = Long.MIN_VALUE;
for (int j = 0; j <= n; j++)
ans = Math.max(ans, Math.min(j, k) * 1L * x + maxSum.get(j));
writer.write(" " + ans);
}
}
writer.write("\n");
}
writer.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 | 306565a562fe0b62f1ac530669e56f89 | 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.reflect.Array;
import java.util.*;
import java.lang.reflect.Array;
import javax.print.DocFlavor.STRING;
import javax.swing.Popup;
import javax.swing.plaf.synth.SynthStyleFactory;
public class Simple{
public static class Pair implements Comparable<Pair>{
int val;
int freq = 0;
Pair prev ;
Pair next;
boolean bool = false;
public Pair(int val,int freq){
this.val = val;
this.freq= freq;
}
public int compareTo(Pair p){
// if(p.freq == this.freq){
// return this.val - p.freq;
// };
// if(this.freq > p.freq)return -1;
// return 1;
return (p.freq-p.val) - (this.freq - this.val);
}
}
public static long factorial(long n)
{
// single line to find factorial
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
static long m = 998244353;
// Function to return the GCD of given numbers
static long gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Recursive function to return (x ^ n) % m
static long modexp(long x, long n)
{
if (n == 0) {
return 1;
}
else if (n % 2 == 0) {
return modexp((x * x) % m, n / 2);
}
else {
return (x * modexp((x * x) % m, (n - 1) / 2) % m);
}
}
// Function to return the fraction modulo mod
// static long getFractionModulo(long a, long b)
// {
// long c = gcd(a, b);
// a = a / c;
// b = b / c;
// // (b ^ m-2) % m
// long d = modexp(b, m - 2);
// // Final answer
// long ans = ((a % m) * (d % m)) % m;
// return ans;
// }
// public static long bs(long lo,long hi,long fact,long num){
// long help = num/fact;
// long ans = hi;
// while(lo<=hi){
// long mid = (lo+hi)/2;
// if(mid/)
// }
// }
// public static boolean isPrime(int n)
// {
// // Check if number is less than
// // equal to 1
// if (n <= 1)
// return false;
// // Check if number is 2
// else if (n == 2)
// return true;
// // Check if n is a multiple of 2
// else if (n % 2 == 0)
// return false;
// // If not, then just check the odds
// for (int i = 3; i <= sqrt(n); i += 2)
// {
// if (n % i == 0)
// return false;
// }
// return true;
// }
// public static int countDigit(long n)
// {
// int count = 0;
// while (n != 0) {
// n = n / 10;
// ++count;
// }
// return count;
// }
// static ArrayList<Long> al ;
// static boolean checkperfectsquare(long n)
// {
// // If ceil and floor are equal
// // the number is a perfect
// // square
// if (processesh.ceil((double)processesh.sqrt(n)) ==
// processesh.floor((double)processesh.sqrt(n)))
// {
// return true;
// }
// else
// {
// return false;
// }
// }
public static void decToBinary(int n,int arr[],int j)
{
// Size of an integer is assumed to be 32 bits
for (int i = 31; i >= 0; i--) {
int k = n >> i;
if ((k & 1) > 0){
arr[j]++;
}
j++;
}
}
public static class Node{
//int u;
long v;
long w;
public Node(long v,long w){
//this.u = u;
this.v=v;
this.w=w;
}
}
public static boolean[] sieve(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++){
if(!isPrime[i])continue;
for(int j= i*2;j<=n;j=j+i){
isPrime[j] = false;
}
}
return isPrime;
}
static long power(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
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 modInverse(int a, int p)
// {
// return power(a, p - 2, p);
// }
public static void bfs(ArrayList<ArrayList<Integer>> adj,boolean vis[],int dis[]){
vis[0] = true;
Queue<Integer> q = new LinkedList<>();
q.add(0);
int count = 0;
while(!q.isEmpty()){
int size = q.size();
for(int j = 0;j<size;j++){
int poll = q.poll();
dis[poll] = count;
vis[poll] = true;
for(Integer x : adj.get(poll)){
if(!vis[x]){
q.add(x);
}
}
}
count++;
}
}
// static void swap(int[] arr, int i, int j)
// {
// int temp = arr[i];
// arr[i] = arr[j];
// arr[j] = temp;
// }
public static boolean isSorted(int arr[],int[] copy,int n){
for(int i=0;i<n;i++){
if(arr[i]!=copy[i])return false;
}
return true;
}
public static class DSU{
int par[];
int rank[];//rank denotes the height of graph
public DSU(int n){
par = new int[n];
for(int i=0;i<n;i++){
par[i] = i;
}
rank = new int[n];
}
public int findPar(int node){
if(par[node]==node)return node;
par[node] = findPar(par[node]);
return par[node];
}
public void union(int u,int v){
u = findPar(u);
v = findPar(v);
if(rank[v]>rank[u]){
par[u] = v;
}
else if(rank[v]<rank[u]){
par[v] = u;
}
else{
rank[u]++;
par[v] = u;
}
}
}
public static long modFact(int n,int k)
{
long M = 998244353;
long f = 1;
for (int i = 1; i <= n; i++){
if(i==k+1)continue;
f = (f%M*i%m) % M; // Now f never can
} // exceed 10^9+7
return f;
}
public static class Item{
int val;
int index;
public Item(int val,int index){
this.val = val;
this.index = index;
}
}
public static void mergeSortCount(Item[] itemArr,int count[],int n,int i,int j){
if(i>=j)return;
int mid = (i+j)/2;
mergeSortCount(itemArr, count, n, i, mid);
mergeSortCount(itemArr, count, n, mid+1, j);
mergeCount(itemArr,count,i,mid+1,mid,j);
}
public static void mergeCount(Item[] itemArr,int count[],int i1,int i2,int j1,int j2){
int size = j2 - i1+1;
Item[] sortedItems = new Item[size];
int index = 0;
int i11 = i1;
//int j11 = j2;
int i22 = i2;
//int j22 = j2;
while(i11<=j1 && i22 <=j2 ){
if(itemArr[i11].val < itemArr[i22].val){
count[itemArr[i11].index]+= (j2-i22+1);
sortedItems[index++] = itemArr[i11++];
}
else{
sortedItems[index++] = itemArr[i22++];
}
}
while(i11<=j1){
sortedItems[index++] = itemArr[i11++];
}
while(i22<=j2){
sortedItems[index++] = itemArr[i22++];
}
index=0;
for(int i=i1;i<=j2;i++){
itemArr[i] = sortedItems[index++];
}
}
public static void main(String args[]){
//System.out.println("Hello Java");
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int t1 = 1;t1<=t;t1++){
int n =s.nextInt();
int x = s.nextInt();
// int arr[]= new int[n];
int arr[] = new int[n];
long prev = 0;
for(int i=0;i<n;i++){
arr[i] = s.nextInt();
}
long help[] = new long[n+1];
Arrays.fill(help, Long.MIN_VALUE);
help[0] =0;
for(int i=0;i<n;i++){
long sum =0;
for(int j=i;j<n;j++){
sum+=(long)arr[j];
help[j-i+1]=Math.max(help[j-i+1],sum);
}
}
// for(int i=0;i<=n;i++){
// System.out.print(help[i]+" ");
// }
// System.out.println();
for(int k=0;k<=n;k++){
long ans =0;
for(int i=0;i<=n;i++){
long temp = help[i] + Math.min(i,k)*x;
ans = Math.max(ans,temp);
}
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 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 | 475058c87f3a31b268a24eb1d260b023 | 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.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
public class Main {
// Graph
// prefix sums
//inputs
public static void main(String args[])throws Exception{
Input sc=new Input();
precalculates p=new precalculates();
StringBuilder sb=new StringBuilder();
int t=sc.readInt();
for(int f=0;f<t;f++){
long d[]=sc.readArrayLong();
int n=(int)d[0];
long x=d[1];
long a[]=sc.readArrayLong();
long dp[]=new long[n];
long ch[]=new long [n];
Arrays.fill(ch,Long.MIN_VALUE);
long len=0;
long m=Long.MIN_VALUE;
for(int i=1;i<=n;i++){
long kk=Long.MIN_VALUE;
long sum=0;
for(int j=0;j<i;j++){
sum+=a[j];
}
if(sum>=kk){
kk=sum;
}
if(sum>=m){
len=i;
m=sum;
}
//System.out.println(sum+" ");
for(int j=i;j<n;j++){
sum+=a[j];
sum-=a[j-i];
if(sum>=m){
len=i;
m=sum;
}
if(sum>=kk)
kk=sum;
// System.out.println(sum+" ");
}
ch[i-1]=Math.max(ch[i-1],kk);
}
// for(int i=0;i<n;i++){
// System.out.println(ch[i]+" ");
// }
long ans=m;
ans=Math.max(ans,0);
sb.append(ans+" ");
for(int i=1;i<=n;i++){
long sum=0;
for(int j=0;j<i;j++){
sum+=a[j];
}
ans=Math.max(ans,sum+(i*x));
for(int j=i;j<n;j++){
sum+=a[j];
sum-=a[j-i];
ans=Math.max(ans,sum+((long)i*x));
}
ans=Math.max(ans,m+((long)Math.min(len,i)*x));
for(int j=0;j<n;j++){
ans=Math.max(ans,ch[j]+((long)Math.min(i,j+1)*x));
}
sb.append(ans+" ");
}
sb.append("\n");
}
System.out.print(sb);
}
}
class Input{
BufferedReader br;
StringTokenizer st;
Input(){
br=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public int[] readArray() throws Exception{
st=new StringTokenizer(br.readLine());
int a[]=new int[st.countTokens()];
for(int i=0;i<a.length;i++){
a[i]=Integer.parseInt(st.nextToken());
}
return a;
}
public long[] readArrayLong() throws Exception{
st=new StringTokenizer(br.readLine());
long a[]=new long[st.countTokens()];
for(int i=0;i<a.length;i++){
a[i]=Long.parseLong(st.nextToken());
}
return a;
}
public int readInt() throws Exception{
st=new StringTokenizer(br.readLine());
return Integer.parseInt(st.nextToken());
}
public long readLong() throws Exception{
st=new StringTokenizer(br.readLine());
return Long.parseLong(st.nextToken());
}
public String readString() throws Exception{
return br.readLine();
}
public int[][] read2dArray(int n,int m)throws Exception{
int a[][]=new int[n][m];
for(int i=0;i<n;i++){
st=new StringTokenizer(br.readLine());
for(int j=0;j<m;j++){
a[i][j]=Integer.parseInt(st.nextToken());
}
}
return a;
}
}
class precalculates{
public long gcd(long p, long q) {
if (q == 0) return p;
else return gcd(q, p % q);
}
public int[] prefixSumOneDimentional(int a[]){
int n=a.length;
int dp[]=new int[n];
for(int i=0;i<n;i++){
if(i==0)
dp[i]=a[i];
else
dp[i]=dp[i-1]+a[i];
}
return dp;
}
public int[] postSumOneDimentional(int a[]) {
int n = a.length;
int dp[] = new int[n];
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1)
dp[i] = a[i];
else
dp[i] = dp[i + 1] + a[i];
}
return dp;
}
public int[][] prefixSum2d(int a[][]){
int n=a.length;int m=a[0].length;
int dp[][]=new int[n+1][m+1];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j]=a[i-1][j-1]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];
}
}
return dp;
}
public long pow(long a,long b){
long mod=998244353;
long ans=1;
if(b<=0)
return 1;
if(b%2==0){
ans=pow(a,b/2)%mod;
return ((ans%mod)*(ans%mod))%mod;
}else{
ans=pow(a,b-1)%mod;
return ((a%mod)*(ans%mod))%mod;
}
}
}
class GraphInteger{
HashMap<Integer,vertex> vtces;
class vertex{
HashMap<Integer,Integer> children;
public vertex(){
children=new HashMap<>();
}
}
public GraphInteger(){
vtces=new HashMap<>();
}
public void addVertex(int a){
vtces.put(a,new vertex());
}
public void addEdge(int a,int b,int cost){
if(!vtces.containsKey(a)){
vtces.put(a,new vertex());
}
if(!vtces.containsKey(b)){
vtces.put(b,new vertex());
}
vtces.get(a).children.put(b,cost);
// vtces.get(b).children.put(a,cost);
}
public boolean isCyclicDirected(){
boolean isdone[]=new boolean[vtces.size()+1];
boolean check[]=new boolean[vtces.size()+1];
for(int i=1;i<=vtces.size();i++) {
if (!isdone[i] && isCyclicDirected(i,isdone, check)) {
return true;
}
}
return false;
}
private boolean isCyclicDirected(int i,boolean isdone[],boolean check[]){
if(check[i])
return true;
if(isdone[i])
return false;
check[i]=true;
isdone[i]=true;
Set<Integer> set=vtces.get(i).children.keySet();
for(Integer ii:set){
if(isCyclicDirected(ii,isdone,check))
return true;
}
check[i]=false;
return false;
}
}
class union_find {
int n;
int[] sz;
int[] par;
union_find(int nval) {
n = nval;
sz = new int[n + 1];
par = new int[n + 1];
for (int i = 0; i <= n; i++) {
par[i] = i;
sz[i] = 1;
}
}
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
boolean find(int a, int b) {
return root(a) == root(b);
}
int union(int a, int b) {
int ra = root(a);
int rb = root(b);
if (ra == rb)
return 0;
if(a==b)
return 0;
if (sz[a] > sz[b]) {
int temp = ra;
ra = rb;
rb = temp;
}
par[ra] = rb;
sz[rb] += sz[ra];
return 1;
}
}
/*
static int mod=998244353;
private static int add(int x, int y) {
x += y;
return x % MOD;
}
private static int mul(int x, int y) {
int res = (int) (((long) x * y) % MOD);
return res;
}
private static int binpow(int x, int y) {
int z = 1;
while (y > 0) {
if (y % 2 != 0)
z = mul(z, x);
x = mul(x, x);
y >>= 1;
}
return z;
}
private static int inv(int x) {
return binpow(x, MOD - 2);
}
private static int devide(int x, int y) {
return mul(x, inv(y));
}
private static int C(int n, int k, int[] fact) {
return devide(fact[n], mul(fact[k], fact[n-k]));
}
*/
| 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 | e18abb5c25be01c6da98212eeeda36fd | 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) {
while (N-- > 0) {
solve();
}
out.close();
}
public static void solve() {
int M = sc.nextInt();
int x = sc.nextInt();
int[] a = sc.readArray(M);
int[] dp = new int[M + 1];
int[] max = new int[M + 1];
Arrays.fill(max, Integer.MIN_VALUE);
max[0] = 0;
int tmp = 0;
for (int i = 1; i <= M; i++) {
for (int j = M; j >= i; j--) {
dp[j] = dp[j - 1] + a[j - 1];
max[i] = Math.max(max[i], dp[j]);
}
}
for (int i = 0; i <= M; i++) {
tmp = Math.max(max[i], tmp);
}
out.print(tmp + " ");
for (int i = 1; i <= M; i++) {
for (int j = i; j <= M; j++) {
max[j] += x;
tmp = Math.max(max[j], tmp);
}
out.print(tmp + " ");
}
out.println();
}
private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
private static MyScanner sc = new MyScanner();
private static int N = sc.nextInt();
private final static int MOD = 1000000007;
@SuppressWarnings("unused")
private 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());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
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 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 | c4069962abf82707428fc26fa7c9c05b | 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 in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 1; t <= test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
int x = sc.nextInt();
int[] arr = new int[n + 1];
for (int i = 1; i <= n; i++) {
arr[i] = sc.nextInt();
}
long[][] dp = new long[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
dp[i][0] = Math.max(arr[i], dp[i - 1][0] + arr[i]);
}
for (int i = 1; i <= n; i++) {
for (int k = 1; k <= n; k++) {
dp[i][k] = Math.max(dp[i - 1][k] + arr[i], Math.max(0, dp[i - 1][k - 1]) + arr[i] + x);
}
}
/*for (int i = 0; i <= n; i++) {
out.println(dp[i][1]);
}*/
for (int k = 0; k <= n; k++) {
long best = 0;
for (int i = 0; i <= n; i++) {
best = Math.max(best, dp[i][k]);
}
out.print(best + " ");
}
out.println();
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.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 lastMonthOfVacation)
{
lastMonthOfVacation.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 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 | dc099d24595eafb4a1798f76b9156856 | 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 javax.print.DocFlavor.INPUT_STREAM;
import java.io.*;
import java.math.*;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
public class Main {
private static class MyScanner {
private static final int BUF_SIZE = 2048;
BufferedReader br;
private MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
private boolean isSpace(char c) {
return c == '\n' || c == '\r' || c == ' ';
}
String next() {
try {
StringBuilder sb = new StringBuilder();
int r;
while ((r = br.read()) != -1 && isSpace((char)r));
if (r == -1) {
return null;
}
sb.append((char) r);
while ((r = br.read()) != -1 && !isSpace((char)r)) {
sb.append((char)r);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
static class Reader{
BufferedReader br;
StringTokenizer st;
public Reader() {
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 void sort(long[] arr ) {
ArrayList<Long> al = new ArrayList<>();
for(long e:arr) al.add(e);
Collections.sort(al);
for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i);
}
static void sort(int[] arr ) {
ArrayList<Integer> al = new ArrayList<>();
for(int e:arr) al.add(e);
Collections.sort(al);
for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i);
}
static void sort(char[] arr) {
ArrayList<Character> al = new ArrayList<Character>();
for(char cc:arr) al.add(cc);
Collections.sort(al);
for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i);
}
static long mod_mul( long... a) {
long ans = a[0]%mod;
for(int i = 1 ; i<a.length ; i++) {
ans = (ans * (a[i]%mod))%mod;
}
return ans;
}
static long mod_sum( long... a) {
long ans = 0;
for(long e:a) {
ans = (ans + e)%mod;
}
return ans;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void print(long[] arr) {
System.out.println("---print---");
for(long e:arr) System.out.print(e+" ");
System.out.println("-----------");
}
static void print(int[] arr) {
System.out.println("---print---");
for(long e:arr) System.out.print(e+" ");
System.out.println("-----------");
}
static boolean[] prime(int num) {
boolean[] bool = new boolean[num];
for (int i = 0; i< bool.length; i++) {
bool[i] = true;
}
for (int i = 2; i< Math.sqrt(num); i++) {
if(bool[i] == true) {
for(int j = (i*i); j<num; j = j+i) {
bool[j] = false;
}
}
}
if(num >= 0) {
bool[0] = false;
bool[1] = false;
}
return bool;
}
static long modInverse(long a, long m)
{
long g = gcd(a, m);
return power(a, m - 2);
}
static long lcm(long a , long b) {
return (a*b)/gcd(a, b);
}
static int lcm(int a , int b) {
return (int)((a*b)/gcd(a, b));
}
static long power(long x, long y){
if(y<0) return 0;
long m = mod;
if (y == 0) return 1; long p = power(x, y / 2) % m; p = (int)((p * (long)p) % m);
if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); }
static class Combinations{
private long[] z; // factorial
private long[] z1; // inverse factorial
private long[] z2; // incerse number
private long mod;
public Combinations(long N , long mod) {
this.mod = mod;
z = new long[(int)N+1];
z1 = new long[(int)N+1];
z[0] = 1;
for(int i =1 ; i<=N ; i++) z[i] = (z[i-1]*i)%mod;
z2 = new long[(int)N+1];
z2[0] = z2[1] = 1;
for (int i = 2; i <= N; i++)
z2[i] = z2[(int)(mod % i)] * (mod - mod / i) % mod;
z1[0] = z1[1] = 1;
for (int i = 2; i <= N; i++)
z1[i] = (z2[i] * z1[i - 1]) % mod;
}
long fac(long n) {
return z[(int)n];
}
long invrsNum(long n) {
return z2[(int)n];
}
long invrsFac(long n) {
return z1[(int)n];
}
long ncr(long N, long R)
{ if(R<0 || R>N ) return 0;
long ans = ((z[(int)N] * z1[(int)R])
% mod * z1[(int)(N - R)])
% mod;
return ans;
}
}
static class DisjointUnionSets {
int[] rank, parent;
int n;
public DisjointUnionSets(int n)
{
rank = new int[n];
parent = new int[n];
this.n = n;
makeSet();
}
void makeSet()
{
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
int find(int x)
{
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void union(int x, int y)
{
int xRoot = find(x), yRoot = find(y);
if (xRoot == yRoot)
return;
if (rank[xRoot] < rank[yRoot])
parent[xRoot] = yRoot;
else if (rank[yRoot] < rank[xRoot])
parent[yRoot] = xRoot;
else
{
parent[yRoot] = xRoot;
rank[xRoot] = rank[xRoot] + 1;
}
}
}
static int max(int... a ) {
int max = a[0];
for(int e:a) max = Math.max(max, e);
return max;
}
static long max(long... a ) {
long max = a[0];
for(long e:a) max = Math.max(max, e);
return max;
}
static int min(int... a ) {
int min = a[0];
for(int e:a) min = Math.min(e, min);
return min;
}
static long min(long... a ) {
long min = a[0];
for(long e:a) min = Math.min(e, min);
return min;
}
static int[] KMP(String str) {
int n = str.length();
int[] kmp = new int[n];
for(int i = 1 ; i<n ; i++) {
int j = kmp[i-1];
while(j>0 && str.charAt(i) != str.charAt(j)) {
j = kmp[j-1];
}
if(str.charAt(i) == str.charAt(j)) j++;
kmp[i] = j;
}
return kmp;
}
/************************************************ Query **************************************************************************************/
/***************************************** Sparse Table ********************************************************/
static class SparseTable{
private long[][] st;
SparseTable(long[] arr){
int n = arr.length;
st = new long[n][25];
log = new int[n+2];
build_log(n+1);
build(arr);
}
private void build(long[] arr) {
int n = arr.length;
for(int i = n-1 ; i>=0 ; i--) {
for(int j = 0 ; j<25 ; j++) {
int r = i + (1<<j)-1;
if(r>=n) break;
if(j == 0 ) st[i][j] = arr[i];
else st[i][j] = Math.min(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] );
}
}
}
public long gcd(long a ,long b) {
if(a == 0) return b;
return gcd(b%a , a);
}
public long query(int l ,int r) {
int w = r-l+1;
int power = log[w];
return Math.min(st[l][power],st[r - (1<<power) + 1][power]);
}
private int[] log;
void build_log(int n) {
log[1] = 0;
for(int i = 2 ; i<=n ; i++) {
log[i] = 1 + log[i/2];
}
}
}
/******************************************************** Segement Tree *****************************************************/
static class SegmentTree{
long[] tree;
long[] arr;
int n;
SegmentTree(long[] arr){
this.n = arr.length;
tree = new long[4*n+1];
this.arr = arr;
buildTree(0, n-1, 1);
}
void buildTree(int s ,int e ,int index ) {
if(s == e) {
tree[index] = arr[s];
return;
}
int mid = (s+e)/2;
buildTree( s, mid, 2*index);
buildTree( mid+1, e, 2*index+1);
tree[index] = gcd(tree[2*index] , tree[2*index+1]);
}
long query(int si ,int ei) {
return query(0 ,n-1 , si ,ei , 1 );
}
private long query( int ss ,int se ,int qs , int qe,int index) {
if(ss>=qs && se<=qe) return tree[index];
if(qe<ss || se<qs) return (long)(0);
int mid = (ss + se)/2;
long left = query( ss , mid , qs ,qe , 2*index);
long right= query(mid + 1 , se , qs ,qe , 2*index+1);
return gcd(left, right);
}
public void update(int index , int val) {
arr[index] = val;
// for(long e:arr) System.out.print(e+" ");
update(index , 0 , n-1 , 1);
}
private void update(int id ,int si , int ei , int index) {
if(id < si || id>ei) return;
if(si == ei ) {
tree[index] = arr[id];
return;
}
if(si > ei) return;
int mid = (ei + si)/2;
update( id, si, mid , 2*index);
update( id , mid+1, ei , 2*index+1);
tree[index] = Math.min(tree[2*index] ,tree[2*index+1]);
}
}
/* ***************************************************************************************************************************************************/
// static MyScanner sc = new MyScanner(); // only in case of less memory
static Reader sc = new Reader();
static int TC;
static StringBuilder sb = new StringBuilder();
static PrintWriter out=new PrintWriter(System.out);
public static void main(String args[]) throws IOException {
int tc = 1;
tc = sc.nextInt();
TC = 0;
for(int i = 1 ; i<=tc ; i++) {
TC++;
// sb.append("Case #" + i + ": " ); // During KickStart && HackerCup
TEST_CASE();
}
System.out.print(sb);
}
static void TEST_CASE() throws IOException {
int n = sc.nextInt();
long x = sc.nextLong();
long[] arr = new long[n];
for(int i = 0 ;i<n ; i++) arr[i] = sc.nextLong();
long[] pre = new long[n];
pre[0] = arr[0];
for(int i = 1; i<n ; i++) pre[i] += arr[i]+pre[i-1];
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 len = j-i+1;
long sum = pre[j]-pre[i]+arr[i];
max[len] = max(max[len] , sum);
}
}
// for(long e:max) System.out.print(e+" ");
// System.out.println();
for(int k = 0 ; k<=n ; k++) {
sb.append(max(max , k , x)+" ");
}
sb.append("\n");
}
static long max(long[] max, int k ,long x) {
long ans = 0;
for(int i =0 ;i<max.length ; i++) {
ans = max(ans ,max[i] + min(i , k)*x );
}
return 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 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 | df8fdd89bbbcd0805a12abd911922496 | 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 Main
{
static final PrintWriter out =new PrintWriter(System.out);
static final FastReader sc = new FastReader();
/*
_oo0oo_
o8888888o
88" . "88
(| -_- |)
0\ = /0
___/`---'\___
.' \\| |// '.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' |_/ |
\ .-\__ '-' ___/-. /
___'. .' /--.--\ `. .'___
."" '< `.___\_<|>_/___.' >' "".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `_. \_ __\ /__ _/ .-` / /
=====`-.____`.___ \_____/___.-`___.-'=====
`=---='
*/
public static boolean sorted(int a[])
{
int n=a.length,i;
int b[]=new int[n];
for(i=0;i<n;i++)
b[i]=a[i];
Arrays.sort(b);
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
int tes=sc.nextInt();
while(tes-->0)
{
int n=sc.nextInt();
int x=sc.nextInt();
int dp[][]=new int[n+1][n+1];
int a[]=new int[n+1];
a[0]=0;
int i,j,max=0;
for(i=1;i<=n;i++)
a[i]=sc.nextInt();
for(i=1;i<=n;i++)
{
dp[0][i]=Math.max(0,dp[0][i-1]+a[i]);
max=Math.max(max,dp[0][i]);
}
System.out.print(max+" ");
for(i=1;i<=n;i++)
{
max=0;
for(j=1;j<=n;j++)
{
dp[i][j]=Math.max(dp[i-1][j-1]+a[j]+x,0);
max=Math.max(max,dp[i][j]);
}
System.out.print(max+" ");
}
System.out.println();
}
}
public static int first(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x)
return mid;
else if (x > arr.get(mid))
return first(arr, (mid + 1), high, x, n);
else
return first(arr, low, (mid - 1), x, n);
}
return -1;
}
public static int last(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x)
return mid;
else if (x < arr.get(mid))
return last(arr, low, (mid - 1), x, n);
else
return last(arr, (mid + 1), high, x, n);
}
return -1;
}
public static int lis(int[] arr) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>=x) {
al.add(arr[i]);
}else {
int v = upper_bound(al, 0, al.size(), arr[i]);
al.set(v, arr[i]);
}
}
return al.size();
}
public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get((int)mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
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 int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long mod=1000000007;
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
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();
}
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 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 | 434663f7501d065a1cdd6dc9e783dcc2 | 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 Nitin Bhakar
*
*/
import java.io.*;
import java.util.*;
public class Codeforces{
static long mod = 1000000007;
static long m = 998244353;
static int ninf = Integer.MIN_VALUE;
static int inf = Integer.MAX_VALUE;
static void swap(int[] a,int i,int j) {int temp = a[i]; a[i] = a[j]; a[j] = temp;}
static void priArr(int[] a) {for(int i=0;i<a.length;i++) out.print(a[i] + " ");out.println();}
static long gcd(long a,long b){if(b==0) return a; return gcd(b,a%b);}
static int gcd(int a,int b){ if(b==0) return a;return gcd(b,a%b);}
static void sieve(boolean[] a, int n) {a[1] = false; for(int i=2;i*i<=n;i++) { if(a[i]) { for(int j=i*i;j<=n;j+=i) a[j] = false;}}}
static int getRanNum(int lo, int hi) {return (int)Math.random()*(hi-lo+1)+lo;}
private static void sort(int[] arr) {List<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);}
private static long power(int n, int m) {long res = 1;while(m>0) {if(m%2 != 0) {res *= n; m--;}n *=n;m/=2;}return res;}
static long modInv(long x,long mod) {return expo(x,mod-2,mod)%mod;}
static long expo(long x,long k,long mod) {long res = 1;while(k>0) {if(k%2 != 0) {res = (res*x)%mod;k--;}x = (x*x)%mod;k /= 2;}return res%mod;}
static long lcm(int a, int b){return a*(b/gcd(a,b));}
static long lcm(long a, long b){return a*(b/gcd(a,b));}
//map.put(a[i],map.getOrDefault(a[i],0)+1);
// map.putIfAbsent;
// StringBuilder ans = new StringBuilder();
static MyScanner sc = new MyScanner();
// static long[][] dp;
//<----------------------------------------------WRITE HERE------------------------------------------->
static void solve(){
int n = sc.nextInt();
int m = sc.nextInt();
int[] a = sc.readIntArray(n);
int[] ans = new int[n+1];
Arrays.fill(ans, ninf);
ans[0] = 0;
for(int i=1;i<=n;i++) {
int cur = 0;
for(int j=i;j<=n;j++) {
cur += a[j-1];
ans[j-i+1] = Math.max(ans[j-i+1], cur);
}
}
int[] pri = new int[n+1];
for(int i=0;i<=n;i++) {
int max = 0;
for(int j=0;j<=n;j++) {
max = Math.max(max, ans[j]+(Math.min(i, j)*m));
}
pri[i] = max;
}
priArr(pri);
}
//<----------------------------------------------WRITE HERE------------------------------------------->
static class Pair {
int v1;
long v2;
Pair(){}
Pair(int v,long f){
v1 = v;
v2 = f;
}
// public int compareTo(Pair p){
// if(this.v1 == p.v1) return this.v2-p.v2;
// return this.v1-p.v1;
// }
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
solve();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
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();
}
public int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
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 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 | 85bde9ff848e2ede14c6d8ed6458913a | 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.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Main {
static PrintWriter out;
static Reader in;
public static void main(String[] args) throws Exception {
input_output();
Main solver = new Main();
solver.solve();
out.close();
out.flush();
}
static long INF = (long)1e16;
static int MAXN = 100_000;
static int MOD = 998244353;
static int t, n, m, d;
void solve() throws Exception {
t = in.nextInt();
while (t --> 0) {
n = in.nextInt();
int x = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = in.nextInt();
int[] subsum = new int[n+1];
for (int i = 0; i < n; i++) subsum[i] = arr[i];
int[] maxsub = new int[n];
for (int i = 0; i < n; i++) maxsub[i] = -Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n-i; j++) {
maxsub[i] = Math.max(maxsub[i], subsum[j]);
subsum[j] = subsum[j+1]+arr[j];
}
}
for (int i = 0; i <= n; i++) {
int ans = 0;
for (int j = 0; j < n; j++) {
ans = Math.max(ans, maxsub[j] + Math.min(i, j+1)*x);
}
out.print(ans+" ");
}
out.println();
}
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static void input_output() throws IOException {
File f = new File("in.txt");
if (f.exists() && !f.isDirectory()) {
in = new Reader(new FileInputStream("in.txt"));
} else in = new Reader();
f = new File("out.txt");
if (f.exists() && !f.isDirectory()) {
out = new PrintWriter(new File("out.txt"));
} else out = new PrintWriter(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 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 | 9d9f0f2718dedd9f003d0cddf3811f6a | 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.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
public class C_Increase_Subarray_Sums {
static long mod = Long.MAX_VALUE;
public static void main(String[] args) {
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
FastReader f = new FastReader();
int t = f.nextInt();
while(t-- > 0){
solve(f, out);
}
out.close();
}
public static void solve(FastReader f, PrintWriter out) {
int n = f.nextInt();
long x = f.nextLong();
int arr[] = f.nextArray(n);
ArrayList<Long> arrli = new ArrayList<>();
arrli.add(0l);
for(int len = 1; len <= n; len++) {
int i = 0;
int j = 0;
long sum = 0;
long maxSum = Long.MIN_VALUE;
while(j < n) {
sum += arr[j];
if(j-i+1 < len) {
j++;
} else {
maxSum = max(maxSum, sum);
sum -= arr[i];
i++;
j++;
}
}
arrli.add(maxSum);
}
long sufix[] = new long[n+1];
sufix[n] = arrli.get(n);
for(int i = n-1; i >= 0; i--) {
sufix[i] = max(arrli.get(i), sufix[i+1]);
}
long maxValue = Long.MIN_VALUE;
for(int i = 0; i <= n; i++) {
long curr = sufix[i] + i*x;
maxValue = max(maxValue, curr);
out.print(maxValue + " ");
}
out.println();
}
// Sort an array
public static void sort(int arr[]) {
ArrayList<Integer> al = new ArrayList<>();
for(int i: arr) {
al.add(i);
}
Collections.sort(al);
for(int i = 0; i < arr.length; i++) {
arr[i] = al.get(i);
}
}
// Find all divisors of n
public static void allDivisors(int n) {
for(int i = 1; i*i <= n; i++) {
if(n%i == 0) {
System.out.println(i + " ");
if(i != n/i) {
System.out.println(n/i + " ");
}
}
}
}
// Check if n is prime or not
public static boolean isPrime(int n) {
if(n < 1) return false;
if(n == 2 || n == 3) return true;
if(n % 2 == 0 || n % 3 == 0) return false;
for(int i = 5; i*i <= n; i += 6) {
if(n % i == 0 || n % (i+2) == 0) {
return false;
}
}
return true;
}
// Find gcd of a and b
public static long gcd(long a, long b) {
long dividend = a > b ? a : b;
long divisor = a < b ? a : b;
while(divisor > 0) {
long reminder = dividend % divisor;
dividend = divisor;
divisor = reminder;
}
return dividend;
}
// Find lcm of a and b
public static long lcm(long a, long b) {
long lcm = gcd(a, b);
long hcf = (a * b) / lcm;
return hcf;
}
// Find factorial in O(n) time
public static long fact(int n) {
long res = 1;
for(int i = 2; i <= n; i++) {
res *= res * i;
}
return res;
}
// Find power in O(logb) time
public static long power(long a, long b) {
long res = 1;
while(b > 0) {
if((b&1) == 1) {
res = (res * a)%mod;
}
a = (a * a)%mod;
b >>= 1;
}
return res;
}
// Find nCr
public static long nCr(int n, int r) {
if(r < 0 || r > n) {
return 0;
}
long ans = fact(n) / (fact(r) * fact(n-r));
return ans;
}
// Find nPr
public static long nPr(int n, int r) {
if(r < 0 || r > n) {
return 0;
}
long ans = fact(n) / fact(r);
return ans;
}
// sort all characters of a string
public static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
// User defined class for 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();
}
boolean hasNext() {
if (st != null && st.hasMoreTokens()) {
return true;
}
String tmp;
try {
br.mark(1000);
tmp = br.readLine();
if (tmp == null) {
return false;
}
br.reset();
} catch (IOException e) {
return false;
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
boolean nextBoolean() {
return Boolean.parseBoolean(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextArray(int n) {
int[] a = new int[n];
for(int i=0; i<n; i++) {
a[i] = nextInt();
}
return a;
}
}
}
/**
Dec Char Dec Char Dec Char Dec Char
--------- --------- --------- ----------
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH (start of heading) 33 ! 65 A 97 a
2 STX (start of text) 34 " 66 B 98 b
3 ETX (end of text) 35 # 67 C 99 c
4 EOT (end of transmission) 36 $ 68 D 100 d
5 ENQ (enquiry) 37 % 69 E 101 e
6 ACK (acknowledge) 38 & 70 F 102 f
7 BEL (bell) 39 ' 71 G 103 g
8 BS (backspace) 40 ( 72 H 104 h
9 TAB (horizontal tab) 41 ) 73 I 105 i
10 LF (NL line feed, new line) 42 * 74 J 106 j
11 VT (vertical tab) 43 + 75 K 107 k
12 FF (NP form feed, new page) 44 , 76 L 108 l
13 CR (carriage return) 45 - 77 M 109 m
14 SO (shift out) 46 . 78 N 110 n
15 SI (shift in) 47 / 79 O 111 o
16 DLE (data link escape) 48 0 80 P 112 p
17 DC1 (device control 1) 49 1 81 Q 113 q
18 DC2 (device control 2) 50 2 82 R 114 r
19 DC3 (device control 3) 51 3 83 S 115 s
20 DC4 (device control 4) 52 4 84 T 116 t
21 NAK (negative acknowledge) 53 5 85 U 117 u
22 SYN (synchronous idle) 54 6 86 V 118 v
23 ETB (end of trans. block) 55 7 87 W 119 w
24 CAN (cancel) 56 8 88 X 120 x
25 EM (end of medium) 57 9 89 Y 121 y
26 SUB (substitute) 58 : 90 Z 122 z
27 ESC (escape) 59 ; 91 [ 123 {
28 FS (file separator) 60 < 92 \ 124 |
29 GS (group separator) 61 = 93 ] 125 }
30 RS (record separator) 62 > 94 ^ 126 ~
31 US (unit separator) 63 ? 95 _ 127 DEL
*/
// (a/b)%mod == (a * moduloInverse(b)) % mod;
// moduloInverse(b) = power(b, mod-2);
| 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 | f8ec941e267aff9c7f3253cda4374721 | 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.*;
public class __ {
private static final long MOD = 1_000_000_007;
private static long binpow(long a, long b, long m) {
if(b == 0) {
return 1%m;
}
long res1 = binpow(a,b/2,m);
if(b%2 == 0) {
return res1 * res1 % m;
} else {
return a * res1 * res1 % m;
}
}
private static long mulInverseUnderModulo(long a, long m) {
return binpow(a, m-2, m);//Fermats little theorm
}
static class FastScanner {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreTokens()) {
try {
st = new StringTokenizer(bf.readLine());
} catch(IOException e) { }
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
String nextLine() throws IOException{
return bf.readLine().trim();
}
}
static class Pair<T extends Comparable<T>, U extends Comparable<U>> implements Comparable<Pair<T,U>> {
T first;
U second;
Pair(){}
Pair(T first, U second) {
this.first = first;
this.second = second;
}
public String toString() {
return this.first + " " + this.second;
}
public int compareTo(Pair<T,U> rhs) {
int firstCompareResult = this.first.compareTo(rhs.first);
if(firstCompareResult == 0) {
return this.second.compareTo(rhs.second);
} else {
return firstCompareResult;
}
}
}
public static void main(String[] args) throws IOException {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
// int t = 1;
while(t-- > 0) {
int n = sc.nextInt(), x = sc.nextInt();
long[] arr = new long[n];
for(int i=0;i<n;i++) arr[i] = sc.nextInt();
// largestSubArr[i] represents largest subarray sum of lengths >= i
long[] largestSubArr = new long[n+1];
for(int i=0;i<=n;i++) {
largestSubArr[i] = Long.MIN_VALUE;
}
//Putting largest sum subarray of a given length
largestSubArr[0] = 0;
for(int i=0;i<n;i++) {
long currSum = 0;
for(int j=i;j<n;j++) {
currSum+=arr[j];
largestSubArr[j-i+1] = Math.max(largestSubArr[j-i+1],currSum);
}
}
//Taking the max prefix to get desired largestSubArr array
for(int i=n-1;i>=0;i--) {
largestSubArr[i] = Math.max(largestSubArr[i], largestSubArr[i+1]);
}
for(int k=1;k<=n;k++) {
largestSubArr[k] = Math.max(k*(long)x + largestSubArr[k], largestSubArr[k-1]);
}
for(long curr: largestSubArr) {
out.print(curr+" ");
}
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 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 | 676f0b878eb4d5d9564a6fba873885e6 | 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.*;
public class Main {
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
public static int cnt;
//-----------MyScanner class for faster input----------
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;
}
}
public static void main(String args[]) {
int mod = 1000000007;
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int x = sc.nextInt();
int a[] = new int[n];
int dp[] = new int[n+1];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
for(int i=1;i<=n;i++){
int psum = 0;
for(int j=0;j<i;j++){
psum += a[j];
}
int mx = psum;
for(int j=i;j<n;j++){
psum += a[j]-a[j-i];
mx = Math.max(mx,psum);
}
dp[i] = mx;
}
for(int i=n-1;i>=0;i--){
for(int j=i+1;j<=n;j++){
dp[i] = Math.max(dp[i],dp[j]);
}
}
for(int i=1;i<=n;i++){
dp[i] = Math.max(dp[i]+x*i,dp[i-1]);
}
for(int y:dp){
out.print(y+" ");
}
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 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 | b2431ff919a39d169411bf34b0d6edf7 | 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 temp {
public static void main(String[] args) throws Exception {;
Scanner scn= new Scanner(System.in);
int t= scn.nextInt();
while(t-->0){
int n= scn.nextInt();
int x=scn.nextInt();
int [] arr = new int[n];
for(int i=0; i<arr.length; i++){
arr[i]= scn.nextInt();
}
int [] maxwin = new int[n+1];
for(int size=1; size<=n; size++){
int max=0;
int sum=0;
for(int i=0; i<size; i++){
sum+=arr[i];
}
max= sum;
int l=0;
int r=size-1;
while(r<n-1){
sum+= arr[r+1];
sum-= arr[l];
l++; r++;
max= Math.max(max, sum);
}
maxwin[size]= max;
}
for(int k=0; k<=n; k++){
int ans=0;
for(int i=1; i<maxwin.length; i++){
if(i>=k){
ans= Math.max(ans, maxwin[i]+k*x);
}
else{
ans= Math.max(ans, maxwin[i]+i*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 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 | 99010e31b0a7b0d9c3057d26fb28105a | 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.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main extends PrintWriter {
static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));Main() { super(System.out); }public static void main(String[] args) throws IOException{ Main d1=new Main ();d1.main();d1.flush(); }
void main() throws IOException {
StringBuilder sb = new StringBuilder();
int t = 1;
t = i(s()[0]);
while (t-- > 0) {
String[] s = s();
int n = i(s[0]); long x = i(s[1]);
int[] a=new int[n];
arri(a, n);
long[] max = new long[n + 1];
long[] sum = new long[n + 1];
for(int i = 0; i < n; i++){
sum[i] += a[i];
if(i > 0) sum[i] += sum[i - 1];
}
for(int i = 1; i <= n; i++) {
for(int j = i - 1; j < n; j++){
if(j - i >= 0) {
max[i] = Math.max(max[i], sum[j] - sum[j - i]);
}else max[i] = sum[j];
}
}
for(int i = 0; i <= n; i++){
long ans = 0;
for(int l = 1; l <= n; l++){
ans = Math.max(ans, max[l] + x * Math.min(i, l));
}
sb.append(ans + " ");
}sb.append("\n");
}
System.out.println(sb);
}
public static boolean go(long num, HashMap<Long, Integer> cnt) {
// System.out.println(num + " " + cnt.containsKey(num));
if(num == 0) return false;
if(cnt.containsKey(num)) {
cnt.put(num, cnt.getOrDefault(num, 0) - 1);
if(cnt.get(num) == 0) cnt.remove(num);
return true;
}
return go(num / 2, cnt) && go((num + 1) / 2, cnt);
}
public static void dfs(int i,ArrayList<Integer>[] adj,int[] vis){
vis[i]=1;
if(adj[i]==null) return;
for(Integer j:adj[i]){
if(vis[j]==0){
dfs(j,adj,vis);
}
}
}
static String[] s() throws IOException { return s.readLine().trim().split("\\s+"); }
static int i(String ss) {return Integer.parseInt(ss); }
static long l(String ss) {return Long.parseLong(ss); }
public void arr(long[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=l(s2[i]); }}
public void arri(int[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=(i(s2[i])); }}
}class Pair{
int start, end;
public Pair(long a, long b){
this.start = (int)a ; this.end = (int)b;
}
}
| 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 | 9099869094f93afc28365e4c074658ee | 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 Scanner obj = new Scanner(System.in);
public static PrintWriter out = new PrintWriter(System.out);
public static void debug(long[] a)
{
int n=a.length;
for(int i=0;i<n;i++)out.print(a[i]+" ");
out.println();
}
public static void main(String[] args) {
int len = obj.nextInt();
while (len-- != 0) {
int n = obj.nextInt();
int x=obj.nextInt();
long[] a=new long[n];
long[] dp=new long[n];
for(int i=0;i<n;i++)a[i]=obj.nextLong();
long sum=0,max=0;
for(int i=0;i<n;i++)
{
sum+=a[i];
max=Math.max(max, sum);
if(sum<0)sum=0;
dp[i]=sum;
}
out.print(max+" ");
long ans=0;
for(int k=1;k<=n;k++)
{
long tut=0,best=0;
for(int i=0;i<k;i++)tut+=a[i];
best=tut;
for(int i=k;i<n;i++)
{
tut-=a[i-k];
tut+=a[i];
best=Math.max(best, tut+dp[i-k]);
}
ans=Math.max(ans, best+(k*x));
out.print(ans+" ");
}
out.println();
}
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 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 | 39d442d172f85d07cccf30021b4c751f | 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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class C {
static class RealScanner {
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 nextLong() {
return Long.parseLong(next());
}
public boolean isSorted(List<Long> list) {
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i) > list.get(i + 1))
return false;
}
return true;
}
}
public static void main(String[] args) {
RealScanner sc = new RealScanner();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
long n = sc.nextLong();
long x = sc.nextLong();
List<Long> list = new ArrayList<>();
long[] dp_arr = new long[(int) (n + 1)];
for (int i = 0; i < n; i++) {
list.add(sc.nextLong());
dp_arr[i] = -Long.MAX_VALUE;
}
dp_arr[(int) n] = -Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
long count = 0;
int j = i;
for (; j < n; j++) {
count += list.get(j);
long max = Math.max(count, dp_arr[j - i + 1]);
dp_arr[j - i + 1] = max;
// System.out.println(max);
// System.out.println(Arrays.toString(dp_arr) + " " + dp_arr[j - i + 1]);
}
}
for (int i = 0; i <= n; i++) {
long res = 0;
int j = 1;
for (; j <= n; j++) {
long max = Math.max(dp_arr[(int) j] + (Math.min(i, j) * x), res);
res = max;
// System.out.println(max);
// System.out.println(dp_arr[j]+" ");
}
out.print(res + " ");
}
out.println();
}
out.flush();
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 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 | d71da36c0f18d9bd0de79e84c7e65eaf | 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 k=sc.nextInt();
int arr[]=new int[n];
long highest[]=new long[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
highest[i]=-1111111;
}
long sum=0;
for(int i=0;i<n;i++) {
for(int j=i,pos=0;j>=0;j--,pos++) {
sum+=arr[j];
// System.out.println(sum+" sum");
if(highest[pos]==-1111111) {
highest[pos]=sum;
}
else {
highest[pos]=Math.max(highest[pos], sum);
}
}
sum=0;
}
// System.out.println(Arrays.toString(highest));
for(int i=n-2;i>=0;i--) {
highest[i]=Math.max(highest[i],highest[i+1]);
}
// System.out.println(Arrays.toString(highest));
if(highest[0]<0) {
System.out.print(0+" ");
}
else {
System.out.print(highest[0]+" ");
}
for(int i=0;i<n;i++) {
if(i==0) {
highest[i]=highest[i]+(i+1*k);
}
else {
highest[i]=Math.max(highest[i-1],highest[i]+((i+1)*k));
}
}
for(int i=0;i<n;i++) {
if(highest[i]<0) {
highest[i]=0;
}
System.out.print(highest[i]+" ");
}
System.out.println();
// System.out.println(Arrays.toString(highest));
}
}
}
| 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 | 831b6f13b60d040f5486b1c5d2f97a1b | 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.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.security.cert.X509CRL;
import java.util.*;
import java.lang.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
@SuppressWarnings("unused")
public class Main {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static String OUTPUT = "";
//global
private final static long BASE = 998244353L;
private final static int ALPHABET = (int)('z') - (int)('a') + 1;
//private final static int BASE = 1000000007;
private final static int INF_I = (1<<31)-1;
private final static long INF_L = (1l<<63)-1;
private final static int MAXN = 200100;
private final static int MAXK = 31;
private final static int[] DX = {-1,0,1,0};
private final static int[] DY = {0,1,0,-1};
static void solve() {
int ntest = readInt();
for (int test=0;test<ntest;test++) {
int N = readInt(), X = readInt();
int[] A = readIntArray(N);
long[] S = new long[N+1];
Arrays.fill(S, -INF_L);
for (int l=0;l<N;l++) {
long s = 0;
for (int r=l;r<N;r++) {
s += 1l*A[r];
S[r-l+1] = Math.max(S[r-l+1], s);
}
}
long[] ans = new long[N+1];
long ma=-INF_L;
long res = -INF_L;
for (int k=N;k>=0;k--) {
ma = Math.max(ma, S[k]);
ans[k] = ma + 1l*k*X;
}
ans[0] = Math.max(ans[0], 0);
for (int k=1;k<=N;k++) ans[k] = Math.max(ans[k-1], ans[k]);
for (int i=0;i<=N;i++) out.print(ans[i] + " ");
out.println();
}
}
public static void main(String[] args) throws Exception
{
long S = System.currentTimeMillis();
if (INPUT=="") {
is = System.in;
} else {
File file = new File(INPUT);
is = new FileInputStream(file);
}
if (OUTPUT == "") out = new PrintWriter(System.out);
else out = new PrintWriter(OUTPUT);
solve();
out.flush();
long G = System.currentTimeMillis();
}
private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> {
private T x;
private T y;
public Point(T x, T y) {
this.x = x;
this.y = y;
}
public T getX() {return x;}
public T getY() {return y;}
@Override
public int compareTo(Point<T> o) {
int cmp = x.compareTo(o.getX());
if (cmp==0) return y.compareTo(o.getY());
return cmp;
}
}
private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> {
public ClassComparator() {}
@Override
public int compare(T a, T b) {
return a.compareTo(b);
}
}
private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> {
public ListComparator() {}
@Override
public int compare(List<T> o1, List<T> o2) {
for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) {
int c = o1.get(i).compareTo(o2.get(i));
if (c != 0) {
return c;
}
}
return Integer.compare(o1.size(), o2.size());
}
}
private static boolean eof()
{
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
try {
is.mark(1000);
while(true){
int b = is.read();
if(b == -1){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;
private static int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
// private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private static double readDouble() { return Double.parseDouble(readString()); }
private static char readChar() { return (char)skip(); }
private static String readString()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static char[] readChar(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] readTable(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = readChar(m);
return map;
}
private static int[] readIntArray(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = readInt();
return a;
}
private static long[] readLongArray(int n) {
long[] a = new long[n];
for (int i=0;i<n;i++) a[i] = readLong();
return a;
}
private static int readInt()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static long readLong()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
} | 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 | 947a13f010adaaf3fa9c5421d05a1f5f | 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.*;
public class Main implements Runnable {
public void solve() {
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
for(int z = 0; z < t; ++z) {
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][n + 1];
dp[0][0] = max(0, a[0]);
dp[0][1] = max(0, a[0] + x);
for(int i = 0; i < n - 1; ++i) {
for(int j = 0; j <= i + 1; ++j) {
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a[i + 1]);
dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + a[i + 1] + x);
}
}
long ans[] = new long[n + 1];
long cans = 0;
for(int i = 0; i <= n; ++i) {
for(int j = 0; j < n; ++j) {
cans = max(cans, dp[j][i]);
}
out.print(cans + " ");
}
out.println();
}
out.close();
}
public static void main(String[] args) {
new Thread(null, new Main(), "Main", 1 << 26).start();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
String str = br.readLine();
if(str == null)
throw new InputMismatchException();
st = new StringTokenizer(str);
} 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();
}
if(str == null)
throw new InputMismatchException();
return str;
}
}
public void run() {
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 | 3cebcc3bd4822cd1e991e85e5104704b | 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.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.StringTokenizer;
public class Solution {
static HashSet<String> set;
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out);
FastScanner fs = new FastScanner();
DecimalFormat formatter = new DecimalFormat("#0.000000");
int ti = fs.nextInt();
// int ti = 1;
outer: while (ti-- > 0) {
int n = fs.nextInt();
int x = fs.nextInt();
int[] a = fs.readArray(n);
int[] ans = new int[n];
int[] pre = new int[n + 1];
pre[0] = 0;
for (int i = 0; i < n; i++) {
pre[i + 1] = pre[i] + a[i];
}
for (int i = 0; i < n; i++) {
int max = Integer.MIN_VALUE;
for (int j = 0; j < n - i; j++) {
// System.out.println(j + i - 1);
max = Math.max(max, pre[j + i + 1] - pre[j]);
}
ans[i] = max;
}
for (int j = 0; j <= n; j++) {
int max = 0;
for (int i = 0; i < n; i++) {
max = Math.max(max, ans[i] + Math.min(i + 1, j) * x);
}
out.print(max + " ");
}
out.println();
}
out.close();
}
static final int mod = 1_000_000_007;
static void sort(int[] a) {
Random random = new Random();
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 int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
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());
}
float nextFloat() {
return Float.parseFloat(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());
}
}
static class Pair {
int a;
int b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
}
}
| 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 | a20d97905af71d4832fb044af0871739 | 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 D {
public static void main(String[] args)throws IOException {
FastReader sc = new FastReader();
StringBuilder sb = new StringBuilder();
int t = sc.nextInt();
out :while(t-- >0) {
int n = sc.nextInt();
int x = sc.nextInt();
int[] arr = sc.readArray(n);
long[] maxSum = new long[n+1];
Arrays.fill(maxSum, Integer.MIN_VALUE);
maxSum[0]=0;
for(int i =0;i<n;i++) {
long sum = 0;
for(int j = i;j<n;j++) {
sum += arr[j];
maxSum[j-i+1] = Math.max(maxSum[j-i+1], sum);
}
}
for(int k =0;k<=n;k++) {
long max = Integer.MIN_VALUE;
for(int i =0;i<=n;i++) {
max = Math.max(maxSum[i] + x*(Math.min(k, i)) , max);
}
sb.append(max).append(" ");
}
sb.append("\n");
}
System.out.println(sb);
}
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());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
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 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 | a2fc6f878e008a88000efe4e8a256e94 | 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.*;
import java.util.*;
// @author : Dinosparton
public class test {
static class Pair{
long x;
long y;
Pair(long x,long y){
this.x = x;
this.y = y;
}
}
static class Sort implements Comparator<Pair>
{
@Override
public int compare(Pair a, Pair b)
{
if(a.x!=b.x)
{
return (int)(a.x - b.x);
}
else
{
return (int)(a.y-b.y);
}
}
}
static class Compare {
void compare(Pair arr[], int n)
{
// 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.x!=p2.x) {
return (int)(p1.x - p2.x);
}
else {
return (int)(p1.y - p2.y);
}
}
});
// for (int i = 0; i < n; i++) {
// System.out.print(arr[i].x + " " + arr[i].y + " ");
// }
// System.out.println();
}
}
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;
}
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner();
StringBuilder res = new StringBuilder();
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt();
int x = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++) {
a[i] = sc.nextInt();
}
int window[] = new int[n+1];
for(int i=0;i<=n;i++) {
int sum = 0;
for(int j=0;j<i;j++) {
sum += a[j];
}
window[i] = sum;
for(int j=i;j<n;j++) {
sum -= a[j-i];
sum += a[j];
window[i] = Math.max(sum, window[i]);
}
}
// for(int i=0;i<=n;i++) {
// System.out.print(window[i]+" ");
// }
// System.out.println();
int max = window[n];
for(int i = n-1;i>=0;i--) {
max = Math.max(max, window[i]);
window[i] = max;
}
for(int i=0;i<=n;i++) {
window[i] += i*x;
}
max = 0;
for(int i=0;i<=n;i++) {
max = Math.max(max, window[i]);
window[i] = max;
}
for(int i : window) {
res.append(i+" ");
}
res.append("\n");
}
System.out.println(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 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 | b8ac833b172a7ca3f0a36c58f9718f84 | 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.*;
import static java.lang.Math.*;
import static java.util.Arrays.sort;
public class Round12 {
public static void main(String[] args) {
FastReader fastReader = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = fastReader.nextInt();
while (t-- > 0) {
int n = fastReader.nextInt();
int x = fastReader.nextInt();
int a[] = fastReader.ria(n);
HashMap<Integer, Long> map = new HashMap<>();
map.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 (map.containsKey(len)) {
map.put(len, max(sum, map.get(len)));
} else {
map.put(len, sum);
}
}
}
StringBuilder ans_b = new StringBuilder();
for (int i = 0; i <= n; i++) {
long ans = 0;
for (int j = 0; j <= n; j++) {
ans = max(ans, j < i ? map.get(j) + (long) j * x : map.get(j) + (long) i * x);
}
ans_b.append(ans).append(" ");
}
out.println(ans_b);
}
out.close();
}
static int[] shift(int a[], int d, int last) {
int[] cur = copy(a);
for (int i = 0; i < d; i++) {
int temp = cur[last];
for (int j = last; j > 0; j--) {
cur[j] = cur[j - 1];
}
cur[0] = temp;
}
return cur;
}
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final long LMAX = 9223372036854775807L;
static Random __r = new Random();
// math util
static int minof(int a, int b, int c) {
return min(a, min(b, c));
}
static int minof(int... x) {
if (x.length == 1) return x[0];
if (x.length == 2) return min(x[0], x[1]);
if (x.length == 3) return min(x[0], min(x[1], x[2]));
int min = x[0];
for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i];
return min;
}
static long minof(long a, long b, long c) {
return min(a, min(b, c));
}
static long minof(long... x) {
if (x.length == 1) return x[0];
if (x.length == 2) return min(x[0], x[1]);
if (x.length == 3) return min(x[0], min(x[1], x[2]));
long min = x[0];
for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i];
return min;
}
static int maxof(int a, int b, int c) {
return max(a, max(b, c));
}
static int maxof(int... x) {
if (x.length == 1) return x[0];
if (x.length == 2) return max(x[0], x[1]);
if (x.length == 3) return max(x[0], max(x[1], x[2]));
int max = x[0];
for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i];
return max;
}
static long maxof(long a, long b, long c) {
return max(a, max(b, c));
}
static long maxof(long... x) {
if (x.length == 1) return x[0];
if (x.length == 2) return max(x[0], x[1]);
if (x.length == 3) return max(x[0], max(x[1], x[2]));
long max = x[0];
for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i];
return max;
}
static int powi(int a, int b) {
if (a == 0) return 0;
int ans = 1;
while (b > 0) {
if ((b & 1) > 0) ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
static long powl(long a, int b) {
if (a == 0) return 0;
long ans = 1;
while (b > 0) {
if ((b & 1) > 0) ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
static int fli(double d) {
return (int) d;
}
static int cei(double d) {
return (int) ceil(d);
}
static long fll(double d) {
return (long) d;
}
static long cel(double d) {
return (long) ceil(d);
}
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static int[] exgcd(int a, int b) {
if (b == 0) return new int[]{1, 0};
int[] y = exgcd(b, a % b);
return new int[]{y[1], y[0] - y[1] * (a / b)};
}
static long[] exgcd(long a, long b) {
if (b == 0) return new long[]{1, 0};
long[] y = exgcd(b, a % b);
return new long[]{y[1], y[0] - y[1] * (a / b)};
}
static int randInt(int min, int max) {
return __r.nextInt(max - min + 1) + min;
}
static long mix(long x) {
x += 0x9e3779b97f4a7c15L;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebL;
return x ^ (x >> 31);
}
public static boolean[] findPrimes(int limit) {
assert limit >= 2;
final boolean[] nonPrimes = new boolean[limit];
nonPrimes[0] = true;
nonPrimes[1] = true;
int sqrt = (int) Math.sqrt(limit);
for (int i = 2; i <= sqrt; i++) {
if (nonPrimes[i]) continue;
for (int j = i; j < limit; j += i) {
if (!nonPrimes[j] && i != j) nonPrimes[j] = true;
}
}
return nonPrimes;
}
// array util
static void reverse(int[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
int swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(long[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
long swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(double[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
double swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(char[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
char swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void shuffle(int[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
int swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(long[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
long swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(double[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
double swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void rsort(int[] a) {
shuffle(a);
sort(a);
}
static void rsort(long[] a) {
shuffle(a);
sort(a);
}
static void rsort(double[] a) {
shuffle(a);
sort(a);
}
static int[] copy(int[] a) {
int[] ans = new int[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static long[] copy(long[] a) {
long[] ans = new long[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static double[] copy(double[] a) {
double[] ans = new double[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static char[] copy(char[] a) {
char[] ans = new char[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
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());
}
int[] ria(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = Integer.parseInt(next());
return a;
}
long nextLong() {
return Long.parseLong(next());
}
long[] rla(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = Long.parseLong(next());
return a;
}
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 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 | 891c799dc922bfc92a859c4ab90f3e9c | 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 Main{
public static void main(String[]args){
long s = System.currentTimeMillis();
new Solver().run();
System.err.println(System.currentTimeMillis()-s+"ms");
}
}
class Solver{
final long mod = (long)1e9+7l;
final boolean DEBUG = true, MULTIPLE_TC = true;
FastReader sc;
PrintWriter out;
final long oo = (long)1e16;
int N;
long X, arr[], res[];
void init(){
N = ni();
X = ni();
arr = new long[N + 1];
res = new long[N + 1];
Arrays.fill(res, -oo);
for(int i = 1; i <= N; i++){
arr[i] = nl();
}
}
void process(int testNumber){
init();
for(int i = 1; i <= N; i++){
long sum = 0l;
for(int j = i, len = 1; j <= N; j++, len++){
sum += arr[j];
res[len] = Math.max(res[len], sum);
}
}
for(int i = N - 1; i >= 0; i--){
res[i] = Math.max(res[i + 1], res[i]);
}
for(int i = 0; i <= N; i++){
res[i] += i * X;
if(i > 0)
res[i] = Math.max(res[i], res[i - 1]);
res[i] = Math.max(res[i], 0l);
p(res[i] + " ");
}
p("\n");
}
void run(){
sc = new FastReader();
out = new PrintWriter(System.out);
int t = MULTIPLE_TC ? ni() : 1;
for(int test = 1; test <= t; test++){
process(test);
}
out.flush();
}
void trace(Object... o){ if(!DEBUG) return; System.err.println(Arrays.deepToString(o)); };
void pn(Object o){ out.println(o); }
void p(Object o){ out.print(o); }
int ni(){ return Integer.parseInt(sc.next()); }
long nl(){ return Long.parseLong(sc.next()); }
double nd(){ return Double.parseDouble(sc.next()); }
String nln(){ return sc.nextLine(); }
long gcd(long a, long b){ return (b==0)?a:gcd(b,a%b);}
int gcd(int a, int b){ return (b==0)?a:gcd(b,a%b); }
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();
}
String nextLine(){
String str = "";
try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
return str;
}
}
}
class pair implements Comparable<pair> {
int first, second;
public pair(int first, int second){
this.first = first;
this.second = second;
}
@Override
public int compareTo(pair ob){
if(this.first != ob.first)
return this.first - ob.first;
return this.second - ob.second;
}
@Override
public String toString(){
return this.first + " " + this.second;
}
static public pair from(int f, int s){
return new pair(f, 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 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 | fc21ce460dff799534b93bb7d4c0e9c2 | 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;
import java.lang.Exception;
public class IncreaseSubarraySums{
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null||!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(){
String str="";
try{
if(st.hasMoreElements()){
str=st.nextToken("\n");
}else{
str=br.readLine();
}
}catch(IOException e){
e.printStackTrace();
}
return str;
}
}
static int max(int a,int b){
if(a>b) return a;
else return b;
}
public static void main(String args[])throws java.lang.Exception{
FastReader sc=new FastReader();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int x=sc.nextInt();
int[] arr=new int[n+1];
for(int i=1;i<n+1;i++){
int a=sc.nextInt();
arr[i]=a+x;
}
for(int i=2;i<n+1;i++) arr[i]+=arr[i-1];
int[] arr2=new int[n+1];
for(int i=1;i<n+1;i++){
int maxn=0;
for(int j=i;j<n+1;j++){
int val=arr[j]-arr[j-i];
if(val>maxn) maxn=val;
}
arr2[i]=maxn;
}
arr2[0]=0;
for(int i=0;i<n;i++){
if(arr2[i]>arr2[i+1]) arr2[i+1]=arr2[i];
}
int[] arr3=new int[n+1];
arr3[n]=arr2[n];
for(int i=n-1;i>=0;i--){
arr3[i]=max(arr2[i],arr3[i+1]-x);
}
for(int i=0;i<=n;i++){
System.out.print(arr3[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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.