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 | e665905215a534ff8753b9c9e60c04ad | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Locale;
import java.util.StringTokenizer;
public class Solution implements Runnable {
private PrintStream out;
private BufferedReader in;
private StringTokenizer st;
public void solve() throws IOException {
long time0 = System.currentTimeMillis();
int t = nextInt();
for (int test = 1; test <= t; test++) {
int n = nextInt();
int k = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
long answer = solve(n, k, a);
out.println(answer);
}
System.err.println("time: " + (System.currentTimeMillis() - time0));
}
private long solve(int n, int k, int[] a) {
if (n <= k) {
return solveAll(n, k, a);
}
long answer = 1L * k * (k - 1) / 2;
long[] sum = new long[n + 1];
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + a[i - 1];
}
long max = 0;
for (int i = 0; i + k <= n; i++) {
max = Math.max(max, sum[i + k] - sum[i]);
}
answer = answer + max;
return answer;
}
private long solveAll(int n, int k, int[] a) {
int wait = k - n;
long answer = 0;
for (int i = 0; i < n; i++) {
answer = answer + wait + a[i] + i;
}
return answer;
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
@Override
public void run() {
try {
solve();
out.close();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
public Solution(String name) throws IOException {
Locale.setDefault(Locale.US);
if (name == null) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintStream(new BufferedOutputStream(System.out));
} else {
in = new BufferedReader(new InputStreamReader(new FileInputStream(name + ".in")));
out = new PrintStream(new BufferedOutputStream(new FileOutputStream(name + ".out")));
}
st = new StringTokenizer("");
}
public static void main(String[] args) throws IOException {
new Thread(new Solution(null)).start();
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | d8017f605dd12c5281ab983dfae44b77 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class TheEnchantedForest {
static int mod = 1000000007;
public static void main(String[] args) throws IOException {
FastReader reader = new FastReader();
FastWriter writer = new FastWriter();
int numTests = reader.readSingleInt();
for(int t = 0; t<numTests; t++){
int[] nk = reader.readIntArray(2);
int n = nk[0], k = nk[1];
int[] a = reader.readIntArray(n);
long[] prefix = new long[n];
prefix[0] = a[0];
for(int i = 1; i<n; i++){
prefix[i] = prefix[i-1] + a[i];
}
if(k >= n) {
/*
long val = prefix[n-1];
long m = n-1;
long add = (m > 0) ? (m*(m+1))/2 + ((k-n)/(m))*(m*(m+1)) : k-n;
long l = (m > 0) ? (k-n)%m : 0;
add += l*(l+1);
long ans = val + add;
*/
long val = prefix[n-1];
long m = n-1;
long add = (m*(m+1))/2;
long ans = val + add + (long)(k-n)*n;
writer.writeSingleLong(ans);
continue;
}
long max = 0;
// 0 1 2 3 4 5
for(int i = 0; i<=n-k; i++){
long sum = (i > 0) ? prefix[i+k-1]-prefix[i-1] : prefix[i+k-1];
max = (sum > max) ? sum : max;
}
k--;
max += ((long)k*(long)(k+1))/2;
writer.writeSingleLong(max);
}
}
public static void mergeSort(int[] a, int n) {
if (n < 2) {
return;
}
int mid = n / 2;
int[] l = new int[mid];
int[] r = new int[n - mid];
for (int i = 0; i < mid; i++) {
l[i] = a[i];
}
for (int i = mid; i < n; i++) {
r[i - mid] = a[i];
}
mergeSort(l, mid);
mergeSort(r, n - mid);
merge(a, l, r, mid, n - mid);
}
public static void merge(int[] a, int[] l, int[] r, int left, int right) {
int i = 0, j = 0, k = 0;
while (i < left && j < right) {
if (l[i] <= r[j]) {
a[k++] = l[i++];
}
else {
a[k++] = r[j++];
}
}
while (i < left) {
a[k++] = l[i++];
}
while (j < right) {
a[k++] = r[j++];
}
}
public static class FastReader {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer;
public int readSingleInt() throws IOException {
return Integer.parseInt(reader.readLine());
}
public int[] readIntArray(int numInts) throws IOException {
int[] nums = new int[numInts];
tokenizer = new StringTokenizer(reader.readLine());
for(int i = 0; i<numInts; i++){
nums[i] = Integer.parseInt(tokenizer.nextToken());
}
return nums;
}
public String readString() throws IOException {
return reader.readLine();
}
}
public static class FastWriter {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
public void writeSingleInteger(int i) throws IOException {
writer.write(Integer.toString(i));
writer.newLine();
writer.flush();
}
public void writeSingleLong(long i) throws IOException {
writer.write(Long.toString(i));
writer.newLine();
writer.flush();
}
public void writeIntArrayWithSpaces(int[] nums) throws IOException {
for(int i = 0; i<nums.length; i++){
writer.write(nums[i] + " ");
}
writer.newLine();
writer.flush();
}
public void writeIntArrayListWithSpaces(ArrayList<Integer> nums) throws IOException {
for(int i = 0; i<nums.size(); i++){
writer.write(nums.get(i) + " ");
}
writer.newLine();
writer.flush();
}
public void writeIntArrayWithoutSpaces(int[] nums) throws IOException {
for(int i = 0; i<nums.length; i++){
writer.write(Integer.toString(nums[i]));
}
writer.newLine();
writer.flush();
}
public void writeString(String s) throws IOException {
writer.write(s);
writer.newLine();
writer.flush();
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | aaba72d8b73839e574f500baae278769 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.*;
/**
* @author KaiXin
* @version 11
* @date 2022-07-12 20:06
*/
public class TaskC {
static final int SIZE = (int)2e5 + 5;
static long[] a = new long[SIZE];
static long[] sum = new long[SIZE];
public static void solve(InputReader in,PrintWriter out) {
int n = in.nextInt();
long k = in.nextLong();
for(int i = 1; i <= n; ++i){
a[i] = in.nextLong();
sum[i] = 0;
sum[i] = sum[i - 1] + a[i];
}
long ans = 0;
if(k >= n){
ans += sum[n] + n * (k - n);
ans += (n - 1L) * n / 2;
}
else {
long temp = (k - 1L) * k / 2;
for(int i = 1; i <= n - k + 1; ++i){
ans = Math.max(ans,temp + sum[i + (int)k - 1] - sum[i - 1]);
}
}
out.println(ans);
}
public static void init(){
}
public static void clear(int n){
}
public static void main(String[] args) {
init();
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int T = 1;
T = in.nextInt();
for (int i = 1; i <= T; ++i) {
solve(in,out);
}
out.close();
}
private static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
/*
*/ | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 86af2d60f39519b14125322a1211be74 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.*;
import java.lang.*;
public class matrix{
public static void main(String ags[]){
Scanner sc=new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0){
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
for (int i = 0; i < n; i++)
a[i]=sc.nextInt();
if (k > n) {
for (int i = 0; i < n; i++)
a[i] += k - n;
k = n;
}
long ans = 0;
long sum = 0;
for (int i = 0; i < k; i++)
sum += a[i];
ans = Math.max(ans, sum);
for (int i = k; i < n; i++) {
sum += a[i] - a[i - k];
ans = Math.max(ans, sum);
}
ans += (long)k * (k - 1) / 2;
System.out.println(ans);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | d9edefbd851fdb8fa93fa8334ad48ab9 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.*;
import java.lang.*;
public class matrix{
public static void main(String ags[]){
Scanner sc=new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0){
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
for (int i = 0; i < n; i++)
a[i]=sc.nextInt();
if (k > n) {
for (int i = 0; i < n; i++)
a[i] += k - n;
k = n;
}
long ans = 0;
long sum = 0;
for (int i = 0; i < k; i++)
sum += a[i];
ans = Math.max(ans, sum);
for (int i = k; i < n; i++) {
sum += a[i] - a[i - k];
ans = Math.max(ans, sum);
}
ans += (long)k * (k - 1) / 2;
System.out.println(ans);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | eedbcc7a17cf171df75fceb494c34032 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.*;
import java.lang.*;
public class matrix{
public static void main(String ags[]){
Scanner sc=new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0){
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
for (int i = 0; i < n; i++)
a[i]=sc.nextInt();
if (k > n) {
for (int i = 0; i < n; i++)
a[i] += k - n;
k = n;
}
long ans = 0;
long sum = 0;
for (int i = 0; i < k; i++)
sum += a[i];
ans = Math.max(ans, sum);
for (int i = k; i < n; i++) {
sum += a[i] - a[i - k];
ans = Math.max(ans, sum);
}
ans += (long)k * (k - 1) / 2;
System.out.println(ans);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | c3c48bd69e7267ab73701d6a65c1c737 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.*;
import java.lang.*;
public class matrix{
public static void main(String ags[]){
Scanner sc=new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0){
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
for (int i = 0; i < n; i++)
a[i]=sc.nextInt();
if (k > n) {
for (int i = 0; i < n; i++)
a[i] += k - n;
k = n;
}
long ans = 0;
long sum = 0;
for (int i = 0; i < k; i++)
sum += a[i];
ans = Math.max(ans, sum);
for (int i = k; i < n; i++) {
sum += a[i] - a[i - k];
ans = Math.max(ans, sum);
}
ans += (long)k * (k - 1) / 2;
System.out.println(ans);
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 78733e5bc2f0acb3b9f74a7a319a2ab6 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 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));
int t = Integer.parseInt(br.readLine());
while(t --> 0) {
Integer[] nk = Arrays.stream(br.readLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new);
Integer[] a = Arrays.stream(br.readLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new);
long sum = 0;
int i;
for (i = 0; i < nk[0] && i < nk[1]; i++) {
sum += a[i];
}
long maxStreak = sum;
for(; i < nk[0]; i++){
sum -= a[i-nk[1]];
sum += a[i];
maxStreak = Math.max(maxStreak, sum);
}
if(nk[1] > nk[0]){
long ans = (long) nk[0] *nk[1] - ((long) (nk[0]) *(nk[0]+1)/2);
System.out.println(ans + sum);
} else {
System.out.println(maxStreak + (((long) nk[1] * (nk[1] - 1)) / 2));
}
}
br.close();
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 1b10eec3bd33c3f34d2b6a4896b9dfc7 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | fe1b6ed8d254ec4ffdb311d052d0c883 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.StringTokenizer;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Array;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.ArrayList;
public class p5
{
BufferedReader br;
StringTokenizer st;
BufferedWriter bw;
public static void main(String[] args)throws Exception
{
new p5().run();
}
void run()throws IOException
{
br = new BufferedReader(new InputStreamReader(System.in));
bw=new BufferedWriter(new OutputStreamWriter(System.out));
solve();
}
void solve() throws IOException
{
short t=ns();
while(t-->0)
{
int n=ni();
int k=ni();
int a[]=nai(n);
if(n==1)
System.out.println(a[0]+k-1);
else if(k<=n)
{
long max=0;
for(int i=-1;++i<k;)
max+=a[i];
long b=max;
for(int i=k-1;++i<n;)
{
b+=a[i];
b-=a[i-k];
if(max<b)
max=b;
}
max+=((long)k*(k-1))/2L;
System.out.println(max);
}
else
{
long sum=0;
for(int i=-1;++i<n;)
sum+=a[i];
int x=(k-1)%(n-1);
int y=(k-1)/(n-1);
y--;
long z=n-1+x;
sum+=((long)z*(z+1))/2L;
sum-=((long)x*(x-1))/2L;
z=(long)n*(n-1);
sum+=(long)y*z;
System.out.println(sum);
}
}
}
public int gcd(int a, int b)
{
if(a==0)
return b;
else
return gcd(b%a,a);
}
int[] nai(int n) { int a[]=new int[n]; for(int i=-1;++i<n;)a[i]=ni(); return a;}
Integer[] naI(int n) { Integer a[]=new Integer[n]; for(int i=-1;++i<n;)a[i]=ni(); return a;}
long[] nal(int n) { long a[]=new long[n]; for(int i=-1;++i<n;)a[i]=nl(); return a;}
char[] nac() {char c[]=nextLine().toCharArray(); return c;}
char [][] nmc(int n) {char c[][]=new char[n][]; for(int i=-1;++i<n;)c[i]=nac(); return c;}
int[][] nmi(int r, int c) {int a[][]=new int[r][c]; for(int i=-1;++i<r;)a[i]=nai(c); return a;}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] intArray(Integer[] a) {
int n=a.length;
int b[]=new int[n];
for(int i=-1;++i<n;)
b[i]=a[i].intValue();
return b;
}
int ni() { return Integer.parseInt(next()); }
byte nb() { return Byte.parseByte(next()); }
short ns() { return Short.parseShort(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
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;
}
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 int[] primeNO(long limit)
{
int size=(int)Math.sqrt(limit)+1;
int size1=size/2;
boolean composite[]=new boolean[size1];
int zz=(int)Math.sqrt(size-1);
for(int i=3;i<=zz;)
{
for(int j=(i*i)/2;j<size1;j+=i)
composite[j]=true;
for(i+=2;composite[i/2];i+=2);
}
int prime[]=new int[3401];
prime[0]=2;
int p=1;
for(int i=1;i<size1;i++)
{
if(!composite[i])
prime[p++]=2*i+1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// System.out.println(p);
////////////////////////////////////////////////////////////////////////////////////////////////////////
prime=Arrays.copyOf(prime, p);
return prime;
}
public static class queue
{
public static class node
{
node next;
int val;
public node(int val)
{
this.val=val;
}
}
node head,tail;
public void add(int val)
{
node a=new node(val);
if(head==null)
{
head=tail=a;
return;
}
tail.next=a;
tail=a;
}
public int remove()
{
int a=head.val;
head=head.next;
if(head==null)
tail=null;
return a;
}
}
public static class stack
{
public static class node
{
node next;
int val;
public node(int val)
{
this.val=val;
}
}
node head;
public void add(int val)
{
node a=new node(val);
a.next=head;
head=a;
}
public int remove()
{
int a=head.val;
head=head.next;
return a;
}
}
public static class data
{
int a,b;
public data(int a, int b)
{
this.a=a;
this.b=b;
}
}
public data[] dataArray(int n)
{
data d[]=new data[n];
for(int i=-1;++i<n;)
d[i]=new data(ni(), ni());
return d;
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 42cdfe669f7303984ba364bf1cc1dbea | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in Actual solution is at the top
*
* @author TopSafder
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int t = in.nextInt();
for (int i = 0; i < t; i++) {
solve(in, out);
}
}
private void solve(InputReader in, OutputWriter out) {
long n = in.nextInt();
long k = in.nextInt();
long[] A = new long[(int) n];
long[] B = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = in.nextInt();
B[i] += A[i];
if (i > 0) {
B[i] += B[i - 1];
}
}
long sum = 0;
if (k >= n) {
sum = (k - n) * n + ((n - 1) * (n) / 2) + B[(int) n - 1];
} else {
sum = B[(int) k - 1];
for (int i = (int) k; i < n; i++) {
sum = Math.max(sum, B[i] - B[i - (int) k]);
}
sum += ((k - 1) * (k)) / 2;
}
out.println(sum);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(long i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.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 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) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 65dac751a85cdaa531ab123532f7415d | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.*;
public class TheEnchantedForest {
public static void main(String[] args) throws IOException {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(sc.readLine());
while(t --> 0) {
Integer[] nk = Arrays.stream(sc.readLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new);
Integer[] a = Arrays.stream(sc.readLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new);
long sum = 0;
int i;
for (i = 0; i < nk[0] && i < nk[1]; i++) {
sum += a[i];
}
long maxStreak = sum;
for(; i < nk[0]; i++){
sum -= a[i-nk[1]];
sum += a[i];
maxStreak = Math.max(maxStreak, sum);
}
if(nk[1] > nk[0]){
long ans = (long) nk[0] *nk[1] - ((long) (nk[0]) *(nk[0]+1)/2);
System.out.println(ans + sum);
} else {
System.out.println(maxStreak + (((long) nk[1] * (nk[1] - 1)) / 2));
}
}
sc.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 0eed0d9d852aa5b618fca1013b681c59 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.*;
public class TheEnchantedForest {
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) {
Integer[] nk = Arrays.stream(br.readLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new);
Integer[] a = Arrays.stream(br.readLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new);
long sum = 0;
int i;
for (i = 0; i < nk[0] && i < nk[1]; i++) {
sum += a[i];
}
long maxStreak = sum;
for(; i < nk[0]; i++){
sum -= a[i-nk[1]];
sum += a[i];
maxStreak = Math.max(maxStreak, sum);
}
if(nk[1] > nk[0]){
long ans = (long) nk[0] *nk[1] - ((long) (nk[0]) *(nk[0]+1)/2);
System.out.println(ans + sum);
} else {
System.out.println(maxStreak + (((long) nk[1] * (nk[1] - 1)) / 2));
}
}
br.close();
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 726906cba484b540e722f375bace57f4 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
//--------------------------INPUT READER---------------------------------//
static class fs {
public BufferedReader br;
StringTokenizer st = new StringTokenizer("");
public fs() { this(System.in); }
public fs(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
String next() {
while (!st.hasMoreTokens()) {
try { st = new StringTokenizer(br.readLine()); }
catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String ns() { return next(); }
int[] na(long nn) {
int n = (int) nn;
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
long[] nal(long nn) {
int n = (int) nn;
long[] l = new long[n];
for(int i = 0; i < n; i++) l[i] = nl();
return l;
}
}
//-----------------------------------------------------------------------//
//---------------------------PRINTER-------------------------------------//
static class Printer {
static PrintWriter w;
public Printer() {this(System.out);}
public Printer(OutputStream os) {
w = new PrintWriter(os);
}
public void p(int i) {w.println(i);}
public void p(long l) {w.println(l);}
public void p(double d) {w.println(d);}
public void p(String s) { w.println(s);}
public void pr(int i) {w.print(i);}
public void pr(long l) {w.print(l);}
public void pr(double d) {w.print(d);}
public void pr(String s) { w.print(s);}
public void pl() {w.println();}
public void close() {w.close();}
}
//-----------------------------------------------------------------------//
//--------------------------VARIABLES------------------------------------//
static fs sc = new fs();
static OutputStream outputStream = System.out;
static Printer w = new Printer(outputStream);
static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE;
static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE;
static long mod = 1000000007;
//-----------------------------------------------------------------------//
//--------------------------ADMIN_MODE-----------------------------------//
private static void ADMIN_MODE() throws IOException {
String DIR = "ADMIN";
try { DIR = System.getProperty("user.dir"); }
catch (Exception e) { }
if(new File(DIR).getName().equals("LOCAL")) {
w = new Printer(new FileOutputStream("output.txt"));
sc = new fs(new FileInputStream("input.txt"));
}
}
//-----------------------------------------------------------------------//
//----------------------------START--------------------------------------//
public static void main(String[] args)
throws IOException {
ADMIN_MODE();
int t = sc.ni();while(t-->0)
solve();
w.close();
}
static long seqSum (long l, long r) {
l--;
return (r*(r+1))/2 - (l<=0?0:(l*(l+1))/2);
}
static long btw(long[] arr, int l, int r) {
return arr[r]-(l==0?0:arr[l-1]);
}
static void solve() throws IOException {
int n = sc.ni();
long k = sc.nl();
long[] arr = sc.nal(n);
long[] pre = arr.clone();
for(int i = 1; i < n; i++) {
pre[i] = arr[i]+pre[i-1];
}
if(k <= n) {
long best = imi;
for(int i = 0; i < n; i++) {
if(i+k-1 >= n) continue;
long curr = btw(pre, i, (int)(i+k-1));
best = Math.max(best, curr);
}
best += seqSum(1, k-1);
w.p(best);
return;
}
long ans = pre[n-1];
long st = k-n+1;
ans += st-1;
for(int i = 0; i < n-1; i++) {
ans += st++;
}
w.p(ans);
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 33de46572f39552f6bc85456ece6af5a | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.util.Comparator.*;
public class Solution {
public static boolean useInFile = false;
public static boolean useOutFile = false;
public static void main(String args[]) throws IOException {
InOut inout = new InOut();
Resolver resolver = new Resolver(inout);
resolver.solve();
inout.flush();
}
private static class Resolver {
final long LONG_INF = (long) 1e18;
final int INF = (int) (1e9 + 7);
final int MOD = 998244353;
long f[], inv[];
InOut inout;
Resolver(InOut inout) {
this.inout = inout;
}
void initF(int n, int mod) {
f = new long[n + 1];
f[1] = 1;
for (int i = 2; i <= n; i++) {
f[i] = (f[i - 1] * i) % mod;
}
}
void initInv(int n, int mod) {
inv = new long[n + 1];
inv[n] = pow(f[n], mod - 2, mod);
for (int i = inv.length - 2; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
long cmn(int n, int m, int mod) {
return f[n] * inv[m] % mod * inv[n - m] % mod;
}
int d[] = {0, -1, 0, 1, 0};
boolean legal(int r, int c, int n, int m) {
return r >= 0 && r < n && c >= 0 && c < m;
}
long helper(long a, long b) {
if (a > b) {
return helper(b, a);
}
return a * 500005L + b;
}
int[] getBits(int n) {
int b[] = new int[31];
for (int i = 0; i < 31; i++) {
if ((n & (1 << i)) != 0) {
b[i] = 1;
}
}
return b;
}
boolean isConvex(long x1, long y1, long x2, long y2, long x3, long y3) {
return (y2 - y1) * (x2 - x3) < (y2 - y3) * (x2 - x1);
}
long dfs(int n, int k, char[] ch, long mx, boolean equal) {
if (k > n) {
return 1;
}
long cnt = mx;
long rs = 0;
if (equal) {
cnt = ch[k - 1] - 'a';
rs = dfs(n, k + 1, ch, mx, true);
}
if (cnt > 0) {
rs = (rs + cnt * dfs(n, k + 1, ch, mx, false)) % INF;
}
return rs;
}
void solve() throws IOException {
int tt = 1;
boolean hvt = true;
if (hvt) {
tt = nextInt();
// tt = Integer.parseInt(nextLine());
}
// initF(300001, MOD);
// initInv(300001, MOD);
// boolean pri[] = generatePrime(40000);
for (int cs = 1; cs <= tt; cs++) {
long rs = 0;
int n = nextInt();
long k = nextInt();
long a[] = anLong(0, n - 1, 12);
int mod = k < n ? (int) k : n;
long max = 0;
long cur = 0;
for (int i = 0; i < n; i++) {
cur += a[i];
if (i >= mod) {
cur -= a[i - mod];
}
max = Math.max(cur, max);
}
rs += max;
if (n > 1) {
long m = (k - n) % (n - 1);
if (k >= n) {
rs += (m + 1) * m / 2;
rs += (m + 1) * m;
long s = m * 2 + 1;
long e = m * 2 + (n - m - 1);
rs += (s + e) * (e - s + 1) / 2;
m = (k - m - n) / (n - 1);
rs += m * (n - 1) * n;
} else {
m = k % n;
rs += (m - 1) * m / 2;
}
} else {
rs += k - 1;
}
print("" + rs, false);
// format("%d", rs);
if (cs < tt) {
format("\n");
// format(" ");
}
// flush();
}
}
private void updateSegTree(int n, long l, SegmentTree lft) {
long lazy;
lazy = 1;
for (int j = 1; j <= l; j++) {
lazy = (lazy + cmn((int) l, j, INF)) % INF;
lft.modify(1, j, j, lazy);
}
lft.modify(1, (int) (l + 1), n, lazy);
}
String next() throws IOException {
return inout.next();
}
String next(int n) throws IOException {
return inout.next(n);
}
String nextLine() throws IOException {
return inout.nextLine();
}
int nextInt() throws IOException {
return inout.nextInt();
}
long nextLong(int n) throws IOException {
return inout.nextLong(n);
}
int[] anInt(int i, int j) throws IOException {
int a[] = new int[j + 1];
for (int k = i; k <= j; k++) {
a[k] = nextInt();
}
return a;
}
long[] anLong(int i, int j, int len) throws IOException {
long a[] = new long[j + 1];
for (int k = i; k <= j; k++) {
a[k] = nextLong(len);
}
return a;
}
void print(String s, boolean nextLine) {
inout.print(s, nextLine);
}
void format(String format, Object... obj) {
inout.format(format, obj);
}
void flush() {
inout.flush();
}
void swap(int a[], int i, int j) {
a[i] ^= a[j];
a[j] ^= a[i];
a[i] ^= a[j];
}
void swap(long a[], int i, int j) {
a[i] ^= a[j];
a[j] ^= a[i];
a[i] ^= a[j];
}
int getP(int x, int p[]) {
if (p[x] == 0 || p[x] == x) {
return x;
}
return p[x] = getP(p[x], p);
}
void union(int x, int y, int p[]) {
if (x < y) {
p[y] = x;
} else {
p[x] = y;
}
}
boolean topSort() {
int n = adj2.length - 1;
int d[] = new int[n + 1];
for (int i = 1; i <= n; i++) {
for (int j = 0; j < adj2[i].size(); j++) {
d[adj2[i].get(j)[0]]++;
}
}
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (d[i] == 0) {
list.add(i);
}
}
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < adj2[list.get(i)].size(); j++) {
int t = adj2[list.get(i)].get(j)[0];
d[t]--;
if (d[t] == 0) {
list.add(t);
}
}
}
return list.size() == n;
}
class SegmentTreeNode {
long defaultVal = 0;
int l, r;
long val = defaultVal, lazy = defaultVal;
SegmentTreeNode(int l, int r) {
this.l = l;
this.r = r;
}
}
class SegmentTree {
SegmentTreeNode tree[];
long inf = Long.MIN_VALUE;
long c[];
SegmentTree(int n) {
assert n > 0;
tree = new SegmentTreeNode[n * 3 + 1];
}
void setAn(long cn[]) {
c = cn;
}
SegmentTree build(int k, int l, int r) {
if (l > r) {
return this;
}
if (null == tree[k]) {
tree[k] = new SegmentTreeNode(l, r);
}
if (l == r) {
return this;
}
int mid = (l + r) >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
return this;
}
void pushDown(int k) {
if (tree[k].l == tree[k].r) {
return;
}
long lazy = tree[k].lazy;
tree[k << 1].val = ((c[tree[k << 1].l] - c[tree[k << 1].r + 1] + MOD) % MOD * lazy) % MOD;
tree[k << 1].lazy = lazy;
tree[k << 1 | 1].val = ((c[tree[k << 1 | 1].l] - c[tree[k << 1 | 1].r + 1] + MOD) % MOD * lazy) % MOD;
tree[k << 1 | 1].lazy = lazy;
tree[k].lazy = 0;
}
void modify(int k, int l, int r, long val) {
if (tree[k].l >= l && tree[k].r <= r) {
tree[k].val = ((c[tree[k].l] - c[tree[k].r + 1] + MOD) % MOD * val) % MOD;
tree[k].lazy = val;
return;
}
int mid = (tree[k].l + tree[k].r) >> 1;
if (tree[k].lazy != 0) {
pushDown(k);
}
if (mid >= l) {
modify(k << 1, l, r, val);
}
if (mid + 1 <= r) {
modify(k << 1 | 1, l, r, val);
}
tree[k].val = (tree[k << 1].val + tree[k << 1 | 1].val) % MOD;
}
long query(int k, int l, int r) {
if (tree[k].l > r || tree[k].r < l) {
return 0;
}
if (tree[k].lazy != 0) {
pushDown(k);
}
if (tree[k].l >= l && tree[k].r <= r) {
return tree[k].val;
}
long ans = (query(k << 1, l, r) + query(k << 1 | 1, l, r)) % MOD;
if (tree[k].l < tree[k].r) {
tree[k].val = (tree[k << 1].val + tree[k << 1 | 1].val) % MOD;
}
return ans;
}
}
class BinaryIndexedTree {
int n = 1;
long C[];
BinaryIndexedTree(int sz) {
while (n <= sz) {
n <<= 1;
}
n = sz + 1;
C = new long[n];
}
int lowbit(int x) {
return x & -x;
}
void add(int x, long val) {
while (x < n) {
C[x] += val;
x += lowbit(x);
}
}
long getSum(int x) {
long res = 0;
while (x > 0) {
res += C[x];
x -= lowbit(x);
}
return res;
}
int binSearch(long sum) {
if (sum == 0) {
return 0;
}
int n = C.length;
int mx = 1;
while (mx < n) {
mx <<= 1;
}
int res = 0;
for (int i = mx / 2; i >= 1; i >>= 1) {
if (C[res + i] < sum) {
sum -= C[res + i];
res += i;
}
}
return res + 1;
}
}
static class TrieNode {
int cnt = 0;
TrieNode next[];
TrieNode() {
next = new TrieNode[2];
}
private void insert(TrieNode trie, int ch[], int i) {
while (i < ch.length) {
int idx = ch[i];
if (null == trie.next[idx]) {
trie.next[idx] = new TrieNode();
}
trie.cnt++;
trie = trie.next[idx];
i++;
}
}
private static int query(TrieNode trie) {
if (null == trie) {
return 0;
}
int ans[] = new int[2];
for (int i = 0; i < trie.next.length; i++) {
if (null == trie.next[i]) {
continue;
}
ans[i] = trie.next[i].cnt;
}
if (ans[0] == 0 && ans[0] == ans[1]) {
return 0;
}
if (ans[0] == 0) {
return query(trie.next[1]);
}
if (ans[1] == 0) {
return query(trie.next[0]);
}
return Math.min(ans[0] - 1 + query(trie.next[1]), ans[1] - 1 + query(trie.next[0]));
}
}
//Binary tree
class TreeNode {
int val;
int tier = -1;
TreeNode parent;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
//binary tree dfs
void tierTree(TreeNode root) {
if (null == root) {
return;
}
if (null != root.parent) {
root.tier = root.parent.tier + 1;
} else {
root.tier = 0;
}
tierTree(root.left);
tierTree(root.right);
}
//LCA start
TreeNode[][] lca;
TreeNode[] tree;
void lcaDfsTree(TreeNode root) {
if (null == root) {
return;
}
tree[root.val] = root;
TreeNode nxt = root.parent;
int idx = 0;
while (null != nxt) {
lca[root.val][idx] = nxt;
nxt = lca[nxt.val][idx];
idx++;
}
lcaDfsTree(root.left);
lcaDfsTree(root.right);
}
TreeNode lcaTree(TreeNode root, int n, TreeNode x, TreeNode y) throws IOException {
if (null == root) {
return null;
}
if (-1 == root.tier) {
tree = new TreeNode[n + 1];
tierTree(root);
}
if (null == lca) {
lca = new TreeNode[n + 1][31];
lcaDfsTree(root);
}
int z = Math.abs(x.tier - y.tier);
int xx = x.tier > y.tier ? x.val : y.val;
while (z > 0) {
final int zz = z;
int l = (int) BinSearch.bs(0, 31
, k -> zz < (1 << k));
xx = lca[xx][l].val;
z -= 1 << l;
}
int yy = y.val;
if (x.tier <= y.tier) {
yy = x.val;
}
while (xx != yy) {
final int xxx = xx;
final int yyy = yy;
int l = (int) BinSearch.bs(0, 31
, k -> (1 << k) <= tree[xxx].tier && lca[xxx][(int) k] != lca[yyy][(int) k]);
xx = lca[xx][l].val;
yy = lca[yy][l].val;
}
return tree[xx];
}
//LCA end
//graph
List<Integer> adj[];
List<int[]> adj2[];
void initGraph(int n, int m, boolean hasW, boolean directed, int type) throws IOException {
if (type == 1) {
adj = new List[n + 1];
} else {
adj2 = new List[n + 1];
}
for (int i = 1; i <= n; i++) {
if (type == 1) {
adj[i] = new ArrayList<>();
} else {
adj2[i] = new ArrayList<>();
}
}
for (int i = 0; i < m; i++) {
int f = nextInt();
int t = nextInt();
if (type == 1) {
adj[f].add(t);
if (!directed) {
adj[t].add(f);
}
} else {
int w = hasW ? nextInt() : 0;
adj2[f].add(new int[]{t, w});
if (!directed) {
adj2[t].add(new int[]{f, w});
}
}
}
}
void getDiv(Map<Integer, Integer> map, long n) {
int sqrt = (int) Math.sqrt(n);
for (int i = 2; i <= sqrt; i++) {
int cnt = 0;
while (n % i == 0) {
cnt++;
n /= i;
}
if (cnt > 0) {
map.put(i, cnt);
}
}
if (n > 1) {
map.put((int) n, 1);
}
}
boolean[] generatePrime(int n) {
boolean p[] = new boolean[n + 1];
p[2] = true;
for (int i = 3; i <= n; i += 2) {
p[i] = true;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
if (!p[i]) {
continue;
}
for (int j = i * i; j <= n; j += i << 1) {
p[j] = false;
}
}
return p;
}
boolean isPrime(long n) { //determines if n is a prime number
int p[] = {2, 3, 5, 233, 331};
int pn = p.length;
long s = 0, t = n - 1;//n - 1 = 2^s * t
while ((t & 1) == 0) {
t >>= 1;
++s;
}
for (int i = 0; i < pn; ++i) {
if (n == p[i]) {
return true;
}
long pt = pow(p[i], t, n);
for (int j = 0; j < s; ++j) {
long cur = llMod(pt, pt, n);
if (cur == 1 && pt != 1 && pt != n - 1) {
return false;
}
pt = cur;
}
if (pt != 1) {
return false;
}
}
return true;
}
long[] llAdd2(long a[], long b[], long mod) {
long c[] = new long[2];
c[1] = (a[1] + b[1]) % (mod * mod);
c[0] = (a[1] + b[1]) / (mod * mod) + a[0] + b[0];
return c;
}
long[] llMod2(long a, long b, long mod) {
long x1 = a / mod;
long y1 = a % mod;
long x2 = b / mod;
long y2 = b % mod;
long c = (x1 * y2 + x2 * y1) / mod;
c += x1 * x2;
long d = (x1 * y2 + x2 * y1) % mod * mod + y1 * y2;
return new long[]{c, d};
}
long llMod(long a, long b, long mod) {
if (a > mod || b > mod) {
return (a * b - (long) ((double) a / mod * b + 0.5) * mod + mod) % mod;
}
return a * b % mod;
// long r = 0;
// a %= mod;
// b %= mod;
// while (b > 0) {
// if ((b & 1) == 1) {
// r = (r + a) % mod;
// }
// b >>= 1;
// a = (a << 1) % mod;
// }
// return r;
}
long pow(long a, long n) {
long ans = 1;
while (n > 0) {
if ((n & 1) == 1) {
ans = ans * a;
}
a = a * a;
n >>= 1;
}
return ans;
}
long pow(long a, long n, long mod) {
long ans = 1;
while (n > 0) {
if ((n & 1) == 1) {
ans = llMod(ans, a, mod);
}
a = llMod(a, a, mod);
n >>= 1;
}
return ans;
}
private long[][] initC(int n) {
long c[][] = new long[n][n];
for (int i = 0; i < n; i++) {
c[i][0] = 1;
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= i; j++) {
c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
}
}
return c;
}
/**
* ps: n >= m, choose m from n;
*/
// private int cmn(long n, long m) {
// if (m > n) {
// n ^= m;
// m ^= n;
// n ^= m;
// }
// m = Math.min(m, n - m);
//
// long top = 1;
// long bot = 1;
// for (long i = n - m + 1; i <= n; i++) {
// top = (top * i) % MOD;
// }
// for (int i = 1; i <= m; i++) {
// bot = (bot * i) % MOD;
// }
//
// return (int) ((top * pow(bot, MOD - 2, MOD)) % MOD);
// }
long[] exGcd(long a, long b) {
if (b == 0) {
return new long[]{a, 1, 0};
}
long[] ans = exGcd(b, a % b);
long x = ans[2];
long y = ans[1] - a / b * ans[2];
ans[1] = x;
ans[2] = y;
return ans;
}
long gcd(long a, long b) {
if (a < b) {
return gcd(b, a);
}
while (b != 0) {
long tmp = a % b;
a = b;
b = tmp;
}
return a;
}
int[] unique(int a[], Map<Integer, Integer> idx) {
int tmp[] = a.clone();
Arrays.sort(tmp);
int j = 0;
for (int i = 0; i < tmp.length; i++) {
if (i == 0 || tmp[i] > tmp[i - 1]) {
idx.put(tmp[i], j++);
}
}
int rs[] = new int[j];
j = 0;
for (int key : idx.keySet()) {
rs[j++] = key;
}
Arrays.sort(rs);
return rs;
}
boolean isEven(long n) {
return (n & 1) == 0;
}
static class BinSearch {
static long bs(long l, long r, IBinSearch sort) throws IOException {
while (l < r) {
long m = l + (r - l) / 2;
if (sort.binSearchCmp(m)) {
l = m + 1;
} else {
r = m;
}
}
return l;
}
interface IBinSearch {
boolean binSearchCmp(long k) throws IOException;
}
}
}
private static class InOut {
private BufferedReader br;
private StreamTokenizer st;
private PrintWriter pw;
InOut() throws FileNotFoundException {
if (useInFile) {
System.setIn(new FileInputStream("resources/inout/in.text"));
}
if (useOutFile) {
System.setOut(new PrintStream("resources/inout/out.text"));
}
br = new BufferedReader(new InputStreamReader(System.in));
st = new StreamTokenizer(br);
pw = new PrintWriter(new OutputStreamWriter(System.out));
st.ordinaryChar('\'');
st.ordinaryChar('\"');
st.ordinaryChar('/');
}
private boolean hasNext() throws IOException {
return st.nextToken() != StreamTokenizer.TT_EOF;
}
private long[] anLong(int n) throws IOException {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
private String next() throws IOException {
if (st.nextToken() == StreamTokenizer.TT_EOF) {
throw new IOException();
}
return st.sval;
}
private String next(int len) throws IOException {
char ch[] = new char[len];
int cur = 0;
char c;
while ((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t') ;
do {
ch[cur++] = c;
} while (!((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t'));
return String.valueOf(ch, 0, cur);
}
private int nextInt() throws IOException {
if (st.nextToken() == StreamTokenizer.TT_EOF) {
throw new IOException();
}
return (int) st.nval;
}
private long nextLong(int n) throws IOException {
return Long.parseLong(next(n));
}
private double nextDouble() throws IOException {
st.nextToken();
return st.nval;
}
private String[] nextSS(String reg) throws IOException {
return br.readLine().split(reg);
}
private String nextLine() throws IOException {
return br.readLine();
}
private void print(String s, boolean newLine) {
if (null != s) {
pw.print(s);
}
if (newLine) {
pw.println();
}
}
private void format(String format, Object... obj) {
pw.format(format, obj);
}
private void flush() {
pw.flush();
}
}
private static class FFT {
double[] roots;
int maxN;
public FFT(int maxN) {
this.maxN = maxN;
initRoots();
}
public long[] multiply(int[] a, int[] b) {
int minSize = a.length + b.length - 1;
int bits = 1;
while (1 << bits < minSize) bits++;
int N = 1 << bits;
double[] aa = toComplex(a, N);
double[] bb = toComplex(b, N);
fftIterative(aa, false);
fftIterative(bb, false);
double[] c = new double[aa.length];
for (int i = 0; i < N; i++) {
c[2 * i] = aa[2 * i] * bb[2 * i] - aa[2 * i + 1] * bb[2 * i + 1];
c[2 * i + 1] = aa[2 * i] * bb[2 * i + 1] + aa[2 * i + 1] * bb[2 * i];
}
fftIterative(c, true);
long[] ret = new long[minSize];
for (int i = 0; i < ret.length; i++) {
ret[i] = Math.round(c[2 * i]);
}
return ret;
}
static double[] toComplex(int[] arr, int size) {
double[] ret = new double[size * 2];
for (int i = 0; i < arr.length; i++) {
ret[2 * i] = arr[i];
}
return ret;
}
void initRoots() {
roots = new double[2 * (maxN + 1)];
double ang = 2 * Math.PI / maxN;
for (int i = 0; i <= maxN; i++) {
roots[2 * i] = Math.cos(i * ang);
roots[2 * i + 1] = Math.sin(i * ang);
}
}
int bits(int N) {
int ret = 0;
while (1 << ret < N) ret++;
if (1 << ret != N) throw new RuntimeException();
return ret;
}
void fftIterative(double[] array, boolean inv) {
int bits = bits(array.length / 2);
int N = 1 << bits;
for (int from = 0; from < N; from++) {
int to = Integer.reverse(from) >>> (32 - bits);
if (from < to) {
double tmpR = array[2 * from];
double tmpI = array[2 * from + 1];
array[2 * from] = array[2 * to];
array[2 * from + 1] = array[2 * to + 1];
array[2 * to] = tmpR;
array[2 * to + 1] = tmpI;
}
}
for (int n = 2; n <= N; n *= 2) {
int delta = 2 * maxN / n;
for (int from = 0; from < N; from += n) {
int rootIdx = inv ? 2 * maxN : 0;
double tmpR, tmpI;
for (int arrIdx = 2 * from; arrIdx < 2 * from + n; arrIdx += 2) {
tmpR = array[arrIdx + n] * roots[rootIdx] - array[arrIdx + n + 1] * roots[rootIdx + 1];
tmpI = array[arrIdx + n] * roots[rootIdx + 1] + array[arrIdx + n + 1] * roots[rootIdx];
array[arrIdx + n] = array[arrIdx] - tmpR;
array[arrIdx + n + 1] = array[arrIdx + 1] - tmpI;
array[arrIdx] += tmpR;
array[arrIdx + 1] += tmpI;
rootIdx += (inv ? -delta : delta);
}
}
}
if (inv) {
for (int i = 0; i < array.length; i++) {
array[i] /= N;
}
}
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | eda6e14d52cc8de671ced1766c33d8e4 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.*;
/**
*
* @author eslam
*/
public class IceCave {
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
String nextLine() {
String str = "";
try {
str = r.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
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());
}
}
// Beginning of the solution
static Kattio input = new Kattio();
static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
static ArrayList<ArrayList<Long>> powerSet = new ArrayList<>();
static long mod = (long) (Math.pow(10, 9) + 7);
static int dp[][];
public static void main(String[] args) throws IOException {
int t = input.nextInt();
while (t-- > 0) {
int n = input.nextInt();
long k = input.nextInt();
Long a[] = new Long[n];
long ans = 0;
for (int i = 0; i < n; i++) {
a[i] = input.nextLong();
}
if (k < n) {
for (int i = 1; i < n; i++) {
a[i] += a[i - 1];
}
for (int i = 0; i < n; i++) {
if (i - (k - 1) > -1) {
if (i - (k - 1) > 0) {
ans = Math.max(a[i] - a[i - (int) k], ans);
} else {
ans = Math.max(a[i], ans);
}
}
if (i + (k - 1) < n) {
if (i == 0) {
ans = Math.max(a[i + (int) (k - 1)], ans);
} else {
ans = Math.max(a[i + (int) (k - 1)] - a[i - 1], ans);
}
}
}
ans += ((k) * (k - 1)) / 2;
} else {
for (int i = 0; i < n; i++) {
ans += a[i] + (long) i;
}
k -= n;
ans += k * (long) n;
}
log.write(ans + "\n");
}
log.flush();
}
public static int[] bfs(int node, ArrayList<Integer> a[]) {
Queue<Integer> q = new LinkedList<>();
q.add(node);
int distances[] = new int[a.length];
Arrays.fill(distances, -1);
distances[node] = 0;
while (!q.isEmpty()) {
int parent = q.poll();
ArrayList<Integer> nodes = a[parent];
int cost = distances[parent];
for (Integer node1 : nodes) {
if (distances[node1] == -1) {
q.add(node1);
distances[node1] = cost + 1;
}
}
}
return distances;
}
public static int get(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
public static long primeFactors(int n) {
long sum = 1;
while (n % 2 == 0) {
sum *= 2l;
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
sum *= (long) i;
n /= i;
}
if (n < i) {
break;
}
}
if (n > 2) {
sum *= n;
}
return sum;
}
public static ArrayList<Integer> printPrimeFactoriztion(int n) {
ArrayList<Integer> a = new ArrayList<>();
for (int i = 1; i < Math.sqrt(n) + 1; i++) {
if (n % i == 0) {
if (isPrime(i)) {
a.add(i);
n /= i;
i = 0;
} else if (isPrime(n / i)) {
a.add(n / i);
n = i;
i = 0;
}
}
}
return a;
}
// end of solution
public static void genrate(int ind, long[] a, ArrayList<Long> sub) {
if (ind == a.length) {
powerSet.add(sub);
return;
}
ArrayList<Long> have = new ArrayList<>();
ArrayList<Long> less = new ArrayList<>();
for (int i = 0; i < sub.size(); i++) {
have.add(sub.get(i));
less.add(sub.get(i));
}
have.add(a[ind]);
genrate(ind + 1, a, have);
genrate(ind + 1, a, less);
}
public static long factorial(long n) {
if (n <= 1) {
return 1;
}
long t = n - 1;
while (t > 1) {
n *= t;
t--;
}
return n;
}
public static boolean isPalindrome(int n) {
int t = n;
int ans = 0;
while (t > 0) {
ans = ans * 10 + t % 10;
t /= 10;
}
return ans == n;
}
static class tri {
int x, y, z;
public tri(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
static boolean isPrime(long num) {
if (num == 1) {
return false;
}
if (num == 2) {
return true;
}
if (num % 2 == 0) {
return false;
}
if (num == 3) {
return true;
}
for (int i = 3; i <= Math.sqrt(num) + 1; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void prefixSum(int[] a) {
for (int i = 1; i < a.length; i++) {
a[i] = a[i] + a[i - 1];
}
}
public static void suffixSum(long[] a) {
for (int i = a.length - 2; i > -1; i--) {
a[i] = a[i] + a[i + 1];
}
}
static long mod(long a, long b) {
long r = a % b;
return r < 0 ? r + b : r;
}
public static long binaryToDecimal(String w) {
long r = 0;
long l = 0;
for (int i = w.length() - 1; i > -1; i--) {
long x = (w.charAt(i) - '0') * (long) Math.pow(2, l);
r = r + x;
l++;
}
return r;
}
public static String decimalToBinary(long n) {
String w = "";
while (n > 0) {
w = n % 2 + w;
n /= 2;
}
return w;
}
public static boolean isSorted(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
public static void print(int a[]) throws IOException {
for (int i = 0; i < a.length; i++) {
log.write(a[i] + " ");
}
log.write("\n");
}
public static void read(int[] a) {
for (int i = 0; i < a.length; i++) {
a[i] = input.nextInt();
}
}
static class pair {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
public static long LCM(long x, long y) {
return x / GCD(x, y) * y;
}
public static long GCD(long x, long y) {
if (y == 0) {
return x;
}
return GCD(y, x % y);
}
public static void simplifyTheFraction(long a, long b) {
long GCD = GCD(a, b);
System.out.println(a / GCD + " " + b / GCD);
}
/**
*/ // class RedBlackNode
static class RedBlackNode<T extends Comparable<T>> {
/**
* Possible color for this node
*/
public static final int BLACK = 0;
/**
* Possible color for this node
*/
public static final int RED = 1;
// the key of each node
public T key;
/**
* Parent of node
*/
RedBlackNode<T> parent;
/**
* Left child
*/
RedBlackNode<T> left;
/**
* Right child
*/
RedBlackNode<T> right;
// the number of elements to the left of each node
public int numLeft = 0;
// the number of elements to the right of each node
public int numRight = 0;
// the color of a node
public int color;
RedBlackNode() {
color = BLACK;
numLeft = 0;
numRight = 0;
parent = null;
left = null;
right = null;
}
// Constructor which sets key to the argument.
RedBlackNode(T key) {
this();
this.key = key;
}
}// end class RedBlackNode
static class RedBlackTree<T extends Comparable<T>> {
// Root initialized to nil.
private RedBlackNode<T> nil = new RedBlackNode<T>();
private RedBlackNode<T> root = nil;
public RedBlackTree() {
root.left = nil;
root.right = nil;
root.parent = nil;
}
// @param: X, The node which the lefRotate is to be performed on.
// Performs a leftRotate around X.
private void leftRotate(RedBlackNode<T> x) {
// Call leftRotateFixup() which updates the numLeft
// and numRight values.
leftRotateFixup(x);
// Perform the left rotate as described in the algorithm
// in the course text.
RedBlackNode<T> y;
y = x.right;
x.right = y.left;
// Check for existence of y.left and make pointer changes
if (!isNil(y.left)) {
y.left.parent = x;
}
y.parent = x.parent;
// X's parent is nul
if (isNil(x.parent)) {
root = y;
} // X is the left child of it's parent
else if (x.parent.left == x) {
x.parent.left = y;
} // X is the right child of it's parent.
else {
x.parent.right = y;
}
// Finish of the leftRotate
y.left = x;
x.parent = y;
}// end leftRotate(RedBlackNode X)
// @param: X, The node which the leftRotate is to be performed on.
// Updates the numLeft & numRight values affected by leftRotate.
private void leftRotateFixup(RedBlackNode x) {
// Case 1: Only X, X.right and X.right.right always are not nil.
if (isNil(x.left) && isNil(x.right.left)) {
x.numLeft = 0;
x.numRight = 0;
x.right.numLeft = 1;
} // Case 2: X.right.left also exists in addition to Case 1
else if (isNil(x.left) && !isNil(x.right.left)) {
x.numLeft = 0;
x.numRight = 1 + x.right.left.numLeft
+ x.right.left.numRight;
x.right.numLeft = 2 + x.right.left.numLeft
+ x.right.left.numRight;
} // Case 3: X.left also exists in addition to Case 1
else if (!isNil(x.left) && isNil(x.right.left)) {
x.numRight = 0;
x.right.numLeft = 2 + x.left.numLeft + x.left.numRight;
} // Case 4: X.left and X.right.left both exist in addtion to Case 1
else {
x.numRight = 1 + x.right.left.numLeft
+ x.right.left.numRight;
x.right.numLeft = 3 + x.left.numLeft + x.left.numRight
+ x.right.left.numLeft + x.right.left.numRight;
}
}// end leftRotateFixup(RedBlackNode X)
// @param: X, The node which the rightRotate is to be performed on.
// Updates the numLeft and numRight values affected by the Rotate.
private void rightRotate(RedBlackNode<T> y) {
// Call rightRotateFixup to adjust numRight and numLeft values
rightRotateFixup(y);
// Perform the rotate as described in the course text.
RedBlackNode<T> x = y.left;
y.left = x.right;
// Check for existence of X.right
if (!isNil(x.right)) {
x.right.parent = y;
}
x.parent = y.parent;
// y.parent is nil
if (isNil(y.parent)) {
root = x;
} // y is a right child of it's parent.
else if (y.parent.right == y) {
y.parent.right = x;
} // y is a left child of it's parent.
else {
y.parent.left = x;
}
x.right = y;
y.parent = x;
}// end rightRotate(RedBlackNode y)
// @param: y, the node around which the righRotate is to be performed.
// Updates the numLeft and numRight values affected by the rotate
private void rightRotateFixup(RedBlackNode y) {
// Case 1: Only y, y.left and y.left.left exists.
if (isNil(y.right) && isNil(y.left.right)) {
y.numRight = 0;
y.numLeft = 0;
y.left.numRight = 1;
} // Case 2: y.left.right also exists in addition to Case 1
else if (isNil(y.right) && !isNil(y.left.right)) {
y.numRight = 0;
y.numLeft = 1 + y.left.right.numRight
+ y.left.right.numLeft;
y.left.numRight = 2 + y.left.right.numRight
+ y.left.right.numLeft;
} // Case 3: y.right also exists in addition to Case 1
else if (!isNil(y.right) && isNil(y.left.right)) {
y.numLeft = 0;
y.left.numRight = 2 + y.right.numRight + y.right.numLeft;
} // Case 4: y.right & y.left.right exist in addition to Case 1
else {
y.numLeft = 1 + y.left.right.numRight
+ y.left.right.numLeft;
y.left.numRight = 3 + y.right.numRight
+ y.right.numLeft
+ y.left.right.numRight + y.left.right.numLeft;
}
}// end rightRotateFixup(RedBlackNode y)
public void insert(T key) {
insert(new RedBlackNode<T>(key));
}
// @param: z, the node to be inserted into the Tree rooted at root
// Inserts z into the appropriate position in the RedBlackTree while
// updating numLeft and numRight values.
private void insert(RedBlackNode<T> z) {
// Create a reference to root & initialize a node to nil
RedBlackNode<T> y = nil;
RedBlackNode<T> x = root;
// While we haven't reached a the end of the tree keep
// tryint to figure out where z should go
while (!isNil(x)) {
y = x;
// if z.key is < than the current key, go left
if (z.key.compareTo(x.key) < 0) {
// Update X.numLeft as z is < than X
x.numLeft++;
x = x.left;
} // else z.key >= X.key so go right.
else {
// Update X.numGreater as z is => X
x.numRight++;
x = x.right;
}
}
// y will hold z's parent
z.parent = y;
// Depending on the value of y.key, put z as the left or
// right child of y
if (isNil(y)) {
root = z;
} else if (z.key.compareTo(y.key) < 0) {
y.left = z;
} else {
y.right = z;
}
// Initialize z's children to nil and z's color to red
z.left = nil;
z.right = nil;
z.color = RedBlackNode.RED;
// Call insertFixup(z)
insertFixup(z);
}// end insert(RedBlackNode z)
// @param: z, the node which was inserted and may have caused a violation
// of the RedBlackTree properties
// Fixes up the violation of the RedBlackTree properties that may have
// been caused during insert(z)
private void insertFixup(RedBlackNode<T> z) {
RedBlackNode<T> y = nil;
// While there is a violation of the RedBlackTree properties..
while (z.parent.color == RedBlackNode.RED) {
// If z's parent is the the left child of it's parent.
if (z.parent == z.parent.parent.left) {
// Initialize y to z 's cousin
y = z.parent.parent.right;
// Case 1: if y is red...recolor
if (y.color == RedBlackNode.RED) {
z.parent.color = RedBlackNode.BLACK;
y.color = RedBlackNode.BLACK;
z.parent.parent.color = RedBlackNode.RED;
z = z.parent.parent;
} // Case 2: if y is black & z is a right child
else if (z == z.parent.right) {
// leftRotaet around z's parent
z = z.parent;
leftRotate(z);
} // Case 3: else y is black & z is a left child
else {
// recolor and rotate round z's grandpa
z.parent.color = RedBlackNode.BLACK;
z.parent.parent.color = RedBlackNode.RED;
rightRotate(z.parent.parent);
}
} // If z's parent is the right child of it's parent.
else {
// Initialize y to z's cousin
y = z.parent.parent.left;
// Case 1: if y is red...recolor
if (y.color == RedBlackNode.RED) {
z.parent.color = RedBlackNode.BLACK;
y.color = RedBlackNode.BLACK;
z.parent.parent.color = RedBlackNode.RED;
z = z.parent.parent;
} // Case 2: if y is black and z is a left child
else if (z == z.parent.left) {
// rightRotate around z's parent
z = z.parent;
rightRotate(z);
} // Case 3: if y is black and z is a right child
else {
// recolor and rotate around z's grandpa
z.parent.color = RedBlackNode.BLACK;
z.parent.parent.color = RedBlackNode.RED;
leftRotate(z.parent.parent);
}
}
}
// Color root black at all times
root.color = RedBlackNode.BLACK;
}// end insertFixup(RedBlackNode z)
// @param: node, a RedBlackNode
// @param: node, the node with the smallest key rooted at node
public RedBlackNode<T> treeMinimum(RedBlackNode<T> node) {
// while there is a smaller key, keep going left
while (!isNil(node.left)) {
node = node.left;
}
return node;
}// end treeMinimum(RedBlackNode node)
// @param: X, a RedBlackNode whose successor we must find
// @return: return's the node the with the next largest key
// from X.key
public RedBlackNode<T> treeSuccessor(RedBlackNode<T> x) {
// if X.left is not nil, call treeMinimum(X.right) and
// return it's value
if (!isNil(x.left)) {
return treeMinimum(x.right);
}
RedBlackNode<T> y = x.parent;
// while X is it's parent's right child...
while (!isNil(y) && x == y.right) {
// Keep moving up in the tree
x = y;
y = y.parent;
}
// Return successor
return y;
}// end treeMinimum(RedBlackNode X)
// @param: z, the RedBlackNode which is to be removed from the the tree
// Remove's z from the RedBlackTree rooted at root
public void remove(RedBlackNode<T> v) {
RedBlackNode<T> z = search(v.key);
// Declare variables
RedBlackNode<T> x = nil;
RedBlackNode<T> y = nil;
// if either one of z's children is nil, then we must remove z
if (isNil(z.left) || isNil(z.right)) {
y = z;
} // else we must remove the successor of z
else {
y = treeSuccessor(z);
}
// Let X be the left or right child of y (y can only have one child)
if (!isNil(y.left)) {
x = y.left;
} else {
x = y.right;
}
// link X's parent to y's parent
x.parent = y.parent;
// If y's parent is nil, then X is the root
if (isNil(y.parent)) {
root = x;
} // else if y is a left child, set X to be y's left sibling
else if (!isNil(y.parent.left) && y.parent.left == y) {
y.parent.left = x;
} // else if y is a right child, set X to be y's right sibling
else if (!isNil(y.parent.right) && y.parent.right == y) {
y.parent.right = x;
}
// if y != z, trasfer y's satellite data into z.
if (y != z) {
z.key = y.key;
}
// Update the numLeft and numRight numbers which might need
// updating due to the deletion of z.key.
fixNodeData(x, y);
// If y's color is black, it is a violation of the
// RedBlackTree properties so call removeFixup()
if (y.color == RedBlackNode.BLACK) {
removeFixup(x);
}
}// end remove(RedBlackNode z)
// @param: y, the RedBlackNode which was actually deleted from the tree
// @param: key, the value of the key that used to be in y
private void fixNodeData(RedBlackNode<T> x, RedBlackNode<T> y) {
// Initialize two variables which will help us traverse the tree
RedBlackNode<T> current = nil;
RedBlackNode<T> track = nil;
// if X is nil, then we will start updating at y.parent
// Set track to y, y.parent's child
if (isNil(x)) {
current = y.parent;
track = y;
} // if X is not nil, then we start updating at X.parent
// Set track to X, X.parent's child
else {
current = x.parent;
track = x;
}
// while we haven't reached the root
while (!isNil(current)) {
// if the node we deleted has a different key than
// the current node
if (y.key != current.key) {
// if the node we deleted is greater than
// current.key then decrement current.numRight
if (y.key.compareTo(current.key) > 0) {
current.numRight--;
}
// if the node we deleted is less than
// current.key thendecrement current.numLeft
if (y.key.compareTo(current.key) < 0) {
current.numLeft--;
}
} // if the node we deleted has the same key as the
// current node we are checking
else {
// the cases where the current node has any nil
// children and update appropriately
if (isNil(current.left)) {
current.numLeft--;
} else if (isNil(current.right)) {
current.numRight--;
} // the cases where current has two children and
// we must determine whether track is it's left
// or right child and update appropriately
else if (track == current.right) {
current.numRight--;
} else if (track == current.left) {
current.numLeft--;
}
}
// update track and current
track = current;
current = current.parent;
}
}//end fixNodeData()
// @param: X, the child of the deleted node from remove(RedBlackNode v)
// Restores the Red Black properties that may have been violated during
// the removal of a node in remove(RedBlackNode v)
private void removeFixup(RedBlackNode<T> x) {
RedBlackNode<T> w;
// While we haven't fixed the tree completely...
while (x != root && x.color == RedBlackNode.BLACK) {
// if X is it's parent's left child
if (x == x.parent.left) {
// set w = X's sibling
w = x.parent.right;
// Case 1, w's color is red.
if (w.color == RedBlackNode.RED) {
w.color = RedBlackNode.BLACK;
x.parent.color = RedBlackNode.RED;
leftRotate(x.parent);
w = x.parent.right;
}
// Case 2, both of w's children are black
if (w.left.color == RedBlackNode.BLACK
&& w.right.color == RedBlackNode.BLACK) {
w.color = RedBlackNode.RED;
x = x.parent;
} // Case 3 / Case 4
else {
// Case 3, w's right child is black
if (w.right.color == RedBlackNode.BLACK) {
w.left.color = RedBlackNode.BLACK;
w.color = RedBlackNode.RED;
rightRotate(w);
w = x.parent.right;
}
// Case 4, w = black, w.right = red
w.color = x.parent.color;
x.parent.color = RedBlackNode.BLACK;
w.right.color = RedBlackNode.BLACK;
leftRotate(x.parent);
x = root;
}
} // if X is it's parent's right child
else {
// set w to X's sibling
w = x.parent.left;
// Case 1, w's color is red
if (w.color == RedBlackNode.RED) {
w.color = RedBlackNode.BLACK;
x.parent.color = RedBlackNode.RED;
rightRotate(x.parent);
w = x.parent.left;
}
// Case 2, both of w's children are black
if (w.right.color == RedBlackNode.BLACK
&& w.left.color == RedBlackNode.BLACK) {
w.color = RedBlackNode.RED;
x = x.parent;
} // Case 3 / Case 4
else {
// Case 3, w's left child is black
if (w.left.color == RedBlackNode.BLACK) {
w.right.color = RedBlackNode.BLACK;
w.color = RedBlackNode.RED;
leftRotate(w);
w = x.parent.left;
}
// Case 4, w = black, and w.left = red
w.color = x.parent.color;
x.parent.color = RedBlackNode.BLACK;
w.left.color = RedBlackNode.BLACK;
rightRotate(x.parent);
x = root;
}
}
}// end while
// set X to black to ensure there is no violation of
// RedBlack tree Properties
x.color = RedBlackNode.BLACK;
}// end removeFixup(RedBlackNode X)
// @param: key, the key whose node we want to search for
// @return: returns a node with the key, key, if not found, returns null
// Searches for a node with key k and returns the first such node, if no
// such node is found returns null
public RedBlackNode<T> search(T key) {
// Initialize a pointer to the root to traverse the tree
RedBlackNode<T> current = root;
// While we haven't reached the end of the tree
while (!isNil(current)) {
// If we have found a node with a key equal to key
if (current.key.equals(key)) // return that node and exit search(int)
{
return current;
} // go left or right based on value of current and key
else if (current.key.compareTo(key) < 0) {
current = current.right;
} // go left or right based on value of current and key
else {
current = current.left;
}
}
// we have not found a node whose key is "key"
return null;
}// end search(int key)
// @param: key, any Comparable object
// @return: return's the number of elements greater than key
public int numGreater(T key) {
// Call findNumGreater(root, key) which will return the number
// of nodes whose key is greater than key
return findNumGreater(root, key);
}// end numGreater(int key)
// @param: key, any Comparable object
// @return: return's teh number of elements smaller than key
public int numSmaller(T key) {
// Call findNumSmaller(root,key) which will return
// the number of nodes whose key is greater than key
return findNumSmaller(root, key);
}// end numSmaller(int key)
// @param: node, the root of the tree, the key who we must
// compare other node key's to.
// @return: the number of nodes greater than key.
public int findNumGreater(RedBlackNode<T> node, T key) {
// Base Case: if node is nil, return 0
if (isNil(node)) {
return 0;
} // If key is less than node.key, all elements right of node are
// greater than key, add this to our total and look to the left
else if (key.compareTo(node.key) < 0) {
return 1 + node.numRight + findNumGreater(node.left, key);
} // If key is greater than node.key, then look to the right as
// all elements to the left of node are smaller than key
else {
return findNumGreater(node.right, key);
}
}// end findNumGreater(RedBlackNode, int key)
/**
* Returns sorted list of keys greater than key. Size of list will not
* exceed maxReturned
*
* @param key Key to search for
* @param maxReturned Maximum number of results to return
* @return List of keys greater than key. List may not exceed
* maxReturned
*/
public List<T> getGreaterThan(T key, Integer maxReturned) {
List<T> list = new ArrayList<T>();
getGreaterThan(root, key, list);
return list.subList(0, Math.min(maxReturned, list.size()));
}
private void getGreaterThan(RedBlackNode<T> node, T key,
List<T> list) {
if (isNil(node)) {
return;
} else if (node.key.compareTo(key) > 0) {
getGreaterThan(node.left, key, list);
list.add(node.key);
getGreaterThan(node.right, key, list);
} else {
getGreaterThan(node.right, key, list);
}
}
// @param: node, the root of the tree, the key who we must compare other
// node key's to.
// @return: the number of nodes smaller than key.
public int findNumSmaller(RedBlackNode<T> node, T key) {
// Base Case: if node is nil, return 0
if (isNil(node)) {
return 0;
} // If key is less than node.key, look to the left as all
// elements on the right of node are greater than key
else if (key.compareTo(node.key) <= 0) {
return findNumSmaller(node.left, key);
} // If key is larger than node.key, all elements to the left of
// node are smaller than key, add this to our total and look
// to the right.
else {
return 1 + node.numLeft + findNumSmaller(node.right, key);
}
}// end findNumSmaller(RedBlackNode nod, int key)
// @param: node, the RedBlackNode we must check to see whether it's nil
// @return: return's true of node is nil and false otherwise
private boolean isNil(RedBlackNode node) {
// return appropriate value
return node == nil;
}// end isNil(RedBlackNode node)
// @return: return's the size of the tree
// Return's the # of nodes including the root which the RedBlackTree
// rooted at root has.
public int size() {
// Return the number of nodes to the root's left + the number of
// nodes on the root's right + the root itself.
return root.numLeft + root.numRight + 1;
}// end size()
}// end class RedBlackTree
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | a4a283bff295f9f3ec22d50c7a124e6f | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | /*
I am dead inside
Do you like NCT, sKz, BTS?
5 4 3 2 1 Moonwalk
Imma knock it down like domino
Is this what you want? Is this what you want?
Let's ttalkbocky about that :()
*/
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
public class x1687A
{
public static void main(String omkar[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int T = Integer.parseInt(st.nextToken());
StringBuilder sb = new StringBuilder();
while(T-->0)
{
st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int[] arr = readArr(N, infile, st);
if(N == 1)
{
long res = arr[0]+K-1;
sb.append(res+"\n");
continue;
}
if(K <= N)
{
long res = 1L*K*(K-1)/2;
long sum = 0L;
for(int i=0; i < K; i++)
sum += arr[i];
long best = sum;
for(int i=K; i < N; i++)
{
sum -= arr[i-K];
sum += arr[i];
best = max(best, sum);
}
res += best;
sb.append(res+"\n");
}
else
{
long res = 1L*N*(K-N);
K = N;
res += 1L*K*(K-1)/2;
long sum = 0L;
for(int i=0; i < K; i++)
sum += arr[i];
long best = sum;
for(int i=K; i < N; i++)
{
sum -= arr[i-K];
sum += arr[i];
best = max(best, sum);
}
res += best;
sb.append(res+"\n");
}
}
System.out.print(sb);
}
public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
}
/*
5+7+3+5+7 = 27
2+4 = 6
5 6 1 2 3
1 7 2 3 4
2 1 3 4 5
3 2 1 5 6
4 3 2 1 7
5 4 3 2 1
6 5 4 1 2
7 6 1 2 3
*/ | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 11011fcdd7a5785a36cfa8e702ed978e | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class A implements Runnable {
public void run() {
long startTime = System.nanoTime();
int tc = nextInt();
while (tc-- > 0) {
int n = nextInt();
int k = nextInt();
long max = 0;
if (n < k) {
if (n == 1) {
max = nextInt() + k - 1;
} else {
for (int i = 0; i < n; i++) {
max += nextInt();
}
int r = k % (n - 1);
max += 3 * f(r);
max += f(n + r - 1) - f(2 * r - 1);
k -= n + r - 1;
max += 2 * (k / (n - 1)) * f(n);
}
} else {
int[] a = new int[n];
long s = 0;
for (int i = 0; i < n; i++) {
int x = a[i] = nextInt();
s += x;
if (i >= k) {
s -= a[i - k];
}
max = Math.max(max, s + k * (k - 1L) / 2);
}
}
println(max);
}
if (fileIOMode) {
System.out.println((System.nanoTime() - startTime) / 1e9);
}
out.close();
}
private long f(int n) {
return n * (n - 1L) / 2;
}
//-----------------------------------------------------------------------------------
private static boolean fileIOMode;
private static BufferedReader in;
private static PrintWriter out;
private static StringTokenizer tokenizer;
public static void main(String[] args) throws Exception {
fileIOMode = args.length > 0 && args[0].equals("!");
if (fileIOMode) {
in = new BufferedReader(new FileReader("a.in"));
out = new PrintWriter("a.out");
} else {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
tokenizer = new StringTokenizer("");
new Thread(new A()).start();
}
private static String nextLine() {
try {
return in.readLine();
} catch (IOException e) {
return null;
}
}
private static String nextToken() {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(nextLine());
}
return tokenizer.nextToken();
}
private static int nextInt() {
return Integer.parseInt(nextToken());
}
private static long nextLong() {
return Long.parseLong(nextToken());
}
private static double nextDouble() {
return Double.parseDouble(nextToken());
}
private static BigInteger nextBigInteger() {
return new BigInteger(nextToken());
}
private static void print(Object o) {
if (fileIOMode) {
System.out.print(o);
}
out.print(o);
}
private static void println(Object o) {
if (fileIOMode) {
System.out.println(o);
}
out.println(o);
}
private static void printf(String s, Object... o) {
if (fileIOMode) {
System.out.printf(s, o);
}
out.printf(s, o);
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 20ceebbbc4aa98e4d19dba39e3051897 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Locale;
import java.util.StringTokenizer;
public class Solution implements Runnable {
private PrintStream out;
private BufferedReader in;
private StringTokenizer st;
public void solve() throws IOException {
long time0 = System.currentTimeMillis();
int t = nextInt();
for (int test = 1; test <= t; test++) {
int n = nextInt();
int k = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
long answer = solve(n, k, a);
out.println(answer);
}
System.err.println("time: " + (System.currentTimeMillis() - time0));
}
private long solve(int n, int k, int[] a) {
if (n <= k) {
return solveAll(n, k, a);
}
long answer = 1L * k * (k - 1) / 2;
long[] sum = new long[n + 1];
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + a[i - 1];
}
long max = 0;
for (int i = 0; i + k <= n; i++) {
max = Math.max(max, sum[i + k] - sum[i]);
}
answer = answer + max;
return answer;
}
private long solveAll(int n, int k, int[] a) {
int wait = k - n;
long answer = 0;
for (int i = 0; i < n; i++) {
answer = answer + wait + a[i] + i;
}
return answer;
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
@Override
public void run() {
try {
solve();
out.close();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
public Solution(String name) throws IOException {
Locale.setDefault(Locale.US);
if (name == null) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintStream(new BufferedOutputStream(System.out));
} else {
in = new BufferedReader(new InputStreamReader(new FileInputStream(name + ".in")));
out = new PrintStream(new BufferedOutputStream(new FileOutputStream(name + ".out")));
}
st = new StringTokenizer("");
}
public static void main(String[] args) throws IOException {
new Thread(new Solution(null)).start();
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | ebbd48bed69683dd728467dd7cb8dd33 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.lang.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
public static void deal(int n,int k,int[] a) {
int t = k;
if(k>=n) {
t = n;
}
long[] pre = new long[n+1];
for(int i=0;i<n;i++) {
a[i] += k-1;
pre[i+1] = pre[i] + a[i];
}
long max = 0;
for(int i=t-1;i<n;i++) {
max = Math.max(max,pre[i+1]-pre[i-t+1]);
}
max = max - (long)t*(t-1)/2;
out.println(max);
}
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
for(int i=0;i<t;i++) {
int n =sc.nextInt();
int k =sc.nextInt();
int[] a = new int[n];
for(int j=0;j<n;j++) a[j] = sc.nextInt();
deal(n,k,a);
}
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();
}
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 | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 131609064016b01d8585b03db4fee8df | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
Task solver = new Task();
solver.solve(in.nextInt(), in, out);
out.close();
}
static class Task {
public void solve(int testNumber, InputReader in, PrintWriter out) {
for (int i = 0; i < testNumber; i++) {
int n = in.nextInt();
int k = in.nextInt();
Long[] a = new Long[n];
for (int j = 0; j < a.length; j++) {
a[j] = in.nextLong();
}
out.println(new Solution().solve(a, k));
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
class Solution {
public long solve(Long[] a, int k) {
int n = a.length;
if (k >= n) {
long sum = 0;
for (Long v : a) {
sum += v;
}
sum += (long) n * (k - 1);
return sum - (long) n * (n - 1) / 2;
}
long cur = 0;
for (int i = 0; i < k; i++) {
cur += a[i];
}
long max = cur;
for (int i = k; i < n; i++) {
cur -= a[i - k];
cur += a[i];
max = Math.max(max, cur);
}
return max + (long) k * (k - 1) / 2;
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 26667ffe62448ebfd9467d2c02f93cd1 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.BufferedReader;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Supplier;
public class Solution {
private void solve() throws IOException {
int n = nextInt();
int k = nextInt();
long[] a = nextLongArray(n);
if (k < n) {
long[] b = new long[n];
b[0] = a[0];
for (int i = 1; i < n; i++) b[i] = b[i - 1] + a[i];
long res = 0;
for (int i = 0; i <= n - k; i++) {
res = Math.max(res, b[i + k - 1] - (i > 0 ? b[i - 1] : 0));
}
res += (k - 1) * (long) k / 2;
out.println(res);
} else {
long res = 0;
for (long aa : a) res += aa;
res += (n - 1) * (long) n / 2;
res += n * (long)(k-n);
out.println(res);
}
}
private static final boolean runNTestsInProd = true;
private static final boolean printCaseNumber = false;
private static final boolean assertInProd = false;
private static final boolean logToFile = false;
private static final boolean readFromConsoleInDebug = false;
private static final boolean writeToConsoleInDebug = true;
private static final boolean testTimer = false;
private static Boolean isDebug = null;
private BufferedReader in;
private StringTokenizer line;
private PrintWriter out;
public static void main(String[] args) throws Exception {
isDebug = Arrays.asList(args).contains("DEBUG_MODE");
if (isDebug) {
log = logToFile ? new PrintWriter("logs/j_solution_" + System.currentTimeMillis() + ".log") : new PrintWriter(System.out);
clock = Clock.systemDefaultZone();
}
new Solution().run();
}
private void run() throws Exception {
in = new BufferedReader(new InputStreamReader(!isDebug || readFromConsoleInDebug ? System.in : new FileInputStream("input.txt")));
out = !isDebug || writeToConsoleInDebug ? new PrintWriter(System.out) : new PrintWriter("output.txt");
try (Timer totalTimer = new Timer("total")) {
int t = runNTestsInProd || isDebug ? nextInt() : 1;
for (int i = 0; i < t; i++) {
if (printCaseNumber) {
out.print("Case #" + (i + 1) + ": ");
}
if (testTimer) {
try (Timer testTimer = new Timer("test #" + (i + 1))) {
solve();
}
} else {
solve();
}
if (isDebug) {
out.flush();
}
}
}
in.close();
out.flush();
out.close();
}
private void println(Object... objects) {
boolean isFirst = true;
for (Object o : objects) {
if (!isFirst) {
out.print(" ");
} else {
isFirst = false;
}
out.print(o.toString());
}
out.println();
}
private int[] nextIntArray(int n) throws IOException {
return nextIntArray(n, 0);
}
private int[] nextIntArray(int n, int delta) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt() + delta;
}
return res;
}
private long[] nextLongArray(int n) throws IOException {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private char[] nextTokenChars() throws IOException {
return nextToken().toCharArray();
}
private String nextToken() throws IOException {
while (line == null || !line.hasMoreTokens()) {
line = new StringTokenizer(in.readLine());
}
return line.nextToken();
}
private static void assertPredicate(boolean p) {
if ((isDebug || assertInProd) && !p) {
throw new RuntimeException();
}
}
private static void assertPredicate(boolean p, String message) {
if ((isDebug || assertInProd) && !p) {
throw new RuntimeException(message);
}
}
private static <T> void assertNotEqual(T unexpected, T actual) {
if ((isDebug || assertInProd) && Objects.equals(actual, unexpected)) {
throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual);
}
}
private static <T> void assertEqual(T expected, T actual) {
if ((isDebug || assertInProd) && !Objects.equals(actual, expected)) {
throw new RuntimeException("assertEqual: " + expected + " != " + actual);
}
}
private static PrintWriter log = null;
private static Clock clock = null;
private static void log(Object... objects) {
log(true, objects);
}
private static void logNoDelimiter(Object... objects) {
log(false, objects);
}
private static void log(boolean printDelimiter, Object[] objects) {
if (isDebug) {
StringBuilder sb = new StringBuilder();
sb.append(LocalDateTime.now(clock)).append(" - ");
boolean isFirst = true;
for (Object o : objects) {
if (!isFirst && printDelimiter) {
sb.append(" ");
} else {
isFirst = false;
}
sb.append(o.toString());
}
log.println(sb);
log.flush();
}
}
private static class Timer implements Closeable {
private final String label;
private final long startTime = isDebug ? System.nanoTime() : 0;
public Timer(String label) {
this.label = label;
}
@Override
public void close() throws IOException {
if (isDebug) {
long executionTime = System.nanoTime() - startTime;
String fraction = Long.toString(executionTime / 1000 % 1_000_000);
logNoDelimiter("Timer[", label, "]: ", executionTime / 1_000_000_000, '.', "00000".substring(0, 6 - fraction.length()), fraction, 's');
}
}
}
private static <T> T timer(String label, Supplier<T> f) throws Exception {
if (isDebug) {
try (Timer timer = new Timer(label)) {
return f.get();
}
} else {
return f.get();
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 8 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 8911c59927771441d52752e8e55d71a6 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes |
import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
static final int INF = 0x3f3f3f3f;
static final long LNF = 0x3f3f3f3f3f3f3f3fL;
public static void main(String[] args) throws IOException {
initReader();
int t=nextInt();
while (t-->0){
int n=nextInt(); long k=nextInt();
long []a=new long[n];
long b[]=new long[n];
long sum=0;
for(int i=0;i<n;i++){
a[i]=nextInt();
sum+=a[i];
b[i]=sum;
}
if(k>n){
long h=k-n;
long n1=n;
long hh=h*n1+(n1-1)*n1/2;
sum+=hh;
pw.println(sum);
}
else {
long kk=b[(int)k-1];
long max=kk;
for(long i=k;i<n;i++){
kk+=a[(int)i]-a[(int)(i-k)];
// pw.println(a[i]+" "+a[i-k]);
// pw.println(k+" "+(k-1));
max=Math.max(kk,max);
}
max+=k*(k-1)/2;
pw.println(max);
}
/**long n=nextLong();
long k=nextLong();
long[] arr=nextLongArray((int) n);
long[] prefix=new long[(int) n];
for (int i = 0; i < n; i++) {
prefix[i]=arr[i];
if(i-1>=0){
prefix[i]+=prefix[i-1];
}
}
long ans=0;
if(n==1){
ans=arr[0];
ans+=(k-1);
}
else if(k<=n-1){
ans=((k-1)*(k))/2L;
long subarray=0;
for (int i = 0; i <=n-k ; i++) {
if(i==0) subarray=Math.max(subarray,prefix[(int) (i+k-1)]);
else subarray=Math.max(subarray,prefix[(int) (i+k-1)]-prefix[i-1]);
}
ans+=subarray;
}
else if(k>=n){
long res=k-n;
for (int i = 0; i < n; i++) {
ans+=(res+arr[i]);
res++;
}
}
pw.println(ans);*/
}
pw.close();
}
public static long[] nextLongArray(int length)throws IOException {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = nextLong();
return array;
}
public static long[] nextLongArray(int length, java.util.function.LongUnaryOperator map)throws IOException {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsLong(nextLong());
return array;
}
public static int[] nextIntArray(int length)throws IOException {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = nextInt();
return array;
}
/***************************************************************************************/
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter pw;
public static void initReader() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = new StringTokenizer("");
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// 从文件读写
// reader = new BufferedReader(new FileReader("test.in"));
// tokenizer = new StringTokenizer("");
// pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out")));
}
public static boolean hasNext() {
try {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
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 char nextChar() throws IOException {
return next().charAt(0);
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 784bc8d63d29bf13685976e5a9806414 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes |
import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
static final int INF = 0x3f3f3f3f;
static final long LNF = 0x3f3f3f3f3f3f3f3fL;
public static void main(String[] args) throws IOException {
initReader();
int t=nextInt();
while (t-->0){
int n=nextInt();long k=nextInt();
long []a=new long[n];
long b[]=new long[n];
long sum=0;
for(int i=0;i<n;i++){
a[i]=nextInt();
sum+=a[i];
b[i]=sum;
}
if(k>n){
long h=k-n;
for(int i=0;i<n;i++){
sum+=h;h++;
}
pw.println(sum);
}
else {
long kk=b[(int)k-1];
long max=kk;
for(int i=1;i<=n-k;i++){
max=Math.max(max,b[(int)k+i-1]-b[i-1]);
}
max+=(k*(k-1))/2L;
pw.println(max);
}
/**long n=nextLong();
long k=nextLong();
long[] arr=nextLongArray((int) n);
long[] prefix=new long[(int) n];
for (int i = 0; i < n; i++) {
prefix[i]=arr[i];
if(i-1>=0){
prefix[i]+=prefix[i-1];
}
}
long ans=0;
if(n==1){
ans=arr[0];
ans+=(k-1);
}
else if(k<=n-1){
ans=((k-1)*(k))/2L;
long subarray=0;
for (int i = 0; i <=n-k ; i++) {
if(i==0) subarray=Math.max(subarray,prefix[(int) (i+k-1)]);
else subarray=Math.max(subarray,prefix[(int) (i+k-1)]-prefix[i-1]);
}
ans+=subarray;
}
else if(k>=n){
long res=k-n;
for (int i = 0; i < n; i++) {
ans+=(res+arr[i]);
res++;
}
}
pw.println(ans);*/
}
pw.close();
}
public static long[] nextLongArray(int length)throws IOException {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = nextLong();
return array;
}
public static long[] nextLongArray(int length, java.util.function.LongUnaryOperator map)throws IOException {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsLong(nextLong());
return array;
}
public static int[] nextIntArray(int length)throws IOException {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = nextInt();
return array;
}
/***************************************************************************************/
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter pw;
public static void initReader() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = new StringTokenizer("");
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// 从文件读写
// reader = new BufferedReader(new FileReader("test.in"));
// tokenizer = new StringTokenizer("");
// pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out")));
}
public static boolean hasNext() {
try {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
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 char nextChar() throws IOException {
return next().charAt(0);
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 2a8e0ffe193af27bc53f9aa86194cc09 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes |
import java.util.*;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt(), k = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++) arr[i] = sc.nextInt();
long[] pre = new long[n];
pre[0] = arr[0];
for(int i=1;i<n;i++) pre[i] = pre[i-1] + arr[i];
if(k < n) {
long res = 0;
for(int i=k-1;i<n;i++) {
long temp = pre[i] - (i-k >= 0? pre[i-k] : 0);
res = Math.max(temp, res);
}
res += ((1L * k * (k-1)) / 2);
System.out.println(res);
continue;
}
long res = pre[n-1] + (1L*n-1)*(k-n+1) + k - n + ((1L*n-1)*(n-2))/2;
System.out.println(res);
}
// ####################
}
// ####################
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 5c28a8749de858a3c5c831f0ba3ac376 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes |
import java.util.*;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt(), k = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++) arr[i] = sc.nextInt();
long[] pre = new long[n];
pre[0] = arr[0];
for(int i=1;i<n;i++) pre[i] = pre[i-1] + arr[i];
if(k < n) {
long res = 0;
for(int i=k-1;i<n;i++) {
long temp = pre[i] - (i-k >= 0? pre[i-k] : 0);
res = Math.max(temp, res);
}
res += ((1L * k * (k-1)) / 2);
System.out.println(res);
continue;
}
long res = pre[n-1] + 1L * n*k - ((1L * n * (n+1)) / 2);
System.out.println(res);
}
// ####################
}
// ####################
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 19e9164c0608cbb5bb467cc671c41776 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import javax.print.attribute.IntegerSyntax;
import java.util.*;
import java.io.*;
public class Solution
{
private static class FastIO {
private static class FastReader
{
BufferedReader br;
StringTokenizer st;
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;
}
}
private static PrintWriter out = new PrintWriter(System.out);
private static FastReader in = new FastReader();
public void print(String s) {out.print(s);}
public void println(String s) {out.println(s);}
public void println() {
println("");
}
public void print(int i) {out.print(i);}
public void print(long i) {out.print(i);}
public void print(char i) {out.print(i);}
public void print(double i) {out.print(i);}
public void println(int i) {out.println(i);}
public void println(long i) {out.println(i);}
public void println(char i) {out.println(i);}
public void println(double i) {out.println(i);}
public void printIntArrayWithoutSpaces(int[] a) {
for(int i : a) {
out.print(i);
}
out.println();
}
public void printIntArrayWithSpaces(int[] a) {
for(int i : a) {
out.print(i + " ");
}
out.println();
}
public void printIntArrayNewLine(int[] a) {
for(int i : a) {
out.println(i);
}
}
public int[] getIntArray(int n) {
int[] res = new int[n];
for(int i = 0; i < n; i++) {
res[i] = in.nextInt();
}
return res;
}
public List<Integer> getIntList(int n) {
List<Integer> list = new ArrayList<>();
for(int i = 0; i < n; i++) {
list.add(in.nextInt());
}
return list;
}
public static void printKickstartCase(int i) {
out.print("Case #" + i + ": ");
}
public String next() {return in.next();}
int nextInt() { return in.nextInt(); }
char nextChar() {return in.next().charAt(0);}
long nextLong() { return in.nextLong(); }
double nextDouble() { return in.nextDouble(); }
String nextLine() {return in.nextLine();}
public void close() {
out.flush();
out.close();
}
}
private static final FastIO io = new FastIO();
private static class MathUtil {
public static final int MOD = 1000000007;
public static int gcd(int a, int b) {
if(b == 0) {
return a;
}
return gcd(b, a % b);
}
public static int gcd(int[] a) {
if(a.length == 0) {
return 0;
}
int res = a[0];
for(int i = 1; i < a.length; i++) {
res = gcd(res, a[i]);
}
return res;
}
public static int gcd(List<Integer> a) {
if(a.size() == 0) {
return 0;
}
int res = a.get(0);
for(int i = 1; i < a.size(); i++) {
res = gcd(res, a.get(i));
}
return res;
}
public static int modular_mult(int a, int b, int M) {
long res = (long)a * b;
return (int)(res % M);
}
public static int modular_mult(int a, int b) {
return modular_mult(a, b, MOD);
}
public static int modular_add(int a, int b, int M) {
long res = (long)a + b;
return (int)(res % M);
}
public static int modular_add(int a, int b) {
return modular_add(a, b, MOD);
}
public static int modular_sub(int a, int b, int M) {
long res = ((long)a - b) + M;
return (int)(res % M);
}
public static int modular_sub(int a, int b) {
return modular_sub(a, b, MOD);
}
//public static int modular_div(int a, int b, int M) {}
//public static int modular_div(int a, int b) {return modular_div(a, b, MOD);}
public static int pow(int a, int b, int M) {
int res = 1;
while (b > 0) {
if ((b & 1) == 1) {
res = modular_mult(res, a, M);
}
a = modular_mult(a, a, M);
b = b >> 1;
}
return res;
}
public static int pow(int a, int b) {
return pow(a, b, MOD);
}
/*public static int fact(int i, int M) {
}
public static int fact(int i) {
}
public static void preComputeFact(int i) {
}
public static int mod_mult_inverse(int den, int mod) {
}
public static void C(int n, int r) {
}*/
}
private static class ArrayUtil {
@FunctionalInterface
private static interface NumberPairComparator {
boolean test(int a, int b);
}
public static int[] nextGreaterOrSmallerRight(int[] a, NumberPairComparator npc) {
int n = a.length;
int[] res = new int[n];
Stack<Integer> stack = new Stack<>();
for(int i = 0; i < n; i++) {
int cur = a[i];
while(!stack.isEmpty() && npc.test(a[stack.peek()], cur)) {
res[stack.pop()] = i;
}
stack.push(i);
}
while(!stack.isEmpty()) {
res[stack.pop()] = n;
}
return res;
}
public static int[] nextGreaterOrSmallerLeft(int[] a, NumberPairComparator npc) {
int n = a.length;
int[] res = new int[n];
Stack<Integer> stack = new Stack<>();
for(int i = n - 1; i >= 0; i--) {
int cur = a[i];
while(!stack.isEmpty() && npc.test(a[stack.peek()], cur)) {
res[stack.pop()] = i;
}
stack.push(i);
}
while(!stack.isEmpty()) {
res[stack.pop()] = n;
}
return res;
}
public static Map<Integer, Integer> getFreqMap(int[] a) {
Map<Integer, Integer> map = new HashMap<>();
for(int i : a) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
return map;
}
public static long arraySum(int[] a) {
long sum = 0;
for(int i : a) {
sum += i;
}
return sum;
}
private static int maxIndex(int[] a) {
int max = Integer.MIN_VALUE;
int max_index = -1;
for(int i = 0; i < a.length; i++) {
if(a[i] > max) {
max = a[i];
max_index = i;
}
}
return max_index;
}
public static long[] getPrefixSumArray(int[] a) {
int n = a.length;
long[] res = new long[n];
res[0] = a[0];
for(int i = 1; i < n; i++) {
res[i] = a[i] + res[i - 1];
}
return res;
}
public static long rangeSum(long[] prefix, int l, int r) {
if(l == 0) {
return prefix[r];
}
return prefix[r] - prefix[l - 1];
}
public static long maximumSubarrayOfSizeK(int[] a, int k) {
long[] prefix = getPrefixSumArray(a);
int n = a.length;
long res = rangeSum(prefix, 0, k -1);
for(int i = 0; i < n; i++) {
int j = i + k - 1;
if(j >= n) {
break;
}
res = Math.max(res, rangeSum(prefix, i, j));
}
return res;
}
}
private static final int M = 1000000007;
private static final String yes = "YES";
private static final String no = "NO";
private static int MAX = M / 100;
private static final int MIN = -MAX;
private static final int UNVISITED = -1;
private static class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "x: " + x + " y: " + y;
}
}
private static List<List<Integer>> getAdj(int n, int e, boolean directed, boolean shift) {
List<List<Integer>> res = new ArrayList<>();
for(int i = 0; i < n; i++) {
res.add(new ArrayList<>());
}
for(int i = 0; i < e; i++) {
int u = io.nextInt();
int v = io.nextInt();
if(shift) {
u --;
v --;
}
res.get(u).add(v);
if(!directed) {
res.get(v).add(u);
}
}
return res;
}
private static class TreeUtil {
private static List<List<Integer>> getTreeInputWithOffset(int n) {
List<List<Integer>> res = new ArrayList<>();
for(int i = 0; i < n; i++) {
res.add(new ArrayList<>());
}
for(int i = 0; i < n - 1; i++) {
int u = io.nextInt() - 1;
int v = io.nextInt() - 1;
res.get(u).add(v);
res.get(v).add(u);
}
return res;
}
private static int subtreeSizeDfs(int root, int parent, List<List<Integer>> tree, List<Integer> res) {
int sum = 0;
for(int child : tree.get(root)) {
if(child == parent) {
continue;
}
sum += subtreeSizeDfs(child, root, tree, res);
}
res.set(root, 1 + sum);
return 1 + sum;
}
private static List<Integer> getSubtreeSizes(List<List<Integer>> tree, int root) {
List<Integer> res = new ArrayList<>();
for(int i = 0; i < tree.size(); i++) {
res.add(0);
}
subtreeSizeDfs(root, -1, tree, res);
return res;
}
}
private static final long e3 = 1_000;
private static final long e6 = 1_000_000;
private static final long e9 = 1_000_000_000;
private static final long e12 = e9 * e3;
private static final long e15 = e9 * e6;
private static final long e18 = e9 * e9;
String mod = "%";
private static final int[] dr = new int[]{-1, 0, 1, -1, 1, -1, 0, 1};
private static final int[] dc = new int[]{-1, -1, -1, 0, 0, 1, 1, 1};
public static void main(String[] args) {
int testcases = io.nextInt();
// int testcases = 1;
for(int zqt = 0; zqt < testcases; zqt ++) {
int n = io.nextInt();
int k = io.nextInt();
int[] a = io.getIntArray(n);
long res = 0;
if(n == 1) {
io.println(a[0] + k - 1);
continue;
}
if(k <= n) {
res = ArrayUtil.maximumSubarrayOfSizeK(a, k) + ((long) k * (k - 1) / 2);
io.println(res);
}
else {
int numWaitRounds = k - n;
res = numWaitRounds + ((long) n * (n - 1) / 2) + ArrayUtil.maximumSubarrayOfSizeK(a, n) + ((long) (n - 1) * numWaitRounds);
io.println(res);
}
}
io.close();
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 84b4af8d712a2c146fb9d71a43123123 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
long n = sc.nextLong();
long k = sc.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = sc.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | c5554016a977f830d051f0d823478b12 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
long n = sc.nextLong();
long k = sc.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = sc.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 622839619de089ade1c2fa3635c0f26c | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
long n = sc.nextLong();
long k = sc.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = sc.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 0a8d9d82a4ad4c6ceb0bdfca9d38582f | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
long n = sc.nextLong();
long k = sc.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = sc.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 6b95cc6ae4b6f808da9ef3e06c76e4ae | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
long n = sc.nextLong();
long k = sc.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = sc.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 9e11741af329c09c086bf801f8bfeaba | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | a5935113a334560ee7b90f27f8b08987 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
long n = sc.nextLong();
long k = sc.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = sc.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 78eedcb2f0c7289ea2c7efd6d4a84d2f | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
long n = sc.nextLong();
long k = sc.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = sc.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | e65cea89c7803a3ca98235ef0c076a13 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] inp = br.readLine().split(" ");
int n = Integer.parseInt(inp[0]);
long k = Integer.parseInt(inp[1]);
long[] arr = new long[n];
inp = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(inp[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 82e4e2c4436779daa1049bce116dc722 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes |
import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) throws IOException {
Soumit sc = new Soumit();
int testcases = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (testcases-->0){
int n = sc.nextInt();
long k = sc.nextInt();
long[] arr = sc.nextLongArray(n);
if(n == 1){
long sum = arr[0] + (k - 1);
sb.append(sum).append("\n");
continue;
}
if(k <= n){
long sum = 0;
for(int i=0;i<k;i++){
sum += arr[i];
}
long max = sum;
for(int i=(int)k;i<n;i++){
sum += arr[i];
sum -= arr[(int) (i - k)];
max = Math.max(max, sum);
}
for(int i=1;i<=k;i++){
max += (k - i);
}
sb.append(max).append("\n");
continue;
}
long sum = 0;
for(int i=0;i<n;i++){
sum += arr[i];
}
for(int i=1;i<=n-2;i++){
sum += k-i;
}
k -= (n - 2);
if(k>0){
sum += 1;
k -= 2;
sum += (Math.max(0, k) * 2);
}
sb.append(sum).append("\n");
}
System.out.println(sb);
sc.close();
}
static class Soumit {
final private int BUFFER_SIZE = 1 << 18;
final private DataInputStream din;
final private byte[] buffer;
private PrintWriter pw;
private int bufferPointer, bytesRead;
StringTokenizer st;
public Soumit() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Soumit(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public void streamOutput(String file) throws IOException {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
pw = new PrintWriter(bw);
}
public void println(String a) {
pw.println(a);
}
public void print(String a) {
pw.print(a);
}
public String readLine() throws IOException {
byte[] buf = new byte[3000064]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public void sort(int[] arr) {
ArrayList<Integer> arlist = new ArrayList<>();
for (int i : arr)
arlist.add(i);
Collections.sort(arlist);
for (int i = 0; i < arr.length; i++)
arr[i] = arlist.get(i);
}
public void sort(long[] arr) {
ArrayList<Long> arlist = new ArrayList<>();
for (long i : arr)
arlist.add(i);
Collections.sort(arlist);
for (int i = 0; i < arr.length; i++)
arr[i] = arlist.get(i);
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = nextDouble();
}
return arr;
}
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;*/
if (din != null) din.close();
if (pw != null) pw.close();
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 5e2a604f83247704e503438f412c2743 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes |
import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) throws IOException {
Soumit sc = new Soumit();
int testcases = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (testcases-->0){
int n = sc.nextInt();
long k = sc.nextInt();
long[] arr = sc.nextLongArray(n);
if(n == 1){
long sum = arr[0] + (k - 1);
sb.append(sum).append("\n");
continue;
}
if(k <= n){
long sum = 0;
for(int i=0;i<k;i++){
sum += arr[i];
}
long max = sum;
for(int i=(int)k;i<n;i++){
sum += arr[i];
sum -= arr[(int) (i - k)];
max = Math.max(max, sum);
}
for(int i=1;i<=k;i++){
max += (k - i);
}
sb.append(max).append("\n");
continue;
}
long sum = 0;
for(int i=0;i<n;i++){
sum += arr[i];
}
for(int i=1;i<=n-2;i++){
sum += k-i;
}
k -= (n - 2);
if(k>0){
sum += 1;
k -= 2;
sum += (Math.max(0, k) * 2);
}
sb.append(sum).append("\n");
}
System.out.println(sb);
sc.close();
}
static class Soumit {
final private int BUFFER_SIZE = 1 << 18;
final private DataInputStream din;
final private byte[] buffer;
private PrintWriter pw;
private int bufferPointer, bytesRead;
StringTokenizer st;
public Soumit() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Soumit(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public void streamOutput(String file) throws IOException {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
pw = new PrintWriter(bw);
}
public void println(String a) {
pw.println(a);
}
public void print(String a) {
pw.print(a);
}
public String readLine() throws IOException {
byte[] buf = new byte[3000064]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public void sort(int[] arr) {
ArrayList<Integer> arlist = new ArrayList<>();
for (int i : arr)
arlist.add(i);
Collections.sort(arlist);
for (int i = 0; i < arr.length; i++)
arr[i] = arlist.get(i);
}
public void sort(long[] arr) {
ArrayList<Long> arlist = new ArrayList<>();
for (long i : arr)
arlist.add(i);
Collections.sort(arlist);
for (int i = 0; i < arr.length; i++)
arr[i] = arlist.get(i);
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = nextDouble();
}
return arr;
}
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;*/
if (din != null) din.close();
if (pw != null) pw.close();
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 17 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 55c70e9328676a0c35dd1fc7c071edcf | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class ProblemB {
public static void main(String[] args) throws IOException
{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(f.readLine());
int t = Integer.parseInt(st.nextToken());
for(int x = 0; x < t; x++)
{
st = new StringTokenizer(f.readLine());
long n = Long.parseLong(st.nextToken());
long k = Long.parseLong(st.nextToken());
int[] vals = new int[(int)n];
st = new StringTokenizer(f.readLine());
for(int i = 0; i < n; i++)
{
vals[i] = Integer.parseInt(st.nextToken());
}
if(k < n)
{
long sum = 0;
long max = 0;
for(int i = 0; i < k; i++)
{
sum+=vals[i];
}
max = Math.max(max, sum);
for(long i = k; i < n; i++)
{
sum-=vals[(int)(i-k)];
sum+=vals[(int)i];
max = Math.max(max, sum);
}
System.out.println(max+k*(k-1)/2);
}
else
{
long sum = 0;
for(int i = 0; i < n; i++)
{
sum+=vals[i];
}
sum+=(k-n);
sum+=(k-1)*(k)/2-(k-n)*(k-n+1)/2;
System.out.println(sum);
}
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | f2188689557e0f71ab5030169ca3d375 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.*;
import java.io.*;
public class A1687 {
final boolean DEBUG = true;
final int INF = Integer.MAX_VALUE;
final long LINF = Long.MAX_VALUE;
FastScanner sc;
PrintWriter out;
void solve() throws Exception {
int n = sc.nextInt();
int k = sc.nextInt();
var a = new long[n];
long s = 0;
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
s += a[i];
}
long ans = 0;
if (k < n) {
int r = 0;
long sw = a[0];
while (r+1 < k)
sw += a[++r];
long mx = sw;
int l = 0;
while (r+1 < n) {
sw += a[++r] - a[l++];
mx = Math.max(mx, sw);
}
//log("mx", mx);
ans += mx;
ans += ((long) k) * (k-1) / 2;
} else {
ans += s;
ans += k - (n - 1) - 1;
long d = k - (n - 1) - 1;
ans += (n-1) * (d + k) / 2;
}
out.println(ans);
}
void run() throws Exception {
Locale.setDefault(Locale.US);
sc = new FastScanner(System.in);
out = new PrintWriter(System.out);
int t = sc.nextInt();
for (; t > 0; t--) {
solve();
}
out.close();
sc.close();
}
void log(Object... objs) {
if (!DEBUG)
return;
var sb = new StringBuilder();
for (var x: objs)
sb.append(x.toString()).append(" ");
out.println(sb);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream in) throws Exception {
br = new BufferedReader(new InputStreamReader(in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String line = "";
try {
line = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return line;
}
void close() throws Exception {
br.close();
}
}
public static void main(String[] arg) throws Exception {
new A1687().run();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 289e13c6c6801e7b65eedceaa0512205 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.*;
import java.io.*;
public class A1687 {
final boolean DEBUG = true;
final int INF = Integer.MAX_VALUE;
final long LINF = Long.MAX_VALUE;
FastScanner sc;
PrintWriter out;
void solve() throws Exception {
int n = sc.nextInt();
int k = sc.nextInt();
var a = new long[n];
var pref = new long[n];
long s = 0;
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
if (i != 0)
pref[i] = pref[i-1] + a[i];
s += a[i];
}
long ans = 0;
if (k <= n) {
long mx = 0;
for (int i = 0; i+k-1 < n; i++) {
mx = Math.max(mx, pref[i+k-1] - pref[i] + a[i]);
}
ans += mx;
ans += ((long) k) * (k-1) / 2;
} else {
ans += s;
ans += k - (n - 1) - 1;
long d = k - (n - 1) - 1;
ans += (n-1) * (d + k) / 2;
}
out.println(ans);
}
void run() throws Exception {
Locale.setDefault(Locale.US);
sc = new FastScanner(System.in);
out = new PrintWriter(System.out);
int t = sc.nextInt();
for (; t > 0; t--) {
solve();
}
out.close();
sc.close();
}
void log(Object... objs) {
if (!DEBUG)
return;
var sb = new StringBuilder();
for (var x: objs)
sb.append(x.toString()).append(" ");
out.println(sb);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream in) throws Exception {
br = new BufferedReader(new InputStreamReader(in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String line = "";
try {
line = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return line;
}
void close() throws Exception {
br.close();
}
}
public static void main(String[] arg) throws Exception {
new A1687().run();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | fc6119b6666510439898e30fecfba4e4 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.*;
public class q1687a {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bd.readLine());
for (int i = 0; i < t; i++) {
StringTokenizer st = new StringTokenizer(bd.readLine());
int n = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
int[] v = new int[n];
st = new StringTokenizer(bd.readLine());
for (int j = 0; j < n; j++) {
v[j] = Integer.parseInt(st.nextToken());
}
System.out.println(ans(v, n, d));
}
}
public static long ans(int[] v, long n, long d) {
if (d >= n) {
long sum = 0;
for (int i = 0; i < n; i++)
sum += v[i];
return sum + n * d - (n * (n + 1) / 2);
} else {
long sum = 0;
for (int i = 0; i < d; i++)
sum += v[i];
long mx = sum;
for (int i = (int) d; i < n; i++) {
sum += v[i];
sum -= v[(int) (i - d)];
if (sum > mx)
mx = sum;
}
return mx + (d * (d - 1) / 2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 990d8b6d18a0d62823a144f761005e6a | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.*;
public class q1687a {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bd.readLine());
for (int i = 0; i < t; i++) {
StringTokenizer st = new StringTokenizer(bd.readLine());
int n = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
int[] v = new int[n];
st = new StringTokenizer(bd.readLine());
for (int j = 0; j < n; j++) {
v[j] = Integer.parseInt(st.nextToken());
}
System.out.println(ans(v, n, d));
}
}
public static long ans(int[] v, long n, long d) {
if (d >= n) {
long sum = 0;
for (int i = 0; i < n; i++)
sum += v[i];
return sum + n * d - (n * (n + 1) / 2);
} else {
long sum = 0;
for (int i = 0; i < d; i++)
sum += v[i];
long max = sum;
for (int i = (int) d; i < n; i++) {
sum += v[i];
sum -= v[(int) (i - d)];
if (sum > max)
max = sum;
}
return max + (d * (d - 1) / 2);
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 2f501d7aed3befbcfde870bb60bf101a | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class q1687a {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
for (int i = 0; i < t; i++) {
StringTokenizer st = new StringTokenizer(in.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] nums = new int[n];
st = new StringTokenizer(in.readLine());
for (int j = 0; j < n; j++) {
nums[j] = Integer.parseInt(st.nextToken());
}
System.out.println(solve(nums, n, k));
}
}
public static long solve(int[] nums, long n, long k) {
if (k >= n) {
long sum = 0;
for (int i = 0; i < n; i++)
sum += nums[i];
return sum + n * k - (n * (n + 1) / 2);
} else {
long sum = 0;
for (int i = 0; i < k; i++)
sum += nums[i];
long max = sum;
for (int i = (int) k; i < n; i++) {
sum += nums[i];
sum -= nums[(int) (i - k)];
if (sum > max)
max = sum;
}
return max + (k * (k - 1) / 2);
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | a87aa5239ef7e15f34685c3210044fcc | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static final long mod = 998244353;
static PrintWriter out = new PrintWriter(System.out);
static FastScanner sc = new FastScanner();
static void solve() {
int n = sc.nextInt();
int k = sc.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
long ans = 0;
if (k <= n) {
long mx = 0, now = 0;
for (int i = 0; i < k - 1; i++) {
now += a[i];
}
for (int i = k - 1; i < n; i++) {
now += a[i];
mx = Math.max(mx, now);
now -= a[i - k + 1];
}
ans = mx + ((long)k * (k - 1)) / 2;
} else {
long now = 0;
for (int i = 0; i < n; i++) {
now += a[i];
}
long r = k - (n);
now += r;
now += ((long)k * (k - 1)) / 2 - (r * (r + 1)) / 2;
ans = now;
}
System.out.println(ans);
}
public static void main(String[] args) {
int t = 1;
t = sc.nextInt();
while (t-- > 0) {
solve();
}
out.close();
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String readLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
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 | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 3726e52c6cd514dc3808778281b86af3 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class A_The_Enchanted_Forest {
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
long[] getArr(int n) {
long[] res = new long[n];
for(int i = 0; i < n; i++){
res[i] = Long.parseLong(next());
}
return res;
}
}
public static void main(String[] args) {
FastScanner fs = new FastScanner();
Integer t = fs.nextInt();
for(int i = 0; i < t; i++){
Integer len = fs.nextInt();
Integer k = fs.nextInt();
int[] arr = new int[len];
for(int j = 0; j < len; j++){
Integer a = fs.nextInt();
arr[j] = a;
}
if(arr.length != 1){
solve(arr,k);
}
else{
long res = (long)(k-1) + (long)arr[0];
System.out.println(res);
}
}
}
public static void solve(int[] arr, int k){
int n = arr.length;
if(k > n-1){
int left = k % (n-1);
int count = 0;
int total = 0;
int[] got = new int[n];
long res = 0l;
for(int c : arr){
res += (long) c;
}
int cur = 1;
boolean minus = true;
if(left == 0){
left= n-1;
}
while(count < n && total < k){
left -= minus? 1 : -1;
long add = (long)cur - (long)got[left];
if(got[left] == 0){
add -= 1l;
count += 1;
}
// System.out.println(add+" "+left);
got[left] = cur;
res += add;
cur += 1;
total += 1;
if(left == 0){
minus = false;
}
if(left == n){
minus = true;
}
// System.out.println(res+" "+count+" "+left);
}
int v = (k - total)/(n-1);
res += v > 0? (long)(n-1) * (long)n * (long)v:0l;
System.out.println(res);
}
else{
long cur = 0l;
for(int i = 0; i < k; i++){
cur += (long) arr[i];
}
long max = cur;
for(int i = k; i < n; i++){
cur += (long) arr[i];
cur -= (long) arr[i-k];
max = Math.max(max,cur);
}
long res = max + (long)k*(long)(k-1)/2;
System.out.println(res);
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | c430fc5bb9321b025e0f2bdb396e00c1 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
// import java.math.BigInteger;
public class Main {
public static FastReader in;
public static PrintWriter out;
public static long mod = (long)(998244353);
public static void solve(int nTestCase){
int N = in.nextInt(), K = in.nextInt();
int[] a = in.readArray(N);
long sum = 0;
if(K>=N){
// fastSort(a);
long remTime = Math.max(0,K-N);
for(int i=N-1;i>=0;i--){
sum += (a[i]+remTime);
remTime++;
// if(remTime==K) break;
}
out.println(sum);
}
else{
long currSum = 0;
for(int i=0;i<K;i++){
currSum += a[i];
}
sum = currSum;
int i = 0, j = K;
while(j<N){
currSum += a[j] - a[i];
i++;
j++;
sum = Math.max(sum,currSum);
}
// out.println("sum "+sum);
out.println(sum+(K*(long)(K-1))/2);
}
}
public static int gcd(int a,int b){
if(a==0) return b;
else return gcd(b%a,a);
}
public static void fastSort(int[] a){
Random random = new Random();
for(int i=0;i<a.length;i++){
int next = random.nextInt(a.length);
int curr = a[next];
a[next] = a[i];
a[i] = curr;
}
Arrays.sort(a);
}
public static void main(String[] args) throws IOException {
boolean isInputOutputFiles = args!=null&&args.length>0&&args[0].equals("LOCAL_RUN");
// isInputOutputFiles = false;
if (isInputOutputFiles) {
in = new FastReader(new BufferedReader(new FileReader("./input/input.txt")));
out = new PrintWriter(new BufferedWriter(new FileWriter("./output/output.txt")));
}
else{
in = new FastReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
long startT = 0;
if (isInputOutputFiles) startT = System.currentTimeMillis();
int T = 1;
T = in.nextInt();
for (int tt = 1; tt <= T; tt++) {
solve(tt);
}
if (isInputOutputFiles) out.println("Total time: "+(System.currentTimeMillis()-startT)+"ms");
out.close();
}
static class Pair implements Comparable<Pair> {
int a;
int b;
public Pair(int a,int b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair o) {
if(this.a==o.a)
return (int)(this.b-o.b);
if((this.a - o.a)>0) return 1;
else if((this.a - o.a)<0) return -1;
else return (int) (this.a - o.a);
}
}
static class SegTree{
int leftmost,rightmost;
int xorAns;
SegTree left,right;
public SegTree(int leftmost,int rightmost,int[] a) {
this.leftmost = leftmost;
this.rightmost = rightmost;
if(leftmost==rightmost) {
this.xorAns = a[leftmost];
return;
}
int mid = (leftmost+rightmost)/2;
left = new SegTree(leftmost,mid,a);
right = new SegTree(mid+1,rightmost,a);
recalc();
}
public void recalc() {
if(leftmost==rightmost) return;
xorAns = left.xorAns^right.xorAns;
return;
}
public void pointUpdate(int index,int newVal) {
if(leftmost==rightmost) {
this.xorAns = newVal;
return;
}
if(index<=left.rightmost) left.pointUpdate(index, newVal);
else right.pointUpdate(index, newVal);
recalc();
}
public int rangeQuery(int l, int r){
if(l>rightmost||r<leftmost) return 0;
if(l<=leftmost && r>=rightmost) return xorAns;
return left.rangeQuery(l,r)^right.rangeQuery(l,r);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(Reader rd) {
br = new BufferedReader(rd);
}
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[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 6cb109b70be9eb9eed52363e884944e9 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | e8a61eaf2ba710b82ec5b4f2e902ea7a | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Q1687A {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | eaddedb6a9216ad3ae6d7b0a2249ed22 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | // Imports
import java.io.*;
import java.util.*;
public class A1687 {
public static void main(String[] args) throws IOException {
// Test once done
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
// BufferedReader f = new BufferedReader(new StringReader("4\n" +
//"5 2\n" +
//"5 6 1 2 3\n" +
//"5 7\n" +
//"5 6 1 2 3\n" +
//"1 2\n" +
//"999999\n" +
//"5 70000\n" +
//"1000000000 1000000000 1000000000 1000000000 1000000000"));
// Picking twice is equivalentto just picking at the latest time
// So we should seek to maximize the squares we visit, picking at
// the latest for each one
// So we should find the range with maximum: rolling window impl
int T = Integer.parseInt(f.readLine());
for(int i = 0; i < T; i++) {
// Do stuff
StringTokenizer st = new StringTokenizer(f.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
long current = 0;
long best = 0;
StringTokenizer line = new StringTokenizer(f.readLine());
int[] arr = new int[N];
for(int j = 0; j < Math.min(K, N); j++) {
arr[j] = Integer.parseInt(line.nextToken());
current += arr[j];
}
best = current;
for(int j = K; j < N; j++) {
arr[j] = Integer.parseInt(line.nextToken());
current += arr[j];
current -= arr[j - K];
best = Math.max(best, current);
}
// Now best has best window value, add in accumulator
long accumulator = K * (long)(K - 1) / 2;
if(K > N) {
accumulator -= (K - N) * (long)(K - N - 1) / 2;
}
System.out.println(best + accumulator);
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 3aa44f21f4613276065027897bcbb55e | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
import java.util.*;
/**
*
* @author eslam
*/
public class IceCave {
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
String nextLine() {
String str = "";
try {
str = r.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
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());
}
}
// Beginning of the solution
static Kattio input = new Kattio();
static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
static ArrayList<ArrayList<Long>> powerSet = new ArrayList<>();
static long mod = (long) (Math.pow(10, 9) + 7);
static int dp[][];
public static void main(String[] args) throws IOException {
int t = input.nextInt();
while (t-- > 0) {
int n = input.nextInt();
long k = input.nextInt();
Long a[] = new Long[n];
long ans = 0;
for (int i = 0; i < n; i++) {
a[i] = input.nextLong();
}
if (k < n) {
for (int i = 1; i < n; i++) {
a[i] += a[i - 1];
}
for (int i = 0; i < n; i++) {
if (i - (k - 1) > -1) {
if (i - (k - 1) > 0) {
ans = Math.max(a[i] - a[i - (int) k], ans);
} else {
ans = Math.max(a[i], ans);
}
}
if (i + (k - 1) < n) {
if (i == 0) {
ans = Math.max(a[i + (int) (k - 1)], ans);
} else {
ans = Math.max(a[i + (int) (k - 1)] - a[i - 1], ans);
}
}
}
ans += ((k) * (k - 1)) / 2;
} else {
for (int i = 0; i < n; i++) {
ans += a[i] + (long) i;
}
k -= n;
ans += k * (long) n;
}
log.write(ans + "\n");
}
log.flush();
}
public static int[] bfs(int node, ArrayList<Integer> a[]) {
Queue<Integer> q = new LinkedList<>();
q.add(node);
int distances[] = new int[a.length];
Arrays.fill(distances, -1);
distances[node] = 0;
while (!q.isEmpty()) {
int parent = q.poll();
ArrayList<Integer> nodes = a[parent];
int cost = distances[parent];
for (Integer node1 : nodes) {
if (distances[node1] == -1) {
q.add(node1);
distances[node1] = cost + 1;
}
}
}
return distances;
}
public static int get(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
public static long primeFactors(int n) {
long sum = 1;
while (n % 2 == 0) {
sum *= 2l;
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
sum *= (long) i;
n /= i;
}
if (n < i) {
break;
}
}
if (n > 2) {
sum *= n;
}
return sum;
}
public static ArrayList<Integer> printPrimeFactoriztion(int n) {
ArrayList<Integer> a = new ArrayList<>();
for (int i = 1; i < Math.sqrt(n) + 1; i++) {
if (n % i == 0) {
if (isPrime(i)) {
a.add(i);
n /= i;
i = 0;
} else if (isPrime(n / i)) {
a.add(n / i);
n = i;
i = 0;
}
}
}
return a;
}
// end of solution
public static void genrate(int ind, long[] a, ArrayList<Long> sub) {
if (ind == a.length) {
powerSet.add(sub);
return;
}
ArrayList<Long> have = new ArrayList<>();
ArrayList<Long> less = new ArrayList<>();
for (int i = 0; i < sub.size(); i++) {
have.add(sub.get(i));
less.add(sub.get(i));
}
have.add(a[ind]);
genrate(ind + 1, a, have);
genrate(ind + 1, a, less);
}
public static long factorial(long n) {
if (n <= 1) {
return 1;
}
long t = n - 1;
while (t > 1) {
n *= t;
t--;
}
return n;
}
public static boolean isPalindrome(int n) {
int t = n;
int ans = 0;
while (t > 0) {
ans = ans * 10 + t % 10;
t /= 10;
}
return ans == n;
}
static class tri {
int x, y, z;
public tri(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
static boolean isPrime(long num) {
if (num == 1) {
return false;
}
if (num == 2) {
return true;
}
if (num % 2 == 0) {
return false;
}
if (num == 3) {
return true;
}
for (int i = 3; i <= Math.sqrt(num) + 1; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void prefixSum(int[] a) {
for (int i = 1; i < a.length; i++) {
a[i] = a[i] + a[i - 1];
}
}
public static void suffixSum(long[] a) {
for (int i = a.length - 2; i > -1; i--) {
a[i] = a[i] + a[i + 1];
}
}
static long mod(long a, long b) {
long r = a % b;
return r < 0 ? r + b : r;
}
public static long binaryToDecimal(String w) {
long r = 0;
long l = 0;
for (int i = w.length() - 1; i > -1; i--) {
long x = (w.charAt(i) - '0') * (long) Math.pow(2, l);
r = r + x;
l++;
}
return r;
}
public static String decimalToBinary(long n) {
String w = "";
while (n > 0) {
w = n % 2 + w;
n /= 2;
}
return w;
}
public static boolean isSorted(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
public static void print(int a[]) throws IOException {
for (int i = 0; i < a.length; i++) {
log.write(a[i] + " ");
}
log.write("\n");
}
public static void read(int[] a) {
for (int i = 0; i < a.length; i++) {
a[i] = input.nextInt();
}
}
static class pair {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
public static long LCM(long x, long y) {
return x / GCD(x, y) * y;
}
public static long GCD(long x, long y) {
if (y == 0) {
return x;
}
return GCD(y, x % y);
}
public static void simplifyTheFraction(long a, long b) {
long GCD = GCD(a, b);
System.out.println(a / GCD + " " + b / GCD);
}
/**
*/ // class RedBlackNode
static class RedBlackNode<T extends Comparable<T>> {
/**
* Possible color for this node
*/
public static final int BLACK = 0;
/**
* Possible color for this node
*/
public static final int RED = 1;
// the key of each node
public T key;
/**
* Parent of node
*/
RedBlackNode<T> parent;
/**
* Left child
*/
RedBlackNode<T> left;
/**
* Right child
*/
RedBlackNode<T> right;
// the number of elements to the left of each node
public int numLeft = 0;
// the number of elements to the right of each node
public int numRight = 0;
// the color of a node
public int color;
RedBlackNode() {
color = BLACK;
numLeft = 0;
numRight = 0;
parent = null;
left = null;
right = null;
}
// Constructor which sets key to the argument.
RedBlackNode(T key) {
this();
this.key = key;
}
}// end class RedBlackNode
static class RedBlackTree<T extends Comparable<T>> {
// Root initialized to nil.
private RedBlackNode<T> nil = new RedBlackNode<T>();
private RedBlackNode<T> root = nil;
public RedBlackTree() {
root.left = nil;
root.right = nil;
root.parent = nil;
}
// @param: X, The node which the lefRotate is to be performed on.
// Performs a leftRotate around X.
private void leftRotate(RedBlackNode<T> x) {
// Call leftRotateFixup() which updates the numLeft
// and numRight values.
leftRotateFixup(x);
// Perform the left rotate as described in the algorithm
// in the course text.
RedBlackNode<T> y;
y = x.right;
x.right = y.left;
// Check for existence of y.left and make pointer changes
if (!isNil(y.left)) {
y.left.parent = x;
}
y.parent = x.parent;
// X's parent is nul
if (isNil(x.parent)) {
root = y;
} // X is the left child of it's parent
else if (x.parent.left == x) {
x.parent.left = y;
} // X is the right child of it's parent.
else {
x.parent.right = y;
}
// Finish of the leftRotate
y.left = x;
x.parent = y;
}// end leftRotate(RedBlackNode X)
// @param: X, The node which the leftRotate is to be performed on.
// Updates the numLeft & numRight values affected by leftRotate.
private void leftRotateFixup(RedBlackNode x) {
// Case 1: Only X, X.right and X.right.right always are not nil.
if (isNil(x.left) && isNil(x.right.left)) {
x.numLeft = 0;
x.numRight = 0;
x.right.numLeft = 1;
} // Case 2: X.right.left also exists in addition to Case 1
else if (isNil(x.left) && !isNil(x.right.left)) {
x.numLeft = 0;
x.numRight = 1 + x.right.left.numLeft
+ x.right.left.numRight;
x.right.numLeft = 2 + x.right.left.numLeft
+ x.right.left.numRight;
} // Case 3: X.left also exists in addition to Case 1
else if (!isNil(x.left) && isNil(x.right.left)) {
x.numRight = 0;
x.right.numLeft = 2 + x.left.numLeft + x.left.numRight;
} // Case 4: X.left and X.right.left both exist in addtion to Case 1
else {
x.numRight = 1 + x.right.left.numLeft
+ x.right.left.numRight;
x.right.numLeft = 3 + x.left.numLeft + x.left.numRight
+ x.right.left.numLeft + x.right.left.numRight;
}
}// end leftRotateFixup(RedBlackNode X)
// @param: X, The node which the rightRotate is to be performed on.
// Updates the numLeft and numRight values affected by the Rotate.
private void rightRotate(RedBlackNode<T> y) {
// Call rightRotateFixup to adjust numRight and numLeft values
rightRotateFixup(y);
// Perform the rotate as described in the course text.
RedBlackNode<T> x = y.left;
y.left = x.right;
// Check for existence of X.right
if (!isNil(x.right)) {
x.right.parent = y;
}
x.parent = y.parent;
// y.parent is nil
if (isNil(y.parent)) {
root = x;
} // y is a right child of it's parent.
else if (y.parent.right == y) {
y.parent.right = x;
} // y is a left child of it's parent.
else {
y.parent.left = x;
}
x.right = y;
y.parent = x;
}// end rightRotate(RedBlackNode y)
// @param: y, the node around which the righRotate is to be performed.
// Updates the numLeft and numRight values affected by the rotate
private void rightRotateFixup(RedBlackNode y) {
// Case 1: Only y, y.left and y.left.left exists.
if (isNil(y.right) && isNil(y.left.right)) {
y.numRight = 0;
y.numLeft = 0;
y.left.numRight = 1;
} // Case 2: y.left.right also exists in addition to Case 1
else if (isNil(y.right) && !isNil(y.left.right)) {
y.numRight = 0;
y.numLeft = 1 + y.left.right.numRight
+ y.left.right.numLeft;
y.left.numRight = 2 + y.left.right.numRight
+ y.left.right.numLeft;
} // Case 3: y.right also exists in addition to Case 1
else if (!isNil(y.right) && isNil(y.left.right)) {
y.numLeft = 0;
y.left.numRight = 2 + y.right.numRight + y.right.numLeft;
} // Case 4: y.right & y.left.right exist in addition to Case 1
else {
y.numLeft = 1 + y.left.right.numRight
+ y.left.right.numLeft;
y.left.numRight = 3 + y.right.numRight
+ y.right.numLeft
+ y.left.right.numRight + y.left.right.numLeft;
}
}// end rightRotateFixup(RedBlackNode y)
public void insert(T key) {
insert(new RedBlackNode<T>(key));
}
// @param: z, the node to be inserted into the Tree rooted at root
// Inserts z into the appropriate position in the RedBlackTree while
// updating numLeft and numRight values.
private void insert(RedBlackNode<T> z) {
// Create a reference to root & initialize a node to nil
RedBlackNode<T> y = nil;
RedBlackNode<T> x = root;
// While we haven't reached a the end of the tree keep
// tryint to figure out where z should go
while (!isNil(x)) {
y = x;
// if z.key is < than the current key, go left
if (z.key.compareTo(x.key) < 0) {
// Update X.numLeft as z is < than X
x.numLeft++;
x = x.left;
} // else z.key >= X.key so go right.
else {
// Update X.numGreater as z is => X
x.numRight++;
x = x.right;
}
}
// y will hold z's parent
z.parent = y;
// Depending on the value of y.key, put z as the left or
// right child of y
if (isNil(y)) {
root = z;
} else if (z.key.compareTo(y.key) < 0) {
y.left = z;
} else {
y.right = z;
}
// Initialize z's children to nil and z's color to red
z.left = nil;
z.right = nil;
z.color = RedBlackNode.RED;
// Call insertFixup(z)
insertFixup(z);
}// end insert(RedBlackNode z)
// @param: z, the node which was inserted and may have caused a violation
// of the RedBlackTree properties
// Fixes up the violation of the RedBlackTree properties that may have
// been caused during insert(z)
private void insertFixup(RedBlackNode<T> z) {
RedBlackNode<T> y = nil;
// While there is a violation of the RedBlackTree properties..
while (z.parent.color == RedBlackNode.RED) {
// If z's parent is the the left child of it's parent.
if (z.parent == z.parent.parent.left) {
// Initialize y to z 's cousin
y = z.parent.parent.right;
// Case 1: if y is red...recolor
if (y.color == RedBlackNode.RED) {
z.parent.color = RedBlackNode.BLACK;
y.color = RedBlackNode.BLACK;
z.parent.parent.color = RedBlackNode.RED;
z = z.parent.parent;
} // Case 2: if y is black & z is a right child
else if (z == z.parent.right) {
// leftRotaet around z's parent
z = z.parent;
leftRotate(z);
} // Case 3: else y is black & z is a left child
else {
// recolor and rotate round z's grandpa
z.parent.color = RedBlackNode.BLACK;
z.parent.parent.color = RedBlackNode.RED;
rightRotate(z.parent.parent);
}
} // If z's parent is the right child of it's parent.
else {
// Initialize y to z's cousin
y = z.parent.parent.left;
// Case 1: if y is red...recolor
if (y.color == RedBlackNode.RED) {
z.parent.color = RedBlackNode.BLACK;
y.color = RedBlackNode.BLACK;
z.parent.parent.color = RedBlackNode.RED;
z = z.parent.parent;
} // Case 2: if y is black and z is a left child
else if (z == z.parent.left) {
// rightRotate around z's parent
z = z.parent;
rightRotate(z);
} // Case 3: if y is black and z is a right child
else {
// recolor and rotate around z's grandpa
z.parent.color = RedBlackNode.BLACK;
z.parent.parent.color = RedBlackNode.RED;
leftRotate(z.parent.parent);
}
}
}
// Color root black at all times
root.color = RedBlackNode.BLACK;
}// end insertFixup(RedBlackNode z)
// @param: node, a RedBlackNode
// @param: node, the node with the smallest key rooted at node
public RedBlackNode<T> treeMinimum(RedBlackNode<T> node) {
// while there is a smaller key, keep going left
while (!isNil(node.left)) {
node = node.left;
}
return node;
}// end treeMinimum(RedBlackNode node)
// @param: X, a RedBlackNode whose successor we must find
// @return: return's the node the with the next largest key
// from X.key
public RedBlackNode<T> treeSuccessor(RedBlackNode<T> x) {
// if X.left is not nil, call treeMinimum(X.right) and
// return it's value
if (!isNil(x.left)) {
return treeMinimum(x.right);
}
RedBlackNode<T> y = x.parent;
// while X is it's parent's right child...
while (!isNil(y) && x == y.right) {
// Keep moving up in the tree
x = y;
y = y.parent;
}
// Return successor
return y;
}// end treeMinimum(RedBlackNode X)
// @param: z, the RedBlackNode which is to be removed from the the tree
// Remove's z from the RedBlackTree rooted at root
public void remove(RedBlackNode<T> v) {
RedBlackNode<T> z = search(v.key);
// Declare variables
RedBlackNode<T> x = nil;
RedBlackNode<T> y = nil;
// if either one of z's children is nil, then we must remove z
if (isNil(z.left) || isNil(z.right)) {
y = z;
} // else we must remove the successor of z
else {
y = treeSuccessor(z);
}
// Let X be the left or right child of y (y can only have one child)
if (!isNil(y.left)) {
x = y.left;
} else {
x = y.right;
}
// link X's parent to y's parent
x.parent = y.parent;
// If y's parent is nil, then X is the root
if (isNil(y.parent)) {
root = x;
} // else if y is a left child, set X to be y's left sibling
else if (!isNil(y.parent.left) && y.parent.left == y) {
y.parent.left = x;
} // else if y is a right child, set X to be y's right sibling
else if (!isNil(y.parent.right) && y.parent.right == y) {
y.parent.right = x;
}
// if y != z, trasfer y's satellite data into z.
if (y != z) {
z.key = y.key;
}
// Update the numLeft and numRight numbers which might need
// updating due to the deletion of z.key.
fixNodeData(x, y);
// If y's color is black, it is a violation of the
// RedBlackTree properties so call removeFixup()
if (y.color == RedBlackNode.BLACK) {
removeFixup(x);
}
}// end remove(RedBlackNode z)
// @param: y, the RedBlackNode which was actually deleted from the tree
// @param: key, the value of the key that used to be in y
private void fixNodeData(RedBlackNode<T> x, RedBlackNode<T> y) {
// Initialize two variables which will help us traverse the tree
RedBlackNode<T> current = nil;
RedBlackNode<T> track = nil;
// if X is nil, then we will start updating at y.parent
// Set track to y, y.parent's child
if (isNil(x)) {
current = y.parent;
track = y;
} // if X is not nil, then we start updating at X.parent
// Set track to X, X.parent's child
else {
current = x.parent;
track = x;
}
// while we haven't reached the root
while (!isNil(current)) {
// if the node we deleted has a different key than
// the current node
if (y.key != current.key) {
// if the node we deleted is greater than
// current.key then decrement current.numRight
if (y.key.compareTo(current.key) > 0) {
current.numRight--;
}
// if the node we deleted is less than
// current.key thendecrement current.numLeft
if (y.key.compareTo(current.key) < 0) {
current.numLeft--;
}
} // if the node we deleted has the same key as the
// current node we are checking
else {
// the cases where the current node has any nil
// children and update appropriately
if (isNil(current.left)) {
current.numLeft--;
} else if (isNil(current.right)) {
current.numRight--;
} // the cases where current has two children and
// we must determine whether track is it's left
// or right child and update appropriately
else if (track == current.right) {
current.numRight--;
} else if (track == current.left) {
current.numLeft--;
}
}
// update track and current
track = current;
current = current.parent;
}
}//end fixNodeData()
// @param: X, the child of the deleted node from remove(RedBlackNode v)
// Restores the Red Black properties that may have been violated during
// the removal of a node in remove(RedBlackNode v)
private void removeFixup(RedBlackNode<T> x) {
RedBlackNode<T> w;
// While we haven't fixed the tree completely...
while (x != root && x.color == RedBlackNode.BLACK) {
// if X is it's parent's left child
if (x == x.parent.left) {
// set w = X's sibling
w = x.parent.right;
// Case 1, w's color is red.
if (w.color == RedBlackNode.RED) {
w.color = RedBlackNode.BLACK;
x.parent.color = RedBlackNode.RED;
leftRotate(x.parent);
w = x.parent.right;
}
// Case 2, both of w's children are black
if (w.left.color == RedBlackNode.BLACK
&& w.right.color == RedBlackNode.BLACK) {
w.color = RedBlackNode.RED;
x = x.parent;
} // Case 3 / Case 4
else {
// Case 3, w's right child is black
if (w.right.color == RedBlackNode.BLACK) {
w.left.color = RedBlackNode.BLACK;
w.color = RedBlackNode.RED;
rightRotate(w);
w = x.parent.right;
}
// Case 4, w = black, w.right = red
w.color = x.parent.color;
x.parent.color = RedBlackNode.BLACK;
w.right.color = RedBlackNode.BLACK;
leftRotate(x.parent);
x = root;
}
} // if X is it's parent's right child
else {
// set w to X's sibling
w = x.parent.left;
// Case 1, w's color is red
if (w.color == RedBlackNode.RED) {
w.color = RedBlackNode.BLACK;
x.parent.color = RedBlackNode.RED;
rightRotate(x.parent);
w = x.parent.left;
}
// Case 2, both of w's children are black
if (w.right.color == RedBlackNode.BLACK
&& w.left.color == RedBlackNode.BLACK) {
w.color = RedBlackNode.RED;
x = x.parent;
} // Case 3 / Case 4
else {
// Case 3, w's left child is black
if (w.left.color == RedBlackNode.BLACK) {
w.right.color = RedBlackNode.BLACK;
w.color = RedBlackNode.RED;
leftRotate(w);
w = x.parent.left;
}
// Case 4, w = black, and w.left = red
w.color = x.parent.color;
x.parent.color = RedBlackNode.BLACK;
w.left.color = RedBlackNode.BLACK;
rightRotate(x.parent);
x = root;
}
}
}// end while
// set X to black to ensure there is no violation of
// RedBlack tree Properties
x.color = RedBlackNode.BLACK;
}// end removeFixup(RedBlackNode X)
// @param: key, the key whose node we want to search for
// @return: returns a node with the key, key, if not found, returns null
// Searches for a node with key k and returns the first such node, if no
// such node is found returns null
public RedBlackNode<T> search(T key) {
// Initialize a pointer to the root to traverse the tree
RedBlackNode<T> current = root;
// While we haven't reached the end of the tree
while (!isNil(current)) {
// If we have found a node with a key equal to key
if (current.key.equals(key)) // return that node and exit search(int)
{
return current;
} // go left or right based on value of current and key
else if (current.key.compareTo(key) < 0) {
current = current.right;
} // go left or right based on value of current and key
else {
current = current.left;
}
}
// we have not found a node whose key is "key"
return null;
}// end search(int key)
// @param: key, any Comparable object
// @return: return's the number of elements greater than key
public int numGreater(T key) {
// Call findNumGreater(root, key) which will return the number
// of nodes whose key is greater than key
return findNumGreater(root, key);
}// end numGreater(int key)
// @param: key, any Comparable object
// @return: return's teh number of elements smaller than key
public int numSmaller(T key) {
// Call findNumSmaller(root,key) which will return
// the number of nodes whose key is greater than key
return findNumSmaller(root, key);
}// end numSmaller(int key)
// @param: node, the root of the tree, the key who we must
// compare other node key's to.
// @return: the number of nodes greater than key.
public int findNumGreater(RedBlackNode<T> node, T key) {
// Base Case: if node is nil, return 0
if (isNil(node)) {
return 0;
} // If key is less than node.key, all elements right of node are
// greater than key, add this to our total and look to the left
else if (key.compareTo(node.key) < 0) {
return 1 + node.numRight + findNumGreater(node.left, key);
} // If key is greater than node.key, then look to the right as
// all elements to the left of node are smaller than key
else {
return findNumGreater(node.right, key);
}
}// end findNumGreater(RedBlackNode, int key)
/**
* Returns sorted list of keys greater than key. Size of list will not
* exceed maxReturned
*
* @param key Key to search for
* @param maxReturned Maximum number of results to return
* @return List of keys greater than key. List may not exceed
* maxReturned
*/
public List<T> getGreaterThan(T key, Integer maxReturned) {
List<T> list = new ArrayList<T>();
getGreaterThan(root, key, list);
return list.subList(0, Math.min(maxReturned, list.size()));
}
private void getGreaterThan(RedBlackNode<T> node, T key,
List<T> list) {
if (isNil(node)) {
return;
} else if (node.key.compareTo(key) > 0) {
getGreaterThan(node.left, key, list);
list.add(node.key);
getGreaterThan(node.right, key, list);
} else {
getGreaterThan(node.right, key, list);
}
}
// @param: node, the root of the tree, the key who we must compare other
// node key's to.
// @return: the number of nodes smaller than key.
public int findNumSmaller(RedBlackNode<T> node, T key) {
// Base Case: if node is nil, return 0
if (isNil(node)) {
return 0;
} // If key is less than node.key, look to the left as all
// elements on the right of node are greater than key
else if (key.compareTo(node.key) <= 0) {
return findNumSmaller(node.left, key);
} // If key is larger than node.key, all elements to the left of
// node are smaller than key, add this to our total and look
// to the right.
else {
return 1 + node.numLeft + findNumSmaller(node.right, key);
}
}// end findNumSmaller(RedBlackNode nod, int key)
// @param: node, the RedBlackNode we must check to see whether it's nil
// @return: return's true of node is nil and false otherwise
private boolean isNil(RedBlackNode node) {
// return appropriate value
return node == nil;
}// end isNil(RedBlackNode node)
// @return: return's the size of the tree
// Return's the # of nodes including the root which the RedBlackTree
// rooted at root has.
public int size() {
// Return the number of nodes to the root's left + the number of
// nodes on the root's right + the root itself.
return root.numLeft + root.numRight + 1;
}// end size()
}// end class RedBlackTree
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | cd5e4008917f8dcd3585bd7abb9f90ea | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | //Some of the methods are copied from GeeksforGeeks Website
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
//static Scanner sc=new Scanner(System.in);
//static Reader sc=new Reader();
static FastReader sc=new FastReader(System.in);
static long mod = (long)(1e9)+ 7;
static int max_num=(int)1e5+5;
public static void main (String[] args) throws java.lang.Exception
{
// try{
/*
Collections.sort(al,(a,b)->a.x-b.x);
Collections.sort(al,Collections.reverseOrder());
long n=sc.nextLong();
String s=sc.next();
char a[]=s.toCharArray();
StringBuilder sb=new StringBuilder();
map.put(a[i],map.getOrDefault(a[i],0)+1);
map.putIfAbsent(x,new ArrayList<>());
out.println("Case #"+tt+": "+ans );
*/
int t = sc.nextInt();
for(int tt=1;tt<=t;tt++)
{
int n=sc.nextInt();
long k=sc.nextInt();
long a[]=new long[n];
long sum=0;
for(int i=0;i<n;i++)
{
a[i]=sc.nextLong();
sum+=a[i];
}
long max=0,cur=0;
int kk=Math.min((int)k,n);
for(int i=0;i<n;i++)
{
cur+=a[i];
if(i==kk-1)
max=Math.max(max,cur);
if(i>=kk)
{
cur-=a[i-kk];
max=Math.max(max,cur);
}
}
if(k>=n)
{
long total=n*k;
long sub=n*(n+1l)/2l;
max+=total-sub;
}
else
{
max+=k*(k-1l)/2l;
}
out.println(max);
}
out.flush();
out.close();
// }
// catch(Exception e)
// {}
}
/*
...SOLUTION ENDS HERE...........SOLUTION ENDS HERE...
*/
static void flag(boolean flag)
{
out.println(flag ? "YES" : "NO");
out.flush();
}
/*
Map<Long,Long> map=new HashMap<>();
for(int i=0;i<n;i++)
{
if(!map.containsKey(a[i]))
map.put(a[i],1);
else
map.replace(a[i],map.get(a[i])+1);
}
Set<Map.Entry<Long,Long>> hmap=map.entrySet();
for(Map.Entry<Long,Long> data : hmap)
{
}
Iterator<Integer> itr = set.iterator();
while(itr.hasNext())
{
int val=itr.next();
}
*/
// static class Pair
// {
// int x,y;
// Pair(int x,int y)
// {
// this.x=x;
// this.y=y;
// }
// }
// Arrays.sort(p, new Comparator<Pair>()
// {
// @Override
// public int compare(Pair o1,Pair o2)
// {
// if(o1.x>o2.x) return 1;
// else if(o1.x==o2.x)
// {
// if(o1.y>o2.y) return 1;
// else return -1;
// }
// else return -1;
// }});
static void print(int a[])
{
int n=a.length;
for(int i=0;i<n;i++)
{
out.print(a[i]+" ");
}
out.println();
out.flush();
}
static void print(long a[])
{
int n=a.length;
for(int i=0;i<n;i++)
{
out.print(a[i]+" ");
}
out.println();
out.flush();
}
static void print_int(ArrayList<Integer> al)
{
int si=al.size();
for(int i=0;i<si;i++)
{
out.print(al.get(i)+" ");
}
out.println();
out.flush();
}
static void print_long(ArrayList<Long> al)
{
int si=al.size();
for(int i=0;i<si;i++)
{
out.print(al.get(i)+" ");
}
out.println();
out.flush();
}
static void pn(int x)
{
out.println(x);
out.flush();
}
static void pn(long x)
{
out.println(x);
out.flush();
}
static void pn(String x)
{
out.println(x);
out.flush();
}
static int LowerBound(int a[], int x)
{
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(int a[], int x)
{// x is the key or target value
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
static void DFS(ArrayList<Integer> graph[],boolean[] visited, int u)
{
visited[u]=true;
int v=0;
for(int i=0;i<graph[u].size();i++)
{
v=graph[u].get(i);
if(!visited[v])
DFS(graph,visited,v);
}
}
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 nCr(long a,long b,long mod)
{
return (((fact[(int)a] * modInverse(fact[(int)b],mod))%mod * modInverse(fact[(int)(a - b)],mod))%mod + mod)%mod;
}
static long fact[]=new long[max_num];
static void fact_fill()
{
fact[0]=1l;
for(int i=1;i<max_num;i++)
{
fact[i]=(fact[i-1]*(long)i);
if(fact[i]>=mod)
fact[i]%=mod;
}
}
static long modInverse(long a, long m)
{
return power(a, m - 2, m);
}
static long power(long x, long y, long m)
{
if (y == 0)
return 1;
long p = power(x, y / 2, m) % m;
p = (long)((p * (long)p) % m);
if (y % 2 == 0)
return p;
else
return (long)((x * (long)p) % m);
}
static long sum_array(int a[])
{
int n=a.length;
long sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum;
}
static long sum_array(long a[])
{
int n=a.length;
long sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum;
}
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 void reverse_array(int a[])
{
int n=a.length;
int i,t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
static void reverse_array(long a[])
{
int n=a.length;
int i; long t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static class FastReader{
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan());
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
static PrintWriter out=new PrintWriter(System.out);
static int int_max=Integer.MAX_VALUE;
static int int_min=Integer.MIN_VALUE;
static long long_max=Long.MAX_VALUE;
static long long_min=Long.MIN_VALUE;
}
// Thank You ! | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 67a21e8def1ab1384abb77e22b7de346 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.*;
import java.lang.*;
public class matrix{
public static void main(String ags[]){
Scanner sc=new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0){
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
for (int i = 0; i < n; i++)
a[i]=sc.nextInt();
if (k > n) {
for (int i = 0; i < n; i++)
a[i] += k - n;
k = n;
}
long ans = 0;
long sum = 0;
for (int i = 0; i < k; i++)
sum += a[i];
ans = Math.max(ans, sum);
for (int i = k; i < n; i++) {
sum += a[i] - a[i - k];
ans = Math.max(ans, sum);
}
ans += (long)k * (k - 1) / 2;
System.out.println(ans);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 1939fc1724cd5c934384094c1656b780 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | f9643ad03239058efaf1d6083447e40f | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | bdd8dd92aebe5157d5efa7724285ee68 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | c309bb9ccc5cb419c1c7fc26d3c90c03 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Q796A {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | d37d93dc190a973877b6dd2bc011dc6f | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Q1687A {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 2cb21525f749add0850648ece6a3e351 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.*;
import java.io.*;
public class enchantedforest {
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)));
StringTokenizer st = new StringTokenizer(br.readLine());
int t = Integer.parseInt(st.nextToken());
for (int tc = 0; tc < t; tc++) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
long[] s = new long[n];
for (int i = 0; i < n; i++) {
s[i] = Integer.parseInt(st.nextToken());
if (i > 0) {
s[i] += s[i - 1];
}
}
if (k >= n) {
pw.println(s[n - 1] + ((long) k - 1 + (long) k - (long) n) * n / 2);
} else {
long max = s[k - 1];
for (int i = k; i < n; i++) {
max = Math.max(max, s[i] - s[i - k]);
}
pw.println(max + (long) k * ((long) k - 1) / 2);
}
}
pw.close();
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 2d157180f094aeedaa687fa598fd8a95 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
public class Scratch {
public static void main(String[] args) throws IOException {
Reader sc=new Reader();
int p=sc.nextInt();
for (int q=0;q<p;q++)
{
int n=sc.nextInt();
int k=sc.nextInt();
int[] arr=new int[n];
for (int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
long[] pre=new long[n+1];
long sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];
pre[i+1]=sum;
}
// System.out.println(Arrays.toString(pre));
if (k<=n) {
long max=0;
for (int i = k; i <=n; i++)
max=Math.max(max,pre[i]-pre[i-k]);
System.out.println(max+((long) k *(k-1))/2);
}
else{
int extra_moves=(k-n);
long ex = ((long) k * (k - 1)) / 2 - ((long) (k - (n - 2)) *(k-(n-2)-1))/2+1+ 2L *extra_moves;
System.out.println((ex+pre[n]));
}
}
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
// Function to return gcd of a and b
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | da099ca8e32a31d83253524b33ed98c0 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main
{
public static void main(String args[]) throws Exception
{
Scanner in=new Scanner(System.in);
int t=in.nextInt();
//int t=1;
while(t-->0)
{
int n=in.nextInt();
int k=in.nextInt();
long[] arr=new long[n];
long sum=0;
for(int i=0;i<n;i++)
{
arr[i]=in.nextLong();
sum+=arr[i];
}
if(k<=n)
{
long tp=0,max=0;
for(int i=0;i<k;++i)
{
tp+=arr[i];
}
max=tp;
for(int i=k;i<n;i++)
{
tp-=arr[i-k];
tp+=arr[i];
max=Math.max(max,tp);
}
max+=((long)k*(k-1))/2;
System.out.println(max);
}
else
{
sum+=(long)n*k-(long)n*(n+1)/2;
System.out.println(sum);
}
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 30f30104a0b3e307f5d528ca86418182 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | /*
Rating: 1378
Date: 05-09-2022
Time: 19-52-20
Author: Kartik Papney
Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/
Leetcode: https://leetcode.com/kartikpapney/
Codechef: https://www.codechef.com/users/kartikpapney
----------------------------Jai Shree Ram----------------------------
*/
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class A_The_Enchanted_Forest {
public static long window(long[] arr, int k) {
long ans = 0;
long max = 0;
for(int i=0; i<arr.length; i++) {
ans += arr[i];
if(i >= k) ans -= arr[i-k];
max = Math.max(max, ans);
}
return max;
}
public static void s() {
long n = sc.nextInt();
long k = sc.nextLong();
long[] arr = sc.readLongArray((int)n);
if(k < n) {
long ans = window(arr, (int)k);
ans += (k*(k-1))/2;
p.writeln(ans);
} else {
long ans = Functions.sum(arr) + n*k - ((n*(n+1))>>1);
p.writeln(ans);
}
}
public static void main(String[] args) {
int t = 1;
t = sc.nextInt();
while (t-- != 0) {
s();
}
p.print();
}
public static boolean debug = false;
static void debug(String st) {
if(debug) p.writeln(st);
}
static final Integer MOD = (int) 1e9 + 7;
static final FastReader sc = new FastReader();
static final Print p = new Print();
static class Functions {
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 max(int... a) {
int max = Integer.MIN_VALUE;
for (int val : a) max = Math.max(val, max);
return max;
}
static int min(int... a) {
int min = Integer.MAX_VALUE;
for (int val : a) min = Math.min(val, min);
return min;
}
static long min(long... a) {
long min = Long.MAX_VALUE;
for (long val : a) min = Math.min(val, min);
return min;
}
static long max(long... a) {
long max = Long.MIN_VALUE;
for (long val : a) max = Math.max(val, max);
return max;
}
static long sum(long... a) {
long sum = 0;
for (long val : a) sum += val;
return sum;
}
static int sum(int... a) {
int sum = 0;
for (int val : a) sum += val;
return sum;
}
public static long mod_add(long a, long b) {
return (a % MOD + b % MOD + MOD) % MOD;
}
public static long pow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1) != 0)
res = mod_mul(res, a);
a = mod_mul(a, a);
b >>= 1;
}
return res;
}
public static long mod_mul(long a, long b) {
long res = 0;
a %= MOD;
while (b > 0) {
if ((b & 1) > 0) {
res = mod_add(res, a);
}
a = (2 * a) % MOD;
b >>= 1;
}
return res;
}
public static long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
public static long factorial(long n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res = (i % MOD * res % MOD) % MOD;
}
return res;
}
public static int count(int[] arr, int x) {
int count = 0;
for (int val : arr) if (val == x) count++;
return count;
}
public static ArrayList<Integer> generatePrimes(int n) {
boolean[] primes = new boolean[n];
for (int i = 2; i < primes.length; i++) primes[i] = true;
for (int i = 2; i < primes.length; i++) {
if (primes[i]) {
for (int j = i * i; j < primes.length; j += i) {
primes[j] = false;
}
}
}
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < primes.length; i++) {
if (primes[i]) arr.add(i);
}
return arr;
}
}
static class Print {
StringBuffer strb = new StringBuffer();
public void write(Object str) {
strb.append(str);
}
public void writes(Object str) {
char c = ' ';
strb.append(str).append(c);
}
public void writeln(Object str) {
char c = '\n';
strb.append(str).append(c);
}
public void writeln() {
char c = '\n';
strb.append(c);
}
public void yes() {
char c = '\n';
writeln("YES");
}
public void no() {
writeln("NO");
}
public void writes(int... arr) {
for (int val : arr) {
write(val);
write(' ');
}
}
public void writes(long... arr) {
for (long val : arr) {
write(val);
write(' ');
}
}
public void writeln(int... arr) {
for (int val : arr) {
writeln(val);
}
}
public void print() {
System.out.print(strb);
}
public void println() {
System.out.println(strb);
}
}
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;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
double[] readArrayDouble(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
String nextLine() {
String str = new String();
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 9d90d457debc7d40d18a53df05718967 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | dd0ee0f565af746daa9e8fa8fdb848d9 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.*;
import java.lang.*;
public class matrix{
public static void main(String ags[]){
Scanner sc=new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0){
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
for (int i = 0; i < n; i++)
a[i]=sc.nextInt();
if (k > n) {
for (int i = 0; i < n; i++)
a[i] += k - n;
k = n;
}
long ans = 0;
long sum = 0;
for (int i = 0; i < k; i++)
sum += a[i];
ans = Math.max(ans, sum);
for (int i = k; i < n; i++) {
sum += a[i] - a[i - k];
ans = Math.max(ans, sum);
}
ans += (long)k * (k - 1) / 2;
System.out.println(ans);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | d6e321de81d3501ad84c54ddd23b6f2e | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 0d68c03aae2b91e98cc579d0b8d08e71 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 5af9458366e4dee8ab5cf5b358f299be | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 9392a14d5c662c080e32db0a18d27e99 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.awt.image.AreaAveragingScaleFilter;
import java.io.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
public class Pratice {
static final long mod = 1000000007;
static StringBuilder sb = new StringBuilder();
static int xn = (int) (2e5 + 10);
static long ans;
static boolean prime[] = new boolean[1000001];
// calculate sqrt and cuberoot
// static Set<Long> set=new TreeSet<>();
// static
// {
// long n=1000000001;
//
//
// for(int i=1;i*i<=n;i++)
// {
// long x=i*i;
// set.add(x);
// }
// for(int i=1;i*i*i<=n;i++)
// {
// long x=i*i*i;
// set.add(x);
// }
// }
static void sieveOfEratosthenes() {
for (int i = 0; i <= 1000000; i++)
prime[i] = true;
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= 1000000; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= 1000000; i += p)
prime[i] = false;
}
}
}
public static void main(String[] args) throws IOException {
Reader reader = new Reader();
int t = reader.nextInt();
while (t-- > 0) {
long n = reader.nextLong();
long k = reader.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = reader.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
// }
// static void SieveOfEratosthenes(int n, boolean prime[],
// boolean primesquare[], int a[])
// {
// // Create a boolean array "prime[0..n]" and
// // initialize all entries it as true. A value
// // in prime[i] will finally be false if i is
// // Not a prime, else true.
// for (int i = 2; i <= n; i++)
// prime[i] = true;
//
// /* Create a boolean array "primesquare[0..n*n+1]"
// and initialize all entries it as false.
// A value in squareprime[i] will finally
// be true if i is square of prime,
// else false.*/
// for (int i = 0; i < ((n * n) + 1); i++)
// primesquare[i] = false;
//
// // 1 is not a prime number
// prime[1] = false;
//
// for (int p = 2; p * p <= n; p++) {
// // If prime[p] is not changed,
// // then it is a prime
// if (prime[p] == true) {
// // Update all multiples of p
// for (int i = p * 2; i <= n; i += p)
// prime[i] = false;
// }
// }
//
// int j = 0;
// for (int p = 2; p <= n; p++) {
// if (prime[p]) {
//
// a[j] = p;
//
//
// primesquare[p * p] = true;
// j++;
// }
// }
// }
//
//
// static int countDivisors(int n)
// {
//
// if (n == 1)
// return 1;
//
// boolean prime[] = new boolean[n + 1];
// boolean primesquare[] = new boolean[(n * n) + 1];
//
// int a[] = new int[n];
//
//
// SieveOfEratosthenes(n, prime, primesquare, a);
//
//
// int ans = 1;
//
// // Loop for counting factors of n
// for (int i = 0;; i++) {
// // a[i] is not less than cube root n
// if (a[i] * a[i] * a[i] > n)
// break;
//
// int cnt = 1;
//
// // if a[i] is a factor of n
// while (n % a[i] == 0) {
// n = n / a[i];
//
// // incrementing power
// cnt = cnt + 1;
// }
//
//
// ans = ans * cnt;
// }
//
//
// if (prime[n])
// ans = ans * 2;
//
// // Second case
// else if (primesquare[n])
// ans = ans * 3;
//
// // Third case
// else if (n != 1)
// ans = ans * 4;
//
// return ans; // Total divisors
// }
public static long[] inarr(long n) throws IOException {
Reader reader = new Reader();
long arr[]=new long[(int) n];
for (long i = 0; i < n; i++) {
arr[(int) i]=reader.nextLong();
}
return arr;
}
public static boolean checkPerfectSquare(int number)
{
int x=number % 10;
if (x==2 || x==3 || x==7 || x==8)
{
return false;
}
for (int i=0; i<=number/2 + 1; i++)
{
if (i*i==number)
{
return true;
}
}
return false;
}
// check number is prime or not
public static boolean isPrime(int n) {
return BigInteger.valueOf(n).isProbablePrime(1);
}
// return the gcd of two numbers
public static long gcd(long a, long b)
{
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.longValue();
}
// return lcm of number
static long lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
// number of digits in the given number
public static long numberOfDigits(long n)
{
long ans= (long) (Math.floor((Math.log10(n)))+1);
return ans;
}
// return most significant bit in the number
public static long mostSignificantNumber(long n)
{
double k=Math.log10(n);
k=k-Math.floor(k);
int ans=(int)Math.pow(10,k);
return ans;
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | c13dcb22f7be019882164e6a20f6f61c | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 1b86b53d65a8285cc8cf91f6de35fde2 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.awt.image.AreaAveragingScaleFilter;
import java.io.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
public class Pratice {
static final long mod = 1000000007;
static StringBuilder sb = new StringBuilder();
static int xn = (int) (2e5 + 10);
static long ans;
static boolean prime[] = new boolean[1000001];
// calculate sqrt and cuberoot
// static Set<Long> set=new TreeSet<>();
// static
// {
// long n=1000000001;
//
//
// for(int i=1;i*i<=n;i++)
// {
// long x=i*i;
// set.add(x);
// }
// for(int i=1;i*i*i<=n;i++)
// {
// long x=i*i*i;
// set.add(x);
// }
// }
static void sieveOfEratosthenes() {
for (int i = 0; i <= 1000000; i++)
prime[i] = true;
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= 1000000; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= 1000000; i += p)
prime[i] = false;
}
}
}
public static void main(String[] args) throws IOException {
Reader reader = new Reader();
int t = reader.nextInt();
while (t-- > 0) {
long n = reader.nextLong();
long k = reader.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = reader.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
// }
// static void SieveOfEratosthenes(int n, boolean prime[],
// boolean primesquare[], int a[])
// {
// // Create a boolean array "prime[0..n]" and
// // initialize all entries it as true. A value
// // in prime[i] will finally be false if i is
// // Not a prime, else true.
// for (int i = 2; i <= n; i++)
// prime[i] = true;
//
// /* Create a boolean array "primesquare[0..n*n+1]"
// and initialize all entries it as false.
// A value in squareprime[i] will finally
// be true if i is square of prime,
// else false.*/
// for (int i = 0; i < ((n * n) + 1); i++)
// primesquare[i] = false;
//
// // 1 is not a prime number
// prime[1] = false;
//
// for (int p = 2; p * p <= n; p++) {
// // If prime[p] is not changed,
// // then it is a prime
// if (prime[p] == true) {
// // Update all multiples of p
// for (int i = p * 2; i <= n; i += p)
// prime[i] = false;
// }
// }
//
// int j = 0;
// for (int p = 2; p <= n; p++) {
// if (prime[p]) {
//
// a[j] = p;
//
//
// primesquare[p * p] = true;
// j++;
// }
// }
// }
//
//
// static int countDivisors(int n)
// {
//
// if (n == 1)
// return 1;
//
// boolean prime[] = new boolean[n + 1];
// boolean primesquare[] = new boolean[(n * n) + 1];
//
// int a[] = new int[n];
//
//
// SieveOfEratosthenes(n, prime, primesquare, a);
//
//
// int ans = 1;
//
// // Loop for counting factors of n
// for (int i = 0;; i++) {
// // a[i] is not less than cube root n
// if (a[i] * a[i] * a[i] > n)
// break;
//
// int cnt = 1;
//
// // if a[i] is a factor of n
// while (n % a[i] == 0) {
// n = n / a[i];
//
// // incrementing power
// cnt = cnt + 1;
// }
//
//
// ans = ans * cnt;
// }
//
//
// if (prime[n])
// ans = ans * 2;
//
// // Second case
// else if (primesquare[n])
// ans = ans * 3;
//
// // Third case
// else if (n != 1)
// ans = ans * 4;
//
// return ans; // Total divisors
// }
public static long[] inarr(long n) throws IOException {
Reader reader = new Reader();
long arr[]=new long[(int) n];
for (long i = 0; i < n; i++) {
arr[(int) i]=reader.nextLong();
}
return arr;
}
public static boolean checkPerfectSquare(int number)
{
int x=number % 10;
if (x==2 || x==3 || x==7 || x==8)
{
return false;
}
for (int i=0; i<=number/2 + 1; i++)
{
if (i*i==number)
{
return true;
}
}
return false;
}
// check number is prime or not
public static boolean isPrime(int n) {
return BigInteger.valueOf(n).isProbablePrime(1);
}
// return the gcd of two numbers
public static long gcd(long a, long b)
{
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.longValue();
}
// return lcm of number
static long lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
// number of digits in the given number
public static long numberOfDigits(long n)
{
long ans= (long) (Math.floor((Math.log10(n)))+1);
return ans;
}
// return most significant bit in the number
public static long mostSignificantNumber(long n)
{
double k=Math.log10(n);
k=k-Math.floor(k);
int ans=(int)Math.pow(10,k);
return ans;
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | f378805d061fbbf280ac87ac16dddf7a | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | d52816305e841c2c9a34d81ad1a995ff | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 01a46164822c22fce9a63139e04f4322 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.Scanner;
public class Forest {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long n = s.nextLong();
long k = s.nextLong();
long sum = 0, maxsum = 0;
int num[] = new int[(int) n];
for(int i=1; i<=n; i++)
{
num[i-1] = s.nextInt();
if(i<=k) sum += num[i-1];
else sum += num[i-1]-num[(int) (i-k-1)];
if(sum>maxsum) maxsum = sum;
}
if(k>=n) System.out.println(maxsum+n*k-n*(n+1)/2);
else System.out.println(maxsum+k*(k-1)/2);
}
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 22fc7b751fead366d98c2ad4934cb8f1 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 38697ba4527eb373d7cd72c6a4465417 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class Main {
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 tc = Integer.parseInt(br.readLine());
while (tc > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long ans = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long wst = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= wst;
ans = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
ans = max;
}
bw.write(ans + "\n");
tc--;
}
br.close();
bw.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 91c636a3d92fcd1c897ee2486fa32a44 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class Main {
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 tc = Integer.parseInt(br.readLine());
while (tc > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long ans = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long wst = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= wst;
ans = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
ans = max;
}
bw.write(ans + "\n");
tc--;
}
br.close();
bw.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | f6eb1e9912ffd2eb46da72ae0fa6171b | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 6025f9742f6936cc519701633ba44253 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 347e56ee8e2643e580fcaf4f515624e2 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | d5e64156de6a357b1965c0d4d84a862e | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 6ecb927901559055d99019baa43773d4 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | ecb9a28f2d4756a47a42a166fb12736f | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 28b0aa0a851610a8d1539061886428dd | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 266d5b57c23a3be7842dc32b50ef5212 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.*;
import java.io.*;
public class CF {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int q = Integer.parseInt(st.nextToken());
while(q-->0) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
long[] arr = new long[n];
st = new StringTokenizer(br.readLine());
long sum = 0;
for (int i=0; i<n; i++) {
arr[i] = Long.parseLong(st.nextToken());
arr[i]+=(k-1);
sum+=arr[i];
}
long ans = 0;
if (k>=n) {
ans = sum-tri(n-1);
}
else {
sum = 0;
long biggest_sum = 0;
for (int i=0; i<k; i++) {
sum+=arr[i];
}
biggest_sum = sum;
for (int i=k; i<n; i++) {
sum-=arr[i-k];
sum+=arr[i];
biggest_sum = Math.max(biggest_sum, sum);
}
ans = biggest_sum-tri(k-1);
}
System.out.println(ans);
}
}
static long tri(long n) {
return n*(n+1)/2;
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 3a55cf78b8215787260e3836cd00fdd4 | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.*;
public class TheEnchantedForest {
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 t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
long k = Integer.parseInt(input[1]);
long[] arr = new long[n];
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
long answer = 0;
if (k > n) {
long sum = 0;
for (long l : arr) {
sum += l;
}
long waste = n * (long) (n - 1) / 2;
sum += (k - 1) * n;
sum -= waste;
answer = sum;
} else {
long max = 0;
long sum = 0;
int l = 0;
int r = 0;
for (; r < k; r++) {
sum += arr[r];
}
max = sum;
for (; r < n; l++, r++) {
sum -= arr[l];
sum += arr[r];
max = Math.max(max, sum);
}
max += (k * (k - 1)) / 2;
answer = max;
}
bw.write(answer + "\n");
}
br.close();
bw.close();
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | f97764b1718f849e5373b4139a7ddc0f | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long mod = (int)1e9+7;
static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
// int t=1;
O : while(t-->0)
{
int n=sc.nextInt();
int k=sc.nextInt();
long k1=(long)k;
int a[]=sc.readArray(n);
long pre[]=new long[n+1];
for(int i=0;i<n;i++){
pre[i+1]=pre[i]+a[i];
}
long to_add=(long)(k1*(k1-1)/2);
if(k>n){
to_add-=(long)(k1-n)*(long)(k1-n-1)/2*1l;
k=n;
}
long ans=0;
for(int i=0;i+k<=n;i++){
ans=Math.max(ans,pre[i+k]-pre[i]);
}
// System.out.println(ans+ " "+ to_add);
System.out.println(ans+to_add);
}
out.flush();
}
static void printN()
{
System.out.println("NO");
}
static void printY()
{
System.out.println("YES");
}
static int findfrequencies(int a[],int n)
{
int count=0;
for(int i=0;i<a.length;i++)
{
if(a[i]==n)
{
count++;
}
}
return count;
}
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());
}
float nextFloat()
{
return Float.parseFloat(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;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static int[] EvenOddArragement(int nums[])
{
int i1=0,i2=nums.length-1;
while(i1<i2){
while(nums[i1]%2==0 && i1<i2){
i1++;
}
while(nums[i2]%2!=0 && i2>i1){
i2--;
}
int temp=nums[i1];
nums[i1]=nums[i2];
nums[i2]=temp;
}
return nums;
}
static int gcd(int a, int b) {
while (b != 0) {
int t = a;
a = b;
b = t % b;
}
return a;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static int DigitSum(int n)
{
int r=0,sum=0;
while(n>=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
return sum;
}
static boolean checkPerfectSquare(int number)
{
double sqrt=Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static boolean isPowerOfTwo(int n)
{
if(n==0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static boolean isPrime2(int n)
{
if (n <= 1)
{
return false;
}
if (n == 2)
{
return true;
}
if (n % 2 == 0)
{
return false;
}
for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
static String minLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for(int i=0;i<n;i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[0];
}
static String maxLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for (int i = 0; i < n; i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[arr.length-1];
}
static class P implements Comparable<P> {
int i, j;
public P(int i, int j) {
this.i=i;
this.j=j;
}
public int compareTo(P o) {
return Integer.compare(i, o.i);
}
}
static class pair{
int i,j;
pair(int x,int y){
i=x;
j=y;
}
}
static int binary_search(int a[],int value)
{
int start=0;
int end=a.length-1;
int mid=start+(end-start)/2;
while(start<=end)
{
if(a[mid]==value)
{
return mid;
}
if(a[mid]>value)
{
end=mid-1;
}
else
{
start=mid+1;
}
mid=start+(end-start)/2;
}
return -1;
}
} | Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | 438ca12b669ef9a40ae6d3aa741ddd2c | train_108.jsonl | 1654266900 | The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* 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);
OutputWriter out = new OutputWriter(outputStream);
ATheEnchantedForest solver = new ATheEnchantedForest();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class ATheEnchantedForest {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt(), k = in.nextInt();
int[] a = in.readIntArray(n);
long[] P = new long[n + 1];
long ans = 0;
for (int i = 1; i <= n; i++) P[i] = P[i - 1] + a[i - 1];
int l = Integer.min(k, n);
for (int i = l; i <= n; i++) {
long s = P[i] - P[i - l];
ans = Long.max(ans, s + k * 1L * l - (l * 1L * (l + 1)) / 2);
}
out.println(ans);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = readInt();
}
return array;
}
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 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 String next() {
int c;
while (isSpaceChar(c = this.read())) {
;
}
StringBuilder result = new StringBuilder();
result.appendCodePoint(c);
while (!isSpaceChar(c = this.read())) {
result.appendCodePoint(c);
}
return result.toString();
}
public int readInt() {
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) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void println(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.print('\n');
}
public void close() {
writer.close();
}
}
}
| Java | ["4\n\n5 2\n\n5 6 1 2 3\n\n5 7\n\n5 6 1 2 3\n\n1 2\n\n999999\n\n5 70000\n\n1000000000 1000000000 1000000000 1000000000 1000000000"] | 2 seconds | ["12\n37\n1000000\n5000349985"] | NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms. | Java 11 | standard input | [
"brute force",
"greedy"
] | e092d58ac58e1e41d17be946128234e5 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 1,600 | For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. | standard output | |
PASSED | c6d2e53fa02781c13249c28dea33fab8 | train_108.jsonl | 1654266900 | Ran is especially skilled in computation and mathematics. It is said that she can do unimaginable calculation work in an instant.—Perfect Memento in Strict SenseRan Yakumo is a cute girl who loves creating cute Maths problems.Let $$$f(x)$$$ be the minimal square number strictly greater than $$$x$$$, and $$$g(x)$$$ be the maximal square number less than or equal to $$$x$$$. For example, $$$f(1)=f(2)=g(4)=g(8)=4$$$.A positive integer $$$x$$$ is cute if $$$x-g(x)<f(x)-x$$$. For example, $$$1,5,11$$$ are cute integers, while $$$3,8,15$$$ are not. Ran gives you an array $$$a$$$ of length $$$n$$$. She wants you to find the smallest non-negative integer $$$k$$$ such that $$$a_i + k$$$ is a cute number for any element of $$$a$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* 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);
OutputWriter out = new OutputWriter(outputStream);
DCuteNumber solver = new DCuteNumber();
solver.solve(1, in, out);
out.close();
}
static class DCuteNumber {
public static int[] createSets(int size) {
int[] p = new int[size];
for (int i = 0; i < size; i++) p[i] = i;
return p;
}
public static int root(int[] p, int x) {
return x == p[x] ? x : (p[x] = root(p, p[x]));
}
public static boolean unite(int[] p, int a, int b) {
a = root(p, a);
b = root(p, b);
p[a] = b;
return a != b;
}
long getFloor(long x) {
long v = (long) Math.sqrt(x);
while (v * v < x) v++;
while (v * v > x) v--;
return v;
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int[] a = in.readIntArray(n);
int[] sets = createSets(n);
for (int x = 1; x <= 2000000; x++) {
long u = x;
int i = 0;
long L = 0, R = 1L << 50;
while (i < n) {
long v = u * u;
long w = v + u;
long _w = w;
L = Long.max(L, v - a[i]);
int j = root(sets, i);
R = Long.min(R, w - a[j]);
if (L > R || j + 1 == n) break;
int delta = a[j + 1] - a[j];
if (delta <= u)
unite(sets, j, j + 1);
u = getFloor(v + delta);
v = u * u;
w = v + u;
long _L = Long.max(L, v - a[j + 1]);
long _R = Long.min(R, w - a[j + 1]);
if (_L > _R) u = getFloor(_w + delta);
i = j + 1;
}
if (L <= R) {
out.println(L);
return;
}
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = readInt();
}
return array;
}
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 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 int readInt() {
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) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void println(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.print('\n');
}
public void close() {
writer.close();
}
}
}
| Java | ["4\n1 3 8 10", "5\n2 3 8 9 11", "8\n1 2 3 4 5 6 7 8"] | 2 seconds | ["1", "8", "48"] | NoteTest case 1:$$$3$$$ is not cute integer, so $$$k\ne 0$$$.$$$2,4,9,11$$$ are cute integers, so $$$k=1$$$. | Java 11 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"implementation",
"math"
] | 6f31e2bc222314236d35c9642a60812e | The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^6$$$) — the length of $$$a$$$. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_1 \leq a_2 \leq \ldots \leq a_n \leq 2\cdot 10^6$$$) — the array $$$a$$$. | 2,900 | Print a single interger $$$k$$$ — the answer. | standard output | |
PASSED | 1f1e8fdcd8a911ebc9f0c2c57a2c34df | train_108.jsonl | 1654266900 | Ran is especially skilled in computation and mathematics. It is said that she can do unimaginable calculation work in an instant.—Perfect Memento in Strict SenseRan Yakumo is a cute girl who loves creating cute Maths problems.Let $$$f(x)$$$ be the minimal square number strictly greater than $$$x$$$, and $$$g(x)$$$ be the maximal square number less than or equal to $$$x$$$. For example, $$$f(1)=f(2)=g(4)=g(8)=4$$$.A positive integer $$$x$$$ is cute if $$$x-g(x)<f(x)-x$$$. For example, $$$1,5,11$$$ are cute integers, while $$$3,8,15$$$ are not. Ran gives you an array $$$a$$$ of length $$$n$$$. She wants you to find the smallest non-negative integer $$$k$$$ such that $$$a_i + k$$$ is a cute number for any element of $$$a$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* 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);
OutputWriter out = new OutputWriter(outputStream);
DCuteNumber solver = new DCuteNumber();
solver.solve(1, in, out);
out.close();
}
static class DCuteNumber {
public static int[] createSets(int size) {
int[] p = new int[size];
for (int i = 0; i < size; i++) p[i] = i;
return p;
}
public static int root(int[] p, int x) {
return x == p[x] ? x : (p[x] = root(p, p[x]));
}
public static boolean unite(int[] p, int a, int b) {
a = root(p, a);
b = root(p, b);
p[a] = b;
return a != b;
}
long getFloor(long x) {
long v = (long) Math.sqrt(x);
while (v * v < x) v++;
while (v * v > x) v--;
return v;
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int[] a = in.readIntArray(n);
int[] sets = createSets(n);
for (int x = 1; x <= 2000000; x++) {
long u = x;
int i = 0;
long L = 0, R = 1L << 50;
while (i < n) {
long v = u * u;
long w = v + u;
long _w = w;
L = Long.max(L, v - a[i]);
int j = root(sets, i);
R = Long.min(R, w - a[j]);
if (L > R || j + 1 == n) break;
int delta = a[j + 1] - a[j];
if (delta <= u)
unite(sets, j, j + 1);
u = getFloor(v + delta);
v = u * u;
w = v + u;
long _L = Long.max(L, v - a[j + 1]);
long _R = Long.min(R, w - a[j + 1]);
if (_L > _R) u = getFloor(_w + delta);
i = j + 1;
}
if (L <= R) {
out.println(L);
return;
}
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = readInt();
}
return array;
}
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 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 int readInt() {
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) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void println(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.print('\n');
}
public void close() {
writer.close();
}
}
}
| Java | ["4\n1 3 8 10", "5\n2 3 8 9 11", "8\n1 2 3 4 5 6 7 8"] | 2 seconds | ["1", "8", "48"] | NoteTest case 1:$$$3$$$ is not cute integer, so $$$k\ne 0$$$.$$$2,4,9,11$$$ are cute integers, so $$$k=1$$$. | Java 11 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"implementation",
"math"
] | 6f31e2bc222314236d35c9642a60812e | The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^6$$$) — the length of $$$a$$$. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_1 \leq a_2 \leq \ldots \leq a_n \leq 2\cdot 10^6$$$) — the array $$$a$$$. | 2,900 | Print a single interger $$$k$$$ — the answer. | standard output | |
PASSED | 3dec61d33bf4b7d1a0868427d510c658 | train_108.jsonl | 1654266900 | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations. | 256 megabytes | import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;
// Please name your class Main
public class Main {
static FastScanner fs=new FastScanner();
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int Int() {
return Integer.parseInt(next());
}
long Long() {
return Long.parseLong(next());
}
String Str(){
return next();
}
}
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//reading /writing file
//Scanner sc=new Scanner(new File("src/text.txt"));
//PrintWriter pr=new PrintWriter("output.txt");
//File file = new File("src/text.txt");
int T=Int();
for(int t=0;t<T;t++){
Solution sol1=new Solution(out,fs);
sol1.solution();
}
out.flush();
}
public static int[] Arr(int n){
int A[]=new int[n];
for(int i=0;i<n;i++)A[i]=Int();
return A;
}
public static int Int(){
return fs.Int();
}
public static long Long(){
return fs.Long();
}
public static String Str(){
return fs.Str();
}
}
class Solution {
PrintWriter out;
int INF = 10000000;
int MOD = 998244353;
int mod = 1000000007;
Main.FastScanner fs;
public Solution(PrintWriter out,Main.FastScanner fs) {
this.out = out;
this.fs=fs;
}
public void solution() {
int n = fs.Int();
int m = fs.Int();
int a[] = new int[n + 1];
long s[] = new long[n + 1];
for(int i = 1; i <= n; i++) {
a[i] = fs.Int();
}
for(int i = 1; i <= n; i++) {
int x = fs.Int();
a[i] -= x;
s[i] = a[i] + s[i - 1];
}
TreeSet<Integer> tree = new TreeSet<>();
Queue<Integer> q = new LinkedList<>();
for(int i = 0; i <= n; i++) {
if(s[i] == 0) q.add(i);
else tree.add(i);
}
List<Integer> graph[] = new ArrayList[n + 1];
Arrays.setAll(graph, e->new ArrayList<>());
for(int i = 0; i < m; i++) {
int l = fs.Int();
int r = fs.Int();
int mn = Math.min(l, r), mx = Math.max(l, r);
graph[mn - 1].add(mx);
graph[mx].add(mn - 1);
}
while(q.size() > 0) {
int root = q.poll();
for(int nxt : graph[root]) {
if(s[nxt] != 0) continue;
int l = Math.min(root, nxt) - 1, r = Math.max(root, nxt);
while(tree.size() > 0) {
Integer h = tree.higher(l);
if(h == null || h > r) break;
q.add(h);
tree.remove(h);
s[h] = 0;
}
}
}
if(tree.size() == 0) {
out.println("YES");
} else {
out.println("NO");
}
}
} | Java | ["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"] | 1 second | ["YES\nNO"] | NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$. | Java 11 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"greedy",
"sortings"
] | 2627417136abced8481ea5728d5c1f06 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i < r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 2,500 | For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | edb7695c9fc75f1e122781e74db890a7 | train_108.jsonl | 1654266900 | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations. | 256 megabytes | import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;
// Please name your class Main
public class Main {
static FastScanner fs=new FastScanner();
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int Int() {
return Integer.parseInt(next());
}
long Long() {
return Long.parseLong(next());
}
String Str(){
return next();
}
}
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//reading /writing file
//Scanner sc=new Scanner(new File("src/text.txt"));
//PrintWriter pr=new PrintWriter("output.txt");
//File file = new File("src/text.txt");
int T=Int();
for(int t=0;t<T;t++){
Solution sol1=new Solution(out,fs);
sol1.solution();
}
out.flush();
}
public static int[] Arr(int n){
int A[]=new int[n];
for(int i=0;i<n;i++)A[i]=Int();
return A;
}
public static int Int(){
return fs.Int();
}
public static long Long(){
return fs.Long();
}
public static String Str(){
return fs.Str();
}
}
class Solution {
PrintWriter out;
int INF = 10000000;
int MOD = 998244353;
int mod = 1000000007;
Main.FastScanner fs;
public Solution(PrintWriter out,Main.FastScanner fs) {
this.out = out;
this.fs=fs;
}
public void solution() {
int n = fs.Int();
int m = fs.Int();
int a[] = new int[n + 1];
long s[] = new long[n + 1];
for(int i = 1; i <= n; i++) {
a[i] = fs.Int();
}
for(int i = 1; i <= n; i++) {
int x = fs.Int();
a[i] -= x;
s[i] = a[i] + s[i - 1];
}
TreeSet<Integer> tree = new TreeSet<>();
Queue<Integer> q = new LinkedList<>();
for(int i = 0; i <= n; i++) {
if(s[i] == 0) q.add(i);
else tree.add(i);
}
List<Integer> graph[] = new ArrayList[n + 1];
Arrays.setAll(graph, e->new ArrayList<>());
for(int i = 0; i < m; i++) {
int l = fs.Int();
int r = fs.Int();
int mn = Math.min(l, r), mx = Math.max(l, r);
graph[mn - 1].add(mx);
graph[mx].add(mn - 1);
}
while(q.size() > 0) {
int root = q.poll();
for(int nxt : graph[root]) {
if(s[nxt] != 0) continue;
int l = Math.min(root, nxt) - 1, r = Math.max(root, nxt);
while(tree.size() > 0) {
Integer h = tree.higher(l);
if(h == null || h > r) break;
q.add(h);
tree.remove(h);
s[h] = 0;
}
}
}
if(tree.size() == 0) {
out.println("YES");
} else {
out.println("NO");
}
}
} | Java | ["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"] | 1 second | ["YES\nNO"] | NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$. | Java 11 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"greedy",
"sortings"
] | 2627417136abced8481ea5728d5c1f06 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i < r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 2,500 | For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 5083619fef1c5561fbe6c50c4269ed4b | train_108.jsonl | 1654266900 | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations. | 256 megabytes |
import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;
// Please name your class Main
public class Main {
static FastScanner fs=new FastScanner();
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int Int() {
return Integer.parseInt(next());
}
long Long() {
return Long.parseLong(next());
}
String Str(){
return next();
}
}
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//reading /writing file
//Scanner sc=new Scanner(new File("src/text.txt"));
//PrintWriter pr=new PrintWriter("output.txt");
//File file = new File("src/text.txt");
int T=Int();
for(int t=0;t<T;t++){
Solution sol1=new Solution(out,fs);
sol1.solution();
}
out.flush();
}
public static int[] Arr(int n){
int A[]=new int[n];
for(int i=0;i<n;i++)A[i]=Int();
return A;
}
public static int Int(){
return fs.Int();
}
public static long Long(){
return fs.Long();
}
public static String Str(){
return fs.Str();
}
}
class Solution {
PrintWriter out;
int INF = 10000000;
int MOD = 998244353;
int mod = 1000000007;
Main.FastScanner fs;
public Solution(PrintWriter out,Main.FastScanner fs) {
this.out = out;
this.fs=fs;
}
public void solution() {
int n = fs.Int();
int m = fs.Int();
int a[] = new int[n + 1];
long s[] = new long[n + 1];
for(int i = 1; i <= n; i++) {
a[i] = fs.Int();
}
for(int i = 1; i <= n; i++) {
int x = fs.Int();
a[i] -= x;
s[i] = a[i] + s[i - 1];
}
TreeSet<Integer> tree = new TreeSet<>();
Queue<Integer> q = new LinkedList<>();
for(int i = 0; i <= n; i++) {
if(s[i] == 0) q.add(i);
else tree.add(i);
}
List<Integer> graph[] = new ArrayList[n + 1];
Arrays.setAll(graph, e->new ArrayList<>());
for(int i = 0; i < m; i++) {
int l = fs.Int();
int r = fs.Int();
int mn = Math.min(l, r), mx = Math.max(l, r);
graph[mn - 1].add(mx);
graph[mx].add(mn - 1);
}
while(q.size() > 0) {
int root = q.poll();
for(int nxt : graph[root]) {
if(s[nxt] != 0) continue;
int l = Math.min(root, nxt) - 1, r = Math.max(root, nxt);
while(tree.size() > 0) {
Integer h = tree.higher(l);
if(h == null || h > r) break;
q.add(h);
tree.remove(h);
s[h] = 0;
}
}
}
if(tree.size() == 0) {
out.println("YES");
} else {
out.println("NO");
}
}
} | Java | ["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"] | 1 second | ["YES\nNO"] | NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$. | Java 11 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"greedy",
"sortings"
] | 2627417136abced8481ea5728d5c1f06 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i < r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 2,500 | For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | b287d1b74761d6787b894b8e7e2fba0c | train_108.jsonl | 1654266900 | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class a{
public static FastScanner fs;
public static StringBuilder sb;
public static void main(String args[])
{
fs=new FastScanner();
sb=new StringBuilder();
int t=fs.nextInt();
while(t-- > 0)
{
solve();
}
System.out.println(sb);
}
public static void solve()
{
int n=fs.nextInt();
int m=fs.nextInt();
long a[]=new long[n+1];
long b[]=new long[n+1];
long pref[]=new long[n+1];
pref[0]=0l;
ArrayList<Integer>g[]=new ArrayList[n+1];
for(int i=0;i<=n;i++)
g[i]=new ArrayList<Integer>();
for(int i=0;i<n;i++)
a[i]=fs.nextLong();
for(int i=0;i<n;i++)
b[i]=fs.nextLong();
for(int i=0;i<n;i++)
pref[i+1]=pref[i]+a[i]-b[i];
for(int i=0;i<m;i++)
{
int x=fs.nextInt();
int y=fs.nextInt();
x--;
g[x].add(y);
g[y].add(x);
}
ArrayList<Integer>zeroprefix=new ArrayList<>();
TreeSet<Integer>set=new TreeSet<>();
for(int i=0;i<=n;i++)
{
if(pref[i]==0l)
zeroprefix.add(i);
else
set.add(i);
}
for(int i=0;i<zeroprefix.size();i++)
{
Integer it=zeroprefix.get(i);
for(int j=0;j<g[it].size();j++)
{
Integer it1=g[it].get(j);
if(pref[it1]!=0l)
continue;
int low=it;
int high=it1;
if(high<low)
{
int temp=low;
low=high;
high=temp;
}
Iterator<Integer> iterate=set.subSet(low,high+1).iterator();
while(iterate.hasNext())
{
Integer cur=iterate.next();
iterate.remove();
pref[cur]=0;
zeroprefix.add(cur);
}
}
}
if(zeroprefix.size()==(n+1))
sb.append("YES\n");
else
sb.append("NO\n");
}
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(Exception e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
}
} | Java | ["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"] | 1 second | ["YES\nNO"] | NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$. | Java 11 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"greedy",
"sortings"
] | 2627417136abced8481ea5728d5c1f06 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i < r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 2,500 | For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 155e2285439e90660e879a9fd46cc72d | train_108.jsonl | 1654266900 | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations. | 256 megabytes | import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;
// Please name your class Main
public class Main {
static FastScanner fs=new FastScanner();
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int Int() {
return Integer.parseInt(next());
}
long Long() {
return Long.parseLong(next());
}
String Str(){
return next();
}
}
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//reading /writing file
//Scanner sc=new Scanner(new File("src/text.txt"));
//PrintWriter pr=new PrintWriter("output.txt");
//File file = new File("src/text.txt");
int T=Int();
for(int t=0;t<T;t++){
Solution sol1=new Solution(out,fs);
sol1.solution();
}
out.flush();
}
public static int[] Arr(int n){
int A[]=new int[n];
for(int i=0;i<n;i++)A[i]=Int();
return A;
}
public static int Int(){
return fs.Int();
}
public static long Long(){
return fs.Long();
}
public static String Str(){
return fs.Str();
}
}
class Solution {
PrintWriter out;
int INF = 10000000;
int MOD = 998244353;
int mod = 1000000007;
Main.FastScanner fs;
public Solution(PrintWriter out,Main.FastScanner fs) {
this.out = out;
this.fs=fs;
}
public void solution() {
int n = fs.Int();
int m = fs.Int();
int a[] = new int[n + 1];
long s[] = new long[n + 1];
for(int i = 1; i <= n; i++) {
a[i] = fs.Int();
}
for(int i = 1; i <= n; i++) {
int x = fs.Int();
a[i] -= x;
s[i] = a[i] + s[i - 1];
}
TreeSet<Integer> tree = new TreeSet<>();
Queue<Integer> q = new LinkedList<>();
for(int i = 0; i <= n; i++) {
if(s[i] == 0) q.add(i);
else tree.add(i);
}
List<Integer> graph[] = new ArrayList[n + 1];
Arrays.setAll(graph, e->new ArrayList<>());
for(int i = 0; i < m; i++) {
int l = fs.Int();
int r = fs.Int();
int mn = Math.min(l, r), mx = Math.max(l, r);
graph[mn - 1].add(mx);
graph[mx].add(mn - 1);
}
while(q.size() > 0) {
int root = q.poll();
for(int nxt : graph[root]) {
if(s[nxt] != 0) continue;
int l = Math.min(root, nxt) - 1, r = Math.max(root, nxt);
while(tree.size() > 0) {
Integer h = tree.higher(l);
if(h == null || h > r) break;
q.add(h);
tree.remove(h);
s[h] = 0;
}
}
}
if(tree.size() == 0) {
out.println("YES");
} else {
out.println("NO");
}
}
} | Java | ["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"] | 1 second | ["YES\nNO"] | NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$. | Java 17 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"greedy",
"sortings"
] | 2627417136abced8481ea5728d5c1f06 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i < r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 2,500 | For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 62beec0f9042929bb4c1a1bb5d73e056 | train_108.jsonl | 1654266900 | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations. | 256 megabytes | import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;
// Please name your class Main
public class Main {
static FastScanner fs=new FastScanner();
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int Int() {
return Integer.parseInt(next());
}
long Long() {
return Long.parseLong(next());
}
String Str(){
return next();
}
}
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//reading /writing file
//Scanner sc=new Scanner(new File("src/text.txt"));
//PrintWriter pr=new PrintWriter("output.txt");
//File file = new File("src/text.txt");
int T=Int();
for(int t=0;t<T;t++){
Solution sol1=new Solution(out,fs);
sol1.solution();
}
out.flush();
}
public static int[] Arr(int n){
int A[]=new int[n];
for(int i=0;i<n;i++)A[i]=Int();
return A;
}
public static int Int(){
return fs.Int();
}
public static long Long(){
return fs.Long();
}
public static String Str(){
return fs.Str();
}
}
class Solution {
PrintWriter out;
int INF = 10000000;
int MOD = 998244353;
int mod = 1000000007;
Main.FastScanner fs;
public Solution(PrintWriter out,Main.FastScanner fs) {
this.out = out;
this.fs=fs;
}
public void solution() {
int n = fs.Int();
int m = fs.Int();
int a[] = new int[n + 1];
long s[] = new long[n + 1];
for(int i = 1; i <= n; i++) {
a[i] = fs.Int();
}
for(int i = 1; i <= n; i++) {
int x = fs.Int();
a[i] -= x;
s[i] = a[i] + s[i - 1];
}
TreeSet<Integer> tree = new TreeSet<>();
Queue<Integer> q = new LinkedList<>();
for(int i = 0; i <= n; i++) {
if(s[i] == 0) q.add(i);
else tree.add(i);
}
List<Integer> graph[] = new ArrayList[n + 1];
Arrays.setAll(graph, e->new ArrayList<>());
for(int i = 0; i < m; i++) {
int l = fs.Int();
int r = fs.Int();
int mn = Math.min(l, r), mx = Math.max(l, r);
graph[mn - 1].add(mx);
graph[mx].add(mn - 1);
}
while(q.size() > 0) {
int root = q.poll();
for(int nxt : graph[root]) {
if(s[nxt] != 0) continue;
int l = Math.min(root, nxt) - 1, r = Math.max(root, nxt);
while(tree.size() > 0) {
Integer h = tree.higher(l);
if(h == null || h > r) break;
q.add(h);
tree.remove(h);
s[h] = 0;
}
}
}
if(tree.size() == 0) {
out.println("YES");
} else {
out.println("NO");
}
}
} | Java | ["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"] | 1 second | ["YES\nNO"] | NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$. | Java 17 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"greedy",
"sortings"
] | 2627417136abced8481ea5728d5c1f06 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i < r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 2,500 | For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 6cbc84aa7ff32f5eaea305cce0633a91 | train_108.jsonl | 1654266900 | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations. | 256 megabytes | // Don't place your source in a package
import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;
// Please name your class Main
public class Main {
static FastScanner fs=new FastScanner();
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int Int() {
return Integer.parseInt(next());
}
long Long() {
return Long.parseLong(next());
}
String Str(){
return next();
}
}
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//reading /writing file
//Scanner sc=new Scanner(new File("src/text.txt"));
//PrintWriter pr=new PrintWriter("output.txt");
//File file = new File("src/text.txt");
int T=Int();
for(int t=0;t<T;t++){
Solution sol1=new Solution(out,fs);
sol1.solution();
}
out.flush();
}
public static int[] Arr(int n){
int A[]=new int[n];
for(int i=0;i<n;i++)A[i]=Int();
return A;
}
public static int Int(){
return fs.Int();
}
public static long Long(){
return fs.Long();
}
public static String Str(){
return fs.Str();
}
}
class Solution {
PrintWriter out;
int INF = 10000000;
int MOD = 998244353;
int mod = 1000000007;
Main.FastScanner fs;
public Solution(PrintWriter out,Main.FastScanner fs) {
this.out = out;
this.fs=fs;
}
public void solution() {
int n = fs.Int();
int m = fs.Int();
int a[] = new int[n + 1];
long s[] = new long[n + 1];
for(int i = 1; i <= n; i++) {
a[i] = fs.Int();
}
for(int i = 1; i <= n; i++) {
int x = fs.Int();
a[i] -= x;
s[i] = a[i] + s[i - 1];
}
TreeSet<Integer> tree = new TreeSet<>();
Queue<Integer> q = new LinkedList<>();
for(int i = 0; i <= n; i++) {
if(s[i] == 0) q.add(i);
else tree.add(i);
}
List<Integer> graph[] = new ArrayList[n + 1];
Arrays.setAll(graph, e->new ArrayList<>());
for(int i = 0; i < m; i++) {
int l = fs.Int();
int r = fs.Int();
int mn = Math.min(l, r), mx = Math.max(l, r);
graph[mn - 1].add(mx);
graph[mx].add(mn - 1);
}
while(q.size() > 0) {
int root = q.poll();
for(int nxt : graph[root]) {
if(s[nxt] != 0) continue;
int l = Math.min(root, nxt) - 1, r = Math.max(root, nxt);
while(tree.size() > 0) {
Integer h = tree.higher(l);
if(h == null || h > r) break;
q.add(h);
tree.remove(h);
s[h] = 0;
}
}
}
if(tree.size() == 0) {
out.println("YES");
} else {
out.println("NO");
}
}
} | Java | ["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"] | 1 second | ["YES\nNO"] | NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$. | Java 17 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"greedy",
"sortings"
] | 2627417136abced8481ea5728d5c1f06 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i < r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 2,500 | For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 12944f93acbb29c8e6784e95fd175f9a | train_108.jsonl | 1654266900 | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations. | 256 megabytes | // Don't place your source in a package
import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;
// Please name your class Main
public class Main {
static FastScanner fs=new FastScanner();
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int Int() {
return Integer.parseInt(next());
}
long Long() {
return Long.parseLong(next());
}
String Str(){
return next();
}
}
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//reading /writing file
//Scanner sc=new Scanner(new File("src/text.txt"));
//PrintWriter pr=new PrintWriter("output.txt");
//File file = new File("src/text.txt");
int T=Int();
for(int t=0;t<T;t++){
Solution sol1=new Solution(out,fs);
sol1.solution();
}
out.flush();
}
public static int[] Arr(int n){
int A[]=new int[n];
for(int i=0;i<n;i++)A[i]=Int();
return A;
}
public static int Int(){
return fs.Int();
}
public static long Long(){
return fs.Long();
}
public static String Str(){
return fs.Str();
}
}
class Solution {
PrintWriter out;
int INF = 10000000;
int MOD = 998244353;
int mod = 1000000007;
Main.FastScanner fs;
public Solution(PrintWriter out,Main.FastScanner fs) {
this.out = out;
this.fs=fs;
}
public void solution() {
int n = fs.Int();
int m = fs.Int();
int a[] = new int[n + 1];
long s[] = new long[n + 1];
for(int i = 1; i <= n; i++) {
a[i] = fs.Int();
}
for(int i = 1; i <= n; i++) {
int x = fs.Int();
a[i] -= x;
s[i] = a[i] + s[i - 1];
}
TreeSet<Integer> tree = new TreeSet<>();
Queue<Integer> q = new LinkedList<>();
for(int i = 0; i <= n; i++) {
if(s[i] == 0) q.add(i);
else tree.add(i);
}
List<Integer> graph[] = new ArrayList[n + 1];
Arrays.setAll(graph, e->new ArrayList<>());
for(int i = 0; i < m; i++) {
int l = fs.Int();
int r = fs.Int();
int mn = Math.min(l, r), mx = Math.max(l, r);
graph[mn - 1].add(mx);
graph[mx].add(mn - 1);
}
while(q.size() > 0) {
int root = q.poll();
for(int nxt : graph[root]) {
if(s[nxt] != 0) continue;
int l = Math.min(root, nxt) - 1, r = Math.max(root, nxt);
while(tree.size() > 0) {
Integer h = tree.higher(l);
if(h == null || h > r) break;
q.add(h);
tree.remove(h);
s[h] = 0;
}
}
}
if(tree.size() == 0) {
out.println("YES");
} else {
out.println("NO");
}
}
} | Java | ["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"] | 1 second | ["YES\nNO"] | NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$. | Java 17 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"greedy",
"sortings"
] | 2627417136abced8481ea5728d5c1f06 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i < r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 2,500 | For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 5ff0d54bbe4b450c9d5fbc2d1b833afd | train_108.jsonl | 1654266900 | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations. | 256 megabytes | /*
I am dead inside
Do you like NCT, sKz, BTS?
5 4 3 2 1 Moonwalk
Imma knock it down like domino
Is this what you want? Is this what you want?
Let's ttalkbocky about that :()
*/
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
public class x1687C2
{
public static void main(String omkar[]) throws Exception
{
FastScanner infile = new FastScanner();
int T = infile.nextInt();
StringBuilder sb = new StringBuilder();
while(T-->0)
{
int N = infile.nextInt();
int M = infile.nextInt();
int[] first = infile.nextInts(N);
int[] second = infile.nextInts(N);
long[] arr = new long[N];
for(int i=0; i < N; i++)
arr[i] = first[i]-second[i];
psums = new long[N];
psums[0] = arr[0];
for(int i=1; i < N; i++)
psums[i] = psums[i-1]+arr[i];
Range[] ranges = new Range[M];
for(int i=0; i < M; i++)
{
int a = infile.nextInt();
int b = infile.nextInt();
ranges[i] = new Range(a-1, b-1, i);
}
if(psums[N-1] == 0)
{
ArrayDeque<Integer> q = new ArrayDeque<Integer>();
ArrayList<Integer>[] edges = new ArrayList[N];
for(int i=0; i < N; i++)
edges[i] = new ArrayList<Integer>();
int[] tot = new int[M];
for(int m=0; m < M; m++)
{
if(ranges[m].left > 0)
{
tot[m]++;
edges[ranges[m].left-1].add(m);
}
edges[ranges[m].right].add(m);
tot[m]++;
}
TreeSet<Integer> active = new TreeSet<Integer>();
for(int i=0; i < N; i++)
{
if(psums[i] == 0)
{
for(int next: edges[i])
{
tot[next]--;
if(tot[next] == 0)
{
q.add(ranges[next].left);
q.add(ranges[next].right);
}
}
}
else
active.add(i);
}
while(q.size() > 0)
{
int L = q.poll();
int R = q.poll();
while(active.size() > 0 && active.last() >= L)
{
int id = active.ceiling(L);
if(id > R)
break;
for(int next: edges[id])
{
tot[next]--;
if(tot[next] == 0)
{
q.add(ranges[next].left);
q.add(ranges[next].right);
}
}
active.remove(id);
}
}
if(active.size() == 0)
sb.append("yEs\n");
else
sb.append("NO\n");
}
else
sb.append("no\n");
}
System.out.print(sb);
}
static long[] psums;
public static long query(int a, int b)
{
long res = psums[b];
if(a > 0)
res -= psums[a-1];
return res;
}
}
class Range
{
public int left;
public int right;
public int id;
public Range(int a, int b, int i)
{
left = a;
right = b;
id = i;
}
}
/*
1
5 3
0 0 0 -1 1
0 0 1 0 -1
1 2
3 5
2 4
*/
/*
Consider [1:3] and [3:5]
a b c d e
a+b+c = 0, c+d+e = 0
if a+b+c+d+e = 0, then c = 0
*/
/*
does BFS work?
let R be a range with sum = 0, let X be a range that overlaps with it
we can change bounds of X
*/
class FastScanner
{
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
} | Java | ["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"] | 1 second | ["YES\nNO"] | NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$. | Java 8 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"greedy",
"sortings"
] | 2627417136abced8481ea5728d5c1f06 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i < r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 2,500 | For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 4fccf277ec60825f85bd9279346abd3c | train_108.jsonl | 1654266900 | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations. | 256 megabytes | // Don't place your source in a package
import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;
// Please name your class Main
public class Main {
static FastScanner fs=new FastScanner();
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int Int() {
return Integer.parseInt(next());
}
long Long() {
return Long.parseLong(next());
}
String Str(){
return next();
}
}
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//reading /writing file
//Scanner sc=new Scanner(new File("src/text.txt"));
//PrintWriter pr=new PrintWriter("output.txt");
//File file = new File("src/text.txt");
int T=Int();
for(int t=0;t<T;t++){
Solution sol1=new Solution(out,fs);
sol1.solution();
}
out.flush();
}
public static int[] Arr(int n){
int A[]=new int[n];
for(int i=0;i<n;i++)A[i]=Int();
return A;
}
public static int Int(){
return fs.Int();
}
public static long Long(){
return fs.Long();
}
public static String Str(){
return fs.Str();
}
}
class Solution {
PrintWriter out;
int INF = 10000000;
int MOD = 998244353;
int mod = 1000000007;
Main.FastScanner fs;
public Solution(PrintWriter out,Main.FastScanner fs) {
this.out = out;
this.fs=fs;
}
public void solution() {
int n = fs.Int();
int m = fs.Int();
int a[] = new int[n + 1];
long s[] = new long[n + 1];
for(int i = 1; i <= n; i++) {
a[i] = fs.Int();
}
for(int i = 1; i <= n; i++) {
int x = fs.Int();
a[i] -= x;
s[i] = a[i] + s[i - 1];
}
TreeSet<Integer> tree = new TreeSet<>();
Queue<Integer> q = new LinkedList<>();
for(int i = 0; i <= n; i++) {
if(s[i] == 0) q.add(i);
else tree.add(i);
}
List<Integer> graph[] = new ArrayList[n + 1];
Arrays.setAll(graph, e->new ArrayList<>());
for(int i = 0; i < m; i++) {
int l = fs.Int();
int r = fs.Int();
int mn = Math.min(l, r), mx = Math.max(l, r);
graph[mn - 1].add(mx);
graph[mx].add(mn - 1);
}
while(q.size() > 0) {
int root = q.poll();
for(int nxt : graph[root]) {
if(s[nxt] != 0) continue;
int l = Math.min(root, nxt) - 1, r = Math.max(root, nxt);
while(tree.size() > 0) {
Integer h = tree.higher(l);
if(h == null || h > r) break;
q.add(h);
tree.remove(h);
s[h] = 0;
}
}
}
if(tree.size() == 0) {
out.println("YES");
} else {
out.println("NO");
}
}
}
| Java | ["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"] | 1 second | ["YES\nNO"] | NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$. | Java 8 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"greedy",
"sortings"
] | 2627417136abced8481ea5728d5c1f06 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i < r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 2,500 | For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | fc90e42066686aab216a948e3370060d | train_108.jsonl | 1654266900 | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class gaint_robot {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
for(int i=0; i<t; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
long[] joyce_qu = new long[n+1];
st = new StringTokenizer(br.readLine());
for(int j=1; j<=n; j++) joyce_qu[j] = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
for(int j=1; j<=n; j++) joyce_qu[j] -= Integer.parseInt(st.nextToken());
for(int j=1; j<=n; j++) joyce_qu[j] += joyce_qu[j-1];
ArrayList<Integer>[] very_cute = new ArrayList[n+1];
for(int j=0; j<=n; j++)very_cute[j] = new ArrayList<>();
for(int j=0; j<m; j++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
very_cute[a-1].add(b);
very_cute[b].add(a-1);
}
TreeSet<Integer> super_cuddy = new TreeSet<>();
super_cuddy.add(1000000000);
LinkedList<Integer> que = new LinkedList<>();
for(int j=1; j<=n; j++) {
if(joyce_qu[j]!=0)super_cuddy.add(j);
else que.add(j);
}
while(!que.isEmpty()) {
int j = que.removeFirst();
for(int k: very_cute[j]) {
if(joyce_qu[k]==0) {
int p = super_cuddy.higher(Math.min(j, k));
while(p<Math.max(j, k)) {
super_cuddy.remove(p);
joyce_qu[p] = 0;
que.add(p);
p = super_cuddy.higher(p);
}
}
}
}
if(super_cuddy.size()==1)out.write("YES\n");
else out.write("NO\n");
}
out.flush();
out.close();
}
}
| Java | ["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"] | 1 second | ["YES\nNO"] | NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$. | Java 8 | standard input | [
"binary search",
"brute force",
"data structures",
"dsu",
"greedy",
"sortings"
] | 2627417136abced8481ea5728d5c1f06 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i < r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. | 2,500 | For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.