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 | b5a1fcb7cfe98d6badf2301745ccd31e | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | /*
Author : Akshat Jindal
from NIT Jalandhar , Punjab , India
JAI MATA DI
*/
import java.util.*;
import java.io.*;
import java.math.*;
import java.sql.Array;
public class Main {
static class FR{
BufferedReader br;
StringTokenizer st;
public FR() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static long mod = (long)(1e9 + 7);
static void sort(long[] arr ) {
ArrayList<Long> al = new ArrayList<>();
for(long e:arr) al.add(e);
Collections.sort(al);
for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i);
}
static void sort(int[] arr ) {
ArrayList<Integer> al = new ArrayList<>();
for(int e:arr) al.add(e);
Collections.sort(al);
for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i);
}
static int UB(long[] arr , long find , int l , int r) {
while(l<=r) {
int m = (l+r)/2;
if(arr[m]<find) l = m+1;
else r = m-1;
}
return l;
}
static int LB(long[] arr , long find,int l ,int r ) {
while(l<=r) {
int m = (l+r)/2;
if(arr[m] > find) r = m-1;
else l = m+1;
}
return r;
}
static int UB(int[] arr , long find , int l , int r) {
while(l<=r) {
int m = (l+r)/2;
if(arr[m]<find) l = m+1;
else r = m-1;
}
return l;
}
static int LB(int[] arr , long find,int l ,int r ) {
while(l<=r) {
int m = (l+r)/2;
if(arr[m] > find) r = m-1;
else l = m+1;
}
return r;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
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 void sort(char[] arr) {
ArrayList<Character> al = new ArrayList<Character>();
for(char cc:arr) al.add(cc);
Collections.sort(al);
for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i);
}
static long[][] ncr(int n, int k)
{
long C[][] = new long[n + 1][k + 1];
int i, j;
// Calculate value of Binomial
// Coefficient in bottom up manner
for (i = 0; i <= n; i++) {
for (j = 0; j <= Math.min(i, k); j++) {
// Base Cases
if (j == 0 || j == i)
C[i][j] = 1;
// Calculate value using
// previously stored values
else
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j])%mod;
}
}
return C;
}
static long modInverse(long a, long m)
{
long g = gcd(a, 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 = (int)((p * (long)p) % m);
if (y % 2 == 0)
return p;
else
return (int)((x * (long)p) % m);
}
static class DisjointUnionSets {
int[] rank, parent;
int n;
public DisjointUnionSets(int n)
{
rank = new int[n];
parent = new int[n];
this.n = n;
makeSet();
}
void makeSet()
{
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
int find(int x)
{
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void union(int x, int y)
{
int xRoot = find(x), yRoot = find(y);
if (xRoot == yRoot)
return;
if (rank[xRoot] < rank[yRoot])
parent[xRoot] = yRoot;
else if (rank[yRoot] < rank[xRoot])
parent[yRoot] = xRoot;
else
{
parent[yRoot] = xRoot;
rank[xRoot] = rank[xRoot] + 1;
}
}
}
/************************************************ Query **************************************************************************************/
static class SparseTable{
private long[][] st;
SparseTable(long[] arr){
int n = arr.length;
st = new long[n][25];
log = new int[n+2];
build_log(n+1);
build(arr);
}
private void build(long[] arr) {
int n = arr.length;
for(int i = n-1 ; i>=0 ; i--) {
for(int j = 0 ; j<25 ; j++) {
int r = i + (1<<j)-1;
if(r>=n) break;
if(j == 0 ) st[i][j] = arr[i];
else st[i][j] = gcd(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] );
}
}
}
public long gcd(long a ,long b) {
if(a == 0) return b;
return gcd(b%a , a);
}
public long query(int l ,int r) {
int w = r-l+1;
int power = log[w];
return gcd(st[l][power],st[r - (1<<power) + 1][power]);
}
private int[] log;
void build_log(int n) {
log[1] = 0;
for(int i = 2 ; i<=n ; i++) {
log[i] = 1 + log[i/2];
}
}
}
// static int query(int[] tree , int ss ,int se ,int qs , int qe,int index) {
//
// if(ss>=qs && se<=qe) return tree[index];
//
// if(qe<ss || se<qs) return Integer.MAX_VALUE;
//
// int mid = (ss + se)/2;
// int left = query(tree , ss , mid , qs ,qe , 2*index);
// int right= query(tree ,mid + 1 , se , qs ,qe , 2*index+1);
// return Math.min(left, right);
// }
// static void buildTree(int[] a ,int s ,int e ,int[] tree ,int index ) {
// if(s == e) {
// tree[index] = a[s];
// return;
// }
//
// int mid = (s+e)/2;
//
// buildTree(a, s, mid, tree, 2*index);
// buildTree(a, mid+1, e, tree, 2*index+1);
//
// tree[index] = tree[2*index] + tree[2*index+1];
// }
/* ***************************************************************************************************************************************************/
static FR sc = new FR();
static StringBuilder sb = new StringBuilder();
public static void main(String args[]) {
int tc = 1;
// tc = sc.nextInt();
while(tc-->0) {
TEST_CASE();
}
System.out.println(sb);
}
static void TEST_CASE() {
int n = sc.nextInt();
if(n%2 == 1) --n;
n = n/2;
long ans = 0;
long[] arr = new long[n];
for(int i = 0 ; i <n ; i++) {
long a = sc.nextLong() , b = sc.nextLong();
ans += Math.min(a, b);
arr[i] = a-b;
}
for(int i =0 ; i<n ; i ++) {
long a = arr[i];
if(a<0) continue;
long b = 0;
for(int j = i+1 ; j<n ; j++) {
if((b+=arr[j]) <= 0) {
ans += Math.min(a, -b) + 1;
if( (a+=b) < 0) break;
b = 0;
}
}
}
System.out.println(ans);
}
}
/*******************************************************************************************************************************************************/
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 9112983165874b0f1fedd61780addc07 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 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.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
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);
CCompressedBracketSequence solver = new CCompressedBracketSequence();
solver.solve(1, in, out);
out.close();
}
static class CCompressedBracketSequence {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int[] arr = in.nextIntArray(n);
long ans = 0;
long min = Long.MAX_VALUE;
for (int i = 0; i < n; i += 2) {
long t = ans;
long temp = arr[i];
if (i + 1 < n) {
temp -= arr[i + 1];
if (temp >= 0) {
ans += arr[i + 1];
} else {
ans += arr[i];
}
min = temp;
}
int j = i + 2;
while (j < n && temp >= 0) {
if (j % 2 == 0) {
temp += arr[j];
j++;
continue;
}
temp -= arr[j];
if (temp <= 0) ans += min + 1;
else if (temp <= min) {
ans += min - temp + 1;
}
min = Math.min(temp, min);
j++;
}
// out.println(i,ans-t);
}
out.println(ans);
}
}
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 {
BufferedReader reader;
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[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 4ed5b585e544c9162f48e208db3bb57a | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes |
import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args)
{
FastScanner sc=new FastScanner();
int t=1;
PrintWriter pw=new PrintWriter(System.out);
while(t-->0) {
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
long ans=0;
for(int left=0;left<n;left+=2) {
long bal=0;
long min_bal=a[left];
for(int right=left+1;right<n && min_bal>=0;right+=2) {
bal+=a[right-1]-a[right];
if(bal<=min_bal) {
ans+=min_bal-Math.max(0, bal)+1;
}
min_bal=Math.min(min_bal, bal);
}
}
pw.println(ans-n/2);
}
pw.flush();
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 4ff5a831f7c8a0c5262f9b4fb848993c | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.io.*;
public class CompressedBracketseq {
private static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static long[] extra(long[] arr, int a, int b) {
long l = 0;
long r = 0;
long extrar = 0;
long extral = 0;
for(int i = a; i<=b; i++)
{
if(i%2==0)
l+=arr[i];
else
r+=arr[i];
if(r>l)
{
extrar+=(r-l);
r = l;
}
}
extral = l-r;
long[] ans = new long[2];
ans[0] = extral;
ans[1] = extrar;
return ans;
}
public static long solution(long[] arr, int n) {
long count = 0;
for(int i = 0; i<n-1; i+=2) {
for(int j = i+1; j<n; j+=2)
{
if(j==i+1)
count+=Math.min(arr[i], arr[j]);
else
{
long[] ans = extra(arr,i+1,j-1);
long extral = ans[0];
long extrar = ans[1];
if(arr[i]>=extrar && arr[j]>=extral)
{
count++;
count+=(Math.min(arr[i]-extrar,arr[j]-extral));
}
}
}
}
return count;
}
private static PrintWriter out = new PrintWriter(System.out);
public static void main (String[] args)
{
MyScanner s = new MyScanner();
int n = s.nextInt();
long[] arr = new long[n];
for(int i =0; i<n; i++)
arr[i] = s.nextLong();
out.println(solution(arr,n));
out.flush();
out.close();
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | fbc7ecde92bbeebfc658eebb464d798a | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class eashan{
public static boolean checker(long[] arr,long K,long diff)
{
long collect=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>diff)
collect+=arr[i]-diff;
}
if(collect>=K)
return true;
else
return false;
}
public static long search(long[] arr,long K,long R)
{
long l=0;
long r=R;
while(l<=r)
{
long mid=(l+r)/2;
if(checker(arr,K,mid))
{
if(checker(arr,K,mid+1))
l=mid+1;
else
return mid;
}
else
r=mid-1;
}
return -1;
}
static void sieveOfEratosthenes(int n,ArrayList<Integer> arr,ArrayList<Integer> arr1)
{
// 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.
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
int x=0;
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
{
arr.add(i);
}
}
System.out.println(arr.size());
}
public static boolean check(long[] time,long complete,long tasks)
{
long c=0;
for(int i=0;i<time.length;i++)
c+=complete/time[i];
if(c>=tasks)
return true;
else
return false;
}
public static long sear(long[] arr,long tasks)
{
long l=0;
long r=(long)Math.pow(10,18);
while(l<=r)
{
long mid=(l+r)/2;
//System.out.println(mid);
//System.out.println(pro.get(mid));
if(check(arr,mid,tasks))
{
if(check(arr,mid-1,tasks))
r=mid-1;
else
return mid;
}
else
l=mid+1;
}
return -1;
}
public static long get_answer(int node_start, int node_end, int node_index, int query_start, int query_end, long seg_tree[])
{
if(query_start <= node_start && query_end >= node_end)
{
return seg_tree[node_index];
}
if(node_end < query_start || query_end < node_start)
{
// long s=0;
return (long)Math.pow(2,31)-1;
}
int mid=node_start+(node_end-node_start)/2;
return get_answer(node_start, mid, 2*node_index+1, query_start, query_end, seg_tree)&
get_answer(mid+1, node_end, 2*node_index+2,query_start, query_end, seg_tree);
}
public static void update_recurse(long seg_tree[], int node_start, int node_end, int update_index, long old_value, long new_value, int node_index)
{
if(update_index < node_start || update_index > node_end)
return;
seg_tree[node_index] = (seg_tree[node_index]|old_value)&new_value;
if(node_start!=node_end)
{
int mid = node_start+(node_end-node_start)/2;
update_recurse(seg_tree, node_start,mid,update_index,old_value,new_value,2*node_index+1);
update_recurse(seg_tree,mid+1,node_end,update_index,old_value, new_value, 2*node_index+2);
}
}
public static void update(long elements[], long seg_tree[], int i, int N, long new_value)
{
if(i > 0 && i <= N-1)
{
int number_of_bits = (int)(Math.floor(Math.log(elements[i]) / Math.log(2))) + 1;
long complement = elements[i]^(long)(Math.pow(2,31)-1);
// long complement = ~elements[i];
// System.out.println(complement);
// System.out.println((long)((1 << number_of_bits)-1));
elements[i] = new_value;
update_recurse(seg_tree,0,N-1,i,complement,new_value,0);
}
}
public static long create_seg_tree(long elements[], int node_start, int node_end, long seg_tree[], int node_index)
{
if(node_start == node_end)
{
seg_tree[node_index]=elements[node_start];
return elements[node_start];
}
int mid = node_start+(node_end-node_start)/2;
seg_tree[node_index]=create_seg_tree(elements, node_start, mid, seg_tree, 2*node_index+1) & create_seg_tree(elements, mid+1, node_end, seg_tree, 2*node_index+2);
return seg_tree[node_index];
}
public static void main(String[] args)throws IOException {
Reader.init(System.in);
BufferedWriter output=new BufferedWriter(new OutputStreamWriter(System.out));
// int T=Reader.nextInt();
// for(int m=1;m<=T;m++)
// {
int N=Reader.nextInt();
long[] arr=new long[N];
for(int i=0;i<N;i++)
arr[i]=Reader.nextLong();
long k=0; long open=0,ans=0;boolean status=true;
Stack<Integer> st=new Stack<>();
Stack<Long> store=new Stack<>();
for(int i=0;i<N;i++)
{
if(i%2==0)
{
st.push(1);
store.push(arr[i]);
continue;
}
while(st.size()>0)
{
if(st.peek()==1)
{
if(store.peek()<arr[i])
{
//System.out.println("1 "+i);
ans += store.peek();
arr[i] -= store.pop();
st.pop();
}
else if(store.peek()==arr[i])
{
//System.out.println("2 "+i);
ans+=store.peek();
store.pop();
st.pop();
arr[i]=0;
k=1;
while(st.size()>0 && st.peek()==-1)
{
k+=store.pop();
st.pop();
}
store.push(k);
st.push(-1);
ans+=k-1;
break;
}
else
{
//System.out.println("3 "+i);
long x=store.pop();
store.push(x-arr[i]);
ans+=arr[i];
st.push(-1);
store.push((long)1);
break;
}
}
else
{
//System.out.println("4 "+i);
st.pop();
ans+=store.pop();
}
//System.out.println(ans);
}
}
output.write(ans+"");
output.flush();
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | fd919890bd4e02a874077f51756fa7dd | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class q3 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
String[] parts = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(parts[i]);
}
long ans = 0;
for(int i = 1;i < n;i += 2){
ans += Math.min(arr[i - 1],arr[i]);
int j = i - 2;
long copen = arr[i - 1] - arr[i];
long open = 0;
while(j >= 0 && copen <= 0){
open += arr[j - 1] - arr[j];
if(open >= 0){
if(open > -copen){
ans += (-copen + 1);
break;
}else if(open < -copen){
ans += open + 1;
copen += open;
open = 0;
}else{
ans += open + 1;
copen = 0;
open = 0;
}
}
j -= 2;
}
}
System.out.println(ans);
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | ed5eb9c684d82c75a7bead16be5a4308 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class q3 {
// public static class Pair{
// int open;
// int ans;
// Pair(int open,int ans){
// this.open = open;
// this.ans = ans;
// }
// }
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
String[] parts = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(parts[i]);
}
// Pair[] dp = new Pair[n];
// int open = 0;
long ans = 0;
for(int i = 1;i < n;i += 2){
ans += Math.min(arr[i - 1],arr[i]);
int j = i - 2;
long copen = arr[i - 1] - arr[i];
long open = 0;
while(j >= 0 && copen <= 0){
open += arr[j - 1] - arr[j];
if(open >= 0){
// System.out.print(ans + " ");
if(open > -copen){
ans += (-copen + 1);
break;
}else if(open < -copen){
ans += open + 1;
copen += open;
open = 0;
}else{
ans += open + 1;
copen = 0;
open = 0;
}
// long val = Math.min(open,-copen) + 1;
// ans += val;
// copen += (val - 1);
// System.out.println(ans);
}
j -= 2;
}
// System.out.println(ans);
// open += arr[i - 1] - arr[i];
// for(int j = i - 2;j >= 0 && copen <= 0;j -= 2){
// cans += dp[j].ans;
// if(dp[j].open > 0 && copen <= 0){
// cans += Math.min(dp[j].open,-copen);
// }
// copen += arr[j - 1] - arr[j];
// break;
// }
// ans += cans;
// System.out.println(ans);
// dp[i] = new Pair(open,cans);
}
System.out.println(ans);
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | d61edc5e04a6662cad5d64fc759fd009 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//int cases = Integer.parseInt(br.readLine());
//o:while(cases-- > 0) {
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int[] c = new int[n];
str = br.readLine().split(" ");
for(int i=0; i<n; i++) {
c[i] = Integer.parseInt(str[i]);
}
long ans = 0;
for(int i=0; i+1<n; i+=2) { // ck - open,, ck1 or ck+1 - close
ans += Math.min(c[i], c[i+1]);
if(c[i+1] > c[i]) { // cant go further if there r pending close brackets here itself
continue;
}
long ci = c[i] - c[i+1]; // pending opens at i
long cj = 0; // represents the pending opens till the j or j+1 from i+2
for(int j=i+2; j+1<n; j+=2) {
cj += c[j];
if(cj > c[j+1]) { // pending opens > closes available here, so no points here
// close how many ever u can and move on
cj -= c[j+1];
continue;
}
// we can close some ci if ci arent 0.
long cj1 = c[j+1] - cj; // spare ones left after closing all opens not including i opens
cj = 0; // since we just closed all pending opens except at i
ans++; // we can consider i to j+1 as regular, so ans++ even if cj1 == 0
if(cj1 > ci) {
ans += ci;
break; // if we have closes even after we close opens at i, we cant move further
}
ci -= cj1; // might become 0 or stay positive
ans += cj1;
}
}
System.out.println(ans);
//}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 3bdaef548a35c4cfb4443899c51fad4c | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class taskc {
public static void main(String[] args) {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextLong();
}
long b[][] = new long[n][n];
long mn[][] = new long[n][n];
for (int i = 0; i < n; i++) {
long sum = a[i] * (i % 2 == 0 ? 1L : -1L);
long min = sum;
for (int j = i + 1; j < n; j++) {
sum += a[j] * (j % 2 == 0 ? 1L : -1L);
min = Math.min(min, sum);
b[i][j] = sum;
mn[i][j] = min;
}
}
long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (i % 2 == 0 && j % 2 == 1) {
long mid = (i + 1 < j - 1 ? b[i + 1][j - 1] : 0L);
long mid_min = (i + 1 < j - 1 ? mn[i + 1][j - 1] : 0L);
// use x from left to make x + mid_min >= 0 && x + mid - y = 0;
long from_x = Math.max(1, -mid_min), to_x = a[i];
if (from_x > to_x) {
continue;
}
long from_y = Math.max(1, from_x + mid);
long to_y = Math.min(a[j], to_x + mid);
if (from_y <= to_y) {
ans += to_y - from_y + 1;
}
}
}
}
out.println(ans);
out.flush();
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer("");
String next() {
while(!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return tok.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());
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 1de26d8f822a7ce30419d57302ac94d8 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
// BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=1;
// t=sc.nextInt();
// int t=Integer.parseInt(br.readLine());
while(--t>=0){
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
long ans=0;
for(int i=1;i<n;i=i+2){
long x=a[i];
long y=0;
for(int j=i;j>=0;j--){
if(j%2!=0)y=y+a[j];
else{
y=y-a[j];
if(y>=0){
ans=ans+Math.max(x-y,0);
if(y<=x&&(i-j)>2)ans++;
x=Math.min(x,y);
}
else{
ans=ans+x;
if((i-j)>2)ans++;
break;
}
}
if(y<0)break;
}
// System.out.println(ans);
}
System.out.println(ans);
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | cd4e3f57bbcb3102750ab75e52f2bd02 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | // Generated by Code Flattener.
// https://plugins.jetbrains.com/plugin/9979-idea-code-flattener
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
Solution solution = new ASolution();
boolean local = System.getProperty("ONLINE_JUDGE") == null && !solution.isInteractive();
String input = local ? "input.txt" : null;
solution.in = new MyScanner(input);
solution.out = MyWriter.of(null);
solution.bm = new Benchmark();
solution.al = new Algorithm();
solution.run();
solution.close();
}
private abstract static class Solution {
public MyScanner in;
public MyWriter out;
public Benchmark bm;
public Algorithm al;
public void init() {
}
public abstract void solve();
protected boolean isMultiTest() {
return true;
}
public void run() {
init();
if (isMultiTest()) {
int t = in.nextInt();
for (int i = 0; i < t; i++) {
solve();
}
} else {
solve();
}
}
public void close() {
out.close();
}
public boolean isInteractive() {
return false;
}
}
private static class ASolution extends Solution {
public void solve() {
int n = in.nextInt();
int[] a = in.nextInts(n);
long ans = 0;
for (int i = 0; i < n; i += 2) {
long lReg = 1;
long rReg = 1;
long count = 0;
for (int j = i + 1; j < n; j++) {
if (j % 2 == 1) {
long l = a[i] - lReg + 1;
long r = a[j] - rReg + 1;
if (l <= 0) {
break;
}
if (r > 0) {
ans += Math.min(l, r);
}
}
if (j % 2 == 1) {
count -= a[j];
} else {
count += a[j];
}
lReg = Math.max(lReg, -count);
rReg = Math.max(1, count + lReg);
}
}
out.println(ans);
}
@Override
protected boolean isMultiTest() {
return false;
}
}
private static class MyScanner {
private final BufferedReader br;
private StringTokenizer st;
public MyScanner(String fileName) {
if (fileName != null) {
try {
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
} else {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
public String next() {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextInts(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public String nextLine() {
try {
String line = br.readLine();
if (line == null) {
throw new RuntimeException("empty line");
}
st = null;
return line;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private static class Benchmark {
}
private static class Algorithm {
}
private static class MyWriter extends PrintWriter {
public static MyWriter of(String fileName) {
if (fileName != null) {
try {
return new MyWriter(new FileWriter(fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
return new MyWriter(new BufferedOutputStream(System.out));
}
}
public MyWriter(FileWriter fileWriter) {
super(fileWriter);
}
public MyWriter(OutputStream out) {
super(out);
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | e63c88983f96f58753c68513ed2dfd8e | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
FastIO io = new FastIO();
int test=1;
while(test>0)
{
int n=io.nextInt();
long arr[]=new long[n];
for(int i=0;i<n;i++)arr[i]=io.nextLong();
long count[]=new long[n];
int sp[]=new int[n];
long ans=0;
for(int i=0;i<n;i++)
{
if(i%2==0)
{
if(i==0)continue;
int j=i-1;
while(j>=0)
{
if(arr[j]!=0)break;
if(j%2==1)
{
count[i]++;
j=sp[j];
}
else
{
j--;
}
}
//io.println(i+" "+count[i]);
}
else
{
int j=i-1;
while(j>=0)
{
if(j%2==1)
{
if(arr[j]>0){
sp[i]=j+1;
break;
}
j--;
continue;
}
if(arr[j]>=arr[i])
{
if(arr[i]==arr[j])ans+=count[j];
arr[j]-=arr[i];
ans+=arr[i];
arr[i]=0;
sp[i]=j;
break;
}
arr[i]-=arr[j];
if(arr[j]!=0)
{
ans+=arr[j]+count[j];
arr[j]=0;
}
j-=1;
}
if(j<0)sp[i]=-1;
}
}
io.println(ans);
test--;
}
io.close();
}
}
class FastIO extends PrintWriter {
private InputStream stream;
private byte[] buf = new byte[1<<16];
private int curChar, numChars;
// standard input
public FastIO() { this(System.in,System.out); }
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
private int nextByte() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1) return -1; // end of file
}
return buf[curChar++];
}
public String nextLine() {
int c; do { c = nextByte(); } while (c <= '\n');
StringBuilder res = new StringBuilder();
do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n');
return res.toString();
}
public String next() {
int c; do { c = nextByte(); } while (c <= ' ');
StringBuilder res = new StringBuilder();
do { res.appendCodePoint(c); c = nextByte(); } while (c > ' ');
return res.toString();
}
public int nextInt() {
int c; do { c = nextByte(); } while (c <= ' ');
int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); }
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10*res+c-'0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public long nextLong() {
int c; do { c = nextByte(); } while (c <= ' ');
long sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); }
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10*res+c-'0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public double nextDouble() { return Double.parseDouble(next()); }
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | e072d82be20e86f595df0842cb67c091 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes |
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
FastScanner fs = new FastScanner();
java.io.PrintWriter out = new java.io.PrintWriter(System.out);
solve(fs, out);
out.flush();
}
public void solve(FastScanner fs, java.io.PrintWriter out) {
int n = fs.nextInt();
long[] c = new long[n];
for (int i = 0;i < n;++ i) c[i] = fs.nextLong();
long[][] dp1 = new long[n][n + 1], dp2 = new long[n][n + 1];
for (int i = 0;i < n;++ i) {
dp1[i][i] = 0;
dp2[i][i] = 0;
for (int j = i + 1;j <= n;++ j) {
if (j % 2 != 0) {
dp1[i][j] = dp1[i][j - 1];
dp2[i][j] = dp2[i][j - 1] + c[j - 1];
} else {
dp1[i][j] = dp1[i][j - 1] + Math.max(0, c[j - 1] - dp2[i][j - 1]);
dp2[i][j] = Math.max(0, dp2[i][j - 1] - c[j - 1]);
}
}
}
long ans = 0;
for (int i = 0;i < n;i += 2) {
if (i < n - 1) ans += Math.min(c[i], c[i + 1]);
for (int j = i + 3;j < n;j += 2) {
long l = c[i], r = c[j];
l -= dp1[i + 1][j];
r -= dp2[i + 1][j];
ans += Math.max(0, Math.min(l, r) + 1);
}
}
out.println(ans);
}
final int MOD = 998_244_353;
int plus(int n, int m) {
int sum = n + m;
if (sum >= MOD) sum -= MOD;
return sum;
}
int minus(int n, int m) {
int sum = n - m;
if (sum < 0) sum += MOD;
return sum;
}
int times(int n, int m) {
return (int)((long)n * m % MOD);
}
int divide(int n, int m) {
return times(n, IntMath.pow(m, MOD - 2, MOD));
}
int[] fact, invf;
void calc(int len) {
len += 2;
fact = new int[len];
invf = new int[len];
fact[0] = fact[1] = invf[0] = invf[1] = 1;
for (int i = 2;i < fact.length;++ i) fact[i] = times(fact[i - 1], i);
invf[len - 1] = divide(1, fact[len - 1]);
for (int i = len - 1;i > 1;-- i) invf[i - 1] = times(i, invf[i]);
}
int comb(int n, int m) {
if (n < m) return 0;
return times(fact[n], times(invf[n - m], invf[m]));
}
}
class FastScanner {
private final java.io.InputStream in = System.in;
private final byte[] buffer = new byte[8192];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) return true;
ptr = 0;
try {
buflen = in.read(buffer);
} catch (java.io.IOException e) {
e.printStackTrace();
}
return buflen > 0;
}
private byte readByte() {
return hasNextByte() ? buffer[ptr++ ] : -1;
}
private static boolean isPrintableChar(byte c) {
return 32 < c || c < 0;
}
private static boolean isNumber(int c) {
return '0' <= c && c <= '9';
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++ ;
return hasNextByte();
}
public String next() {
if (!hasNext()) throw new java.util.NoSuchElementException();
StringBuilder sb = new StringBuilder();
byte b;
while (isPrintableChar(b = readByte()))
sb.appendCodePoint(b);
return sb.toString();
}
public final char nextChar() {
if (!hasNext()) throw new java.util.NoSuchElementException();
return (char)readByte();
}
public final long nextLong() {
if (!hasNext()) throw new java.util.NoSuchElementException();
long n = 0;
try {
byte b = readByte();
if (b == '-') {
while (isNumber(b = readByte()))
n = n * 10 + '0' - b;
return n;
} else if (!isNumber(b)) throw new NumberFormatException();
do
n = n * 10 + b - '0';
while (isNumber(b = readByte()));
} catch (java.util.NoSuchElementException e) {}
return n;
}
public final int nextInt() {
if (!hasNext()) throw new java.util.NoSuchElementException();
int n = 0;
try {
byte b = readByte();
if (b == '-') {
while (isNumber(b = readByte()))
n = n * 10 + '0' - b;
return n;
} else if (!isNumber(b)) throw new NumberFormatException();
do
n = n * 10 + b - '0';
while (isNumber(b = readByte()));
} catch (java.util.NoSuchElementException e) {}
return n;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
class Arrays {
public static void sort(final int[] array) {
sort(array, 0, array.length);
}
public static void sort(final int[] array, int fromIndex, int toIndex) {
if (toIndex - fromIndex <= 512) {
java.util.Arrays.sort(array, fromIndex, toIndex);
return;
}
sort(array, fromIndex, toIndex, 0, new int[array.length]);
}
private static final void sort(int[] a, final int from, final int to, final int l, final int[] bucket) {
if (to - from <= 512) {
java.util.Arrays.sort(a, from, to);
return;
}
final int BUCKET_SIZE = 256;
final int INT_RECURSION = 4;
final int MASK = 0xff;
final int shift = l << 3;
final int[] cnt = new int[BUCKET_SIZE + 1];
final int[] put = new int[BUCKET_SIZE];
for (int i = from; i < to; i++) ++ cnt[(a[i] >>> shift & MASK) + 1];
for (int i = 0; i < BUCKET_SIZE; i++) cnt[i + 1] += cnt[i];
for (int i = from; i < to; i++) {
int bi = a[i] >>> shift & MASK;
bucket[cnt[bi] + put[bi]++] = a[i];
}
for (int i = BUCKET_SIZE - 1, idx = from; i >= 0; i--) {
int begin = cnt[i];
int len = cnt[i + 1] - begin;
System.arraycopy(bucket, begin, a, idx, len);
idx += len;
}
final int nxtL = l + 1;
if (nxtL < INT_RECURSION) {
sort(a, from, to, nxtL, bucket);
if (l == 0) {
int lft, rgt;
lft = from - 1; rgt = to;
while (rgt - lft > 1) {
int mid = lft + rgt >> 1;
if (a[mid] < 0) lft = mid;
else rgt = mid;
}
reverse(a, from, rgt);
reverse(a, rgt, to);
}
}
}
public static void sort(final long[] array) {
sort(array, 0, array.length);
}
public static void sort(final long[] array, int fromIndex, int toIndex) {
if (toIndex - fromIndex <= 512) {
java.util.Arrays.sort(array, fromIndex, toIndex);
return;
}
sort(array, fromIndex, toIndex, 0, new long[array.length]);
}
private static final void sort(long[] a, final int from, final int to, final int l, final long[] bucket) {
final int BUCKET_SIZE = 256;
final int LONG_RECURSION = 8;
final int MASK = 0xff;
final int shift = l << 3;
final int[] cnt = new int[BUCKET_SIZE + 1];
final int[] put = new int[BUCKET_SIZE];
for (int i = from; i < to; i++) ++ cnt[(int) ((a[i] >>> shift & MASK) + 1)];
for (int i = 0; i < BUCKET_SIZE; i++) cnt[i + 1] += cnt[i];
for (int i = from; i < to; i++) {
int bi = (int) (a[i] >>> shift & MASK);
bucket[cnt[bi] + put[bi]++] = a[i];
}
for (int i = BUCKET_SIZE - 1, idx = from; i >= 0; i--) {
int begin = cnt[i];
int len = cnt[i + 1] - begin;
System.arraycopy(bucket, begin, a, idx, len);
idx += len;
}
final int nxtL = l + 1;
if (nxtL < LONG_RECURSION) {
sort(a, from, to, nxtL, bucket);
if (l == 0) {
int lft, rgt;
lft = from - 1; rgt = to;
while (rgt - lft > 1) {
int mid = lft + rgt >> 1;
if (a[mid] < 0) lft = mid;
else rgt = mid;
}
reverse(a, from, rgt);
reverse(a, rgt, to);
}
}
}
public static void reverse(int[] array) {
reverse(array, 0, array.length);
}
public static void reverse(int[] array, int fromIndex, int toIndex) {
for (-- toIndex;fromIndex < toIndex;++ fromIndex, -- toIndex) {
int swap = array[fromIndex];
array[fromIndex] = array[toIndex];
array[toIndex] = swap;
}
}
public static void reverse(long[] array) {
reverse(array, 0, array.length);
}
public static void reverse(long[] array, int fromIndex, int toIndex) {
for (-- toIndex;fromIndex < toIndex;++ fromIndex, -- toIndex) {
long swap = array[fromIndex];
array[fromIndex] = array[toIndex];
array[toIndex] = swap;
}
}
public static void shuffle(int[] array) {
java.util.Random rnd = new java.util.Random();
for (int i = 0;i < array.length;++ i) {
int j = rnd.nextInt(array.length - i) + i;
int swap = array[i];
array[i] = array[j];
array[j] = swap;
}
}
public static void shuffle(long[] array) {
java.util.Random rnd = new java.util.Random();
for (int i = 0;i < array.length;++ i) {
int j = rnd.nextInt(array.length - i) + i;
long swap = array[i];
array[i] = array[j];
array[j] = swap;
}
}
}
class IntMath {
public static int gcd(int a, int b) {
while (a != 0)
if ((b %= a) != 0) a %= b;
else return a;
return b;
}
public static int gcd(int... array) {
int ret = array[0];
for (int i = 1; i < array.length; ++i)
ret = gcd(ret, array[i]);
return ret;
}
public static long gcd(long a, long b) {
while (a != 0)
if ((b %= a) != 0) a %= b;
else return a;
return b;
}
public static long gcd(long... array) {
long ret = array[0];
for (int i = 1; i < array.length; ++i)
ret = gcd(ret, array[i]);
return ret;
}
public static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
public static int pow(int a, int b) {
int ans = 1;
for (int mul = a; b > 0; b >>= 1, mul *= mul)
if ((b & 1) != 0) ans *= mul;
return ans;
}
public static long pow(long a, long b) {
long ans = 1;
for (long mul = a; b > 0; b >>= 1, mul *= mul)
if ((b & 1) != 0) ans *= mul;
return ans;
}
public static int pow(int a, long b, int mod) {
if (b < 0) b = b % (mod - 1) + mod - 1;
long ans = 1;
for (long mul = a; b > 0; b >>= 1, mul = mul * mul % mod)
if ((b & 1) != 0) ans = ans * mul % mod;
return (int)ans;
}
public static int pow(long a, long b, int mod) {
return pow((int)(a % mod), b, mod);
}
public static int floorsqrt(long n) {
return (int)Math.sqrt(n + 0.1);
}
public static int ceilsqrt(long n) {
return n <= 1 ? (int)n : (int)Math.sqrt(n - 0.1) + 1;
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 1734bff3a56926ecd643dfc40233e30e | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
public class C {
static FastScanner fs = new FastScanner();
static PrintWriter pw = new PrintWriter(System.out);
static StringBuilder sb = new StringBuilder("");
static final long INF = (long) 10e12;
public static void main(String[] args) {
int n = fs.nextInt();
int[] c = new int[n];
for (int i = 0; i < n; i++) c[i] = fs.nextInt();
long[] s = new long[n+1];
for (int i = 0; i < n; i++) {
if (i % 2 == 0) s[i+1] = s[i] + c[i];
else s[i+1] = s[i] - c[i];
}
long ans = 0;
for (int i = 0; i < n; i+=2) {
long p = INF;
for (int j = i+1; j < n; j+=2) {
long mn = Math.max(s[i], s[j+1]);
long mx = Math.min(p+1, Math.min(s[i+1], s[j]));
if (mn < mx) ans += (mx-mn);
p = Math.min(p, s[j+1]);
}
}
pw.print(ans);
pw.close();
}
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());}
double nextDouble() {return Double.parseDouble(next());}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 64df2089ee463d9d247f2be77fea1b22 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class A3 {
void solve() {
int n = cint();
long[] a = carr_long(n);
// int n = 6;
// long[] a = new long[n];
// for (int i = 0; i < a.length; i++) {
// a[i] = new Random().nextInt(4) + 1;
// }
// out.println(Arrays.toString(a));
long result = 0;
for (int i = 0; i < n; i += 2) {
for (int j = i + 1; j < n; j += 2) {
long r = count(a, i, j);
result += r;
}
}
cout(result);
// cout(directCount(a, 0, a.length-1));
}
long count(long[] a, int x, int y) {
long balance = 0;
long minLeft = 0;
long minRight = 0;
for (int i = x+1; i <= y-1; i++) {
if (i % 2 == 0) {
balance += a[i];
} else {
balance -= a[i];
}
minLeft = Math.max(-balance, minLeft);
}
long b2 = 0;
for (int i = y-1; i >= x+1; i--) {
if (i % 2 == 0) {
b2 -= a[i];
} else {
b2 += a[i];
}
minRight = Math.max(-b2, minRight);
}
long r1 = a[x];
long r2 = a[y];
if (balance > 0) {
r1 = r1 - Math.max(0, minLeft - 1);
r2 = r2 - Math.max(balance, minRight - 1);
} else {
r1 = r1 - Math.max(-balance, minLeft - 1);
r2 = r2 - Math.max(0, minRight - 1);
}
long count = Math.min(r1, r2);
// if (count > 0) {
// out.println(toStr(a, x, y));
// out.println("a[x]=" + a[x]);
// out.println("a[y]=" + a[y]);
// out.println("left=" + left);
// out.println("minimum=" + minimum);
// out.println("[" + x + " " + y + "] = " + count);
// }
return count < 0 ? 0 : count;
}
private static String toStr(long[] a, int x, int y) {
StringBuilder buff = new StringBuilder();
for (int i = x; i <= y; i++) {
char c = (i % 2 == 0) ? '(' : ')';
for (int j = 0; j < a[i]; j++) {
buff.append(c);
}
}
return buff.toString();
}
private int directCount(long[] a, int x, int y) {
String s = toStr(a, x, y);
int result = 0;
for (int i = 0; i < s.length(); i++) {
for (int j = i+1; j <= s.length(); j++) {
int r = 0;
boolean ok = true;
for (int k = i; k < j; k++) {
if (s.charAt(k) == '(') r++; else r--;
if (r < 0) {
ok = false;
break;
}
}
if (ok && r == 0) {
result++;
out.println(s.substring(i, j));
// } else {
// out.println(s.substring(i, j));
}
}
}
return result;
}
public static void main(String... args) {
// int t = in.nextInt();
// in.nextLine();
// for (int test = 0; test < t; test++) {
new A3().solve();
// }
}
static final Scanner in = new Scanner(System.in);
static final PrintStream out = System.out;
static int cint() {
return in.nextInt();
}
static long clong() {
return in.nextLong();
}
static String cstr() {
return in.nextLine();
}
static int[] carr_int(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
return a;
}
static long[] carr_long(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextLong();
}
return a;
}
static void cout(int... a) {
for (int i = 0; i < a.length; i++) {
if (i != 0) {
out.print(' ');
}
out.print(a[i]);
}
out.println();
}
static void cout(long... a) {
for (int i = 0; i < a.length; i++) {
if (i != 0) {
out.print(' ');
}
out.print(a[i]);
}
out.println();
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | f11c43c2d1fe97511c5f25d9c0a0ea4b | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
//static final long mod=(long)1e9+7;
/*static final long mod=998244353L;
public static long pow(long a,long p)
{
long res=1;
while(p>0)
{
if(p%2==1)
{
p--;
res*=a;
res%=mod;
}
else
{
a*=a;
a%=mod;
p/=2;
}
}
return res;
}*/
/*static class Pair
{
int u,v;
Pair(int u,int v)
{
this.u=u;
this.v=v;
}
}*/
/*static class Pair implements Comparable<Pair>
{
int v,l;
Pair(int v,int l)
{
this.v=v;
this.l=l;
}
public int compareTo(Pair p)
{
return l-p.l;
}
}*/
/*static long gcd(long a,long b)
{
if(b%a==0)
return a;
return gcd(b%a,a);
}
public static void dfs(int u,ArrayList<Integer> edge[],boolean vis[])
{
vis[u]=true;
for(int v:edge[u])
{
if(!vis[v])
dfs(v,edge,vis);
}
}
static class DSU
{
int par[],rank[],n;
DSU(int n)
{
this.n=n;
par=new int[n+1];
rank=new int[n+1];
for(int i=1;i<=n;i++)
par[i]=i;
}
public void union(int u,int v)
{
u=find(u);
v=find(v);
if(u==v)
return;
if(rank[u]>rank[v])
par[v]=u;
else if(rank[u]<rank[v])
par[u]=v;
else
{
rank[v]++;
par[u]=v;
}
}
public int find(int u)
{
if(u==par[u])
return u;
return par[u]=find(par[u]);
}
}
static class Edge
{
int v,ind;
long w;
Edge(int v,int w,int ind)
{
this.ind=ind;
this.v=v;
this.w=1L*w;
}
}
static class Pair implements Comparable<Pair>
{
int u,v;
Pair(int u,int v)
{
this.u=u;
this.v=v;
}
public int compareTo(Pair p)
{
if(this.u!=p.u)
return this.u-p.u;
return p.v-this.v;
}
}*/
public static void main(String args[])throws Exception
{
FastReader fs=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
//int tc=fs.nextInt();
int tc=1;
while(tc-->0)
{
int n=fs.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=fs.nextInt();
long ans=0;
for(int i=0;i<n;i+=2)
{
long s=a[i],prev=a[i];
for(int j=i+1;j<n;j++)
{
if(j%2==1)
{
if(j==i+1)
{
if(s<a[j])
{
ans+=s;
break;
}
else
{
s-=a[j];
ans+=a[j];
prev=s;
}
}
else
{
if(s-a[j]<0)
{
ans+=prev+1;
break;
}
if(s-a[j]<=prev)
{
ans+=(prev-s+a[j]+1);
s-=a[j];
prev=s;
}
else
s-=a[j];
}
}
else
s+=a[j];
}
}
pw.println(ans);
}
pw.flush();
pw.close();
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 358524ab3297f6d4250b53a6a23d1c93 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
PrintWriter out = new PrintWriter(System.out);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer("");
String next() throws IOException {
if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); }
return tok.nextToken();
}
int ni() throws IOException { return Integer.parseInt(next()); }
long nl() throws IOException { return Long.parseLong(next()); }
long mod=1000000007;
void solve() throws IOException {
int n=ni();
long ct=0;
long ans=0;
long currmin=0;
Stack<Long>S=new Stack();
Stack<Long>T=new Stack();
S.push(0L);
T.push(0L);
for (int i=0;i<n;i++) {
int u=ni();
if (i%2==0) ct+=u;
else {
ans+=Math.min(ct-currmin,u);
ct-=u;
//if (ct<0) ct=0;
while (!S.empty() && ct<S.peek()) {
S.pop();
ans+=T.pop();
}
if (S.empty()) {
S.push(ct);
T.push(0L);
currmin=ct;
}
else {
if (ct==S.peek()) {
long v=T.pop();
ans+=v;
T.push(v+1);
}
else {
S.push(ct);
T.push(1L);
}
}
}
}
out.println(ans);
out.flush();
}
int gcd(int a,int b) { return(b==0?a:gcd(b,a%b)); }
long gcd(long a,long b) { return(b==0?a:gcd(b,a%b)); }
long mp(long a,long p) { long r=1; while(p>0) { if ((p&1)==1) r=(r*a)%mod; p>>=1; a=(a*a)%mod; } return r; }
public static void main(String[] args) throws IOException {
new Main().solve();
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | c64815371cf15c9016d9177511e60883 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.Scanner;
/**
* @author Tay Qi Xiang
* @date 05/09/2021
*/
public class compressedBracketSequence {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
long ans = 0;
for (int i = 0; i < n; i += 2) {
long cur = 0, mn = 0;
for (int j = i + 1; j < n; j += 2) {
if (j - i > 1) {
mn = Math.min(mn, cur - a[j - 2]);
cur += a[j - 1] - a[j - 2];
}
ans += Math.max(-1, Math.min(a[i] + mn, a[j] + mn - cur)) + ((j - i > 1) ? 1 : 0);
}
}
System.out.println(ans);
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 6f1fa79236a5aadb37737e77da6e73f9 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | // Problem: Compressed Bracket Sequence
// https://codeforces.com/contest/1556/problem/C
// This submission:
import java.util.*;
public class CompressedBracketSequence {
private static class BracketPair {
private final long open, close;
private BracketPair(long openInput, long closeInput) {
open = openInput;
close = closeInput;
}
}
// The (input) brackets grouped into pairs of consecutive open brackets followed by consecutive
// close brackets.
private static ArrayList<BracketPair> v;
// Returns the number of subsegments that forms regular bracket sequences using brackets
// from `v`.
private static long solve() {
long result = 0;
// Keeps track of the amount of open brackets we can use at the beginning.
long open = 0;
for (int i = 0; i < v.size(); ++i) {
BracketPair p = v.get(i);
open += p.open;
if (open < p.close) {
// The sequence of `p.close` close brackets blocks any additional valid solutions that
// starts with the `open` brackets at the beginning.
result += open;
open = 0;
} else {
// open >= p.close >= 1
// For `(p.close - 1)` of the open brackets, they cannot be extended further (blocked
// by the `p.close` close brackets).
// One of the open bracket (starts with exactly `p.close` open brackets at the beginning)
// is ended by the `p.close` close brackets. This subsegment can stop (one configuration),
// or be further extended (`f(i + 1)` configurations).
result += (p.close - 1) + 1 + f(i + 1);
open -= p.close;
}
}
return result;
}
// Cache for function `f`.
// Memoization is beneficial because both `solve` and `f` calls `f`.
private static Long[] f;
// Returns the number of subsegments that starts at the beginning of `v[i, ]`
// (i.e., prefixes of brackets of `v[i, ]`) that forms regular bracket sequences.
private static long f(int i) {
if (i >= v.size()) {
return 0;
}
if (f[i] != null) {
return f[i];
}
long open = 0;
long close = 0;
for (int k = i; k < v.size(); ++k) {
open += v.get(k).open;
close += v.get(k).close;
if (open < close) {
// Further extension is blocked by the `p.close` close brackets.
return f[i] = 1L;
} else if (open == close) {
// We can stop with `close` close brackets (one configuration), or further extend
// (`f(k + 1)` configurations).
return f[i] = 1 + f(k + 1);
}
// There aren't enough close brackets yet. Go to the next bracket pair until we found
// enough close brackets.
}
return f[i] = 0L;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
v = new ArrayList<>();
// Note: We do not need to read the last segment of open brackets when `n` is odd.
// Those open brackets will not get matched in any regular bracket sequences.
for (int i = 0; i + 1 < n; i += 2) {
int open = in.nextInt();
int close = in.nextInt();
v.add(new BracketPair(open, close));
}
f = new Long[v.size()];
System.out.println(solve());
in.close();
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 1e5a5bfdb1b27785a056f45ad1f558b9 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | // Problem: Compressed Bracket Sequence
// https://codeforces.com/contest/1556/problem/C
import java.util.*;
public class CompressedBracketSequence {
private static class BracketPair {
private long open, close;
}
// Cache for the number of valid solutions.
// The element `cache.get(i)` stores the number of valid solutions using sequence `v[i, ]`
// with `key` additonal open brackets at the beginning.
private static ArrayList<Map<Long, Long>> cache;
private static ArrayList<BracketPair> v;
private static long f(int i, long open) {
if (i >= v.size()) {
return 0;
}
if (cache.get(i).containsKey(open)) {
return cache.get(i).get(open);
}
BracketPair p = v.get(i);
long result = 0;
if (open + p.open < p.close) {
result = open + p.open;
} else if (p.close >= 1) {
// open + p.open >= p.close >= 1
result = (p.close - 1) + 1 + g(i + 1);
}
result += f(i + 1, Math.max(0, open + p.open - p.close));
cache.get(i).put(open, result);
return result;
}
private static Long[] g;
private static long g(int i) {
if (i >= v.size()) {
return 0;
}
if (g[i] != null) {
return g[i];
}
BracketPair p = v.get(i);
long result = 0;
if (p.open < p.close) {
result = 1;
} else if (p.open == p.close) {
result = 1 + g(i + 1);
} else {
// p.open > p.close
long open = p.open - p.close;
for (int j = i + 1; j < v.size(); ++j) {
open += v.get(j).open;
if (open < v.get(j).close) {
result = 1;
break;
} else if (open > v.get(j).close) {
open -= v.get(j).close;
} else {
// open == v.get(j).close
result = 1 + g(j + 1);
break;
}
}
}
return g[i] = result;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
v = new ArrayList<>();
BracketPair current = new BracketPair();
for (int i = 0; i < n; ++i) {
int x = in.nextInt();
if (i % 2 == 0) {
// Open brackets.
if (current.close > 0) {
v.add(current);
current = new BracketPair();
}
current.open += x;
} else {
// Close brackets.
current.close += x;
}
}
v.add(current);
cache = new ArrayList<>();
for (int i = 0; i < v.size(); ++i) {
cache.add(new HashMap<>());
}
g = new Long[v.size()];
System.out.println(f(0, 0));
in.close();
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 5decf31f362cb3862667bbe5cf502a29 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
public class Sol{
/*
->check n=1, int overflow , array bounds , all possibilites(dont stuck on 1 approach)
->Problem = Observation(constraints(m<=n/3 or k<=min(100,n))
+ Thinking + Technique (seg_tree,binary lift,rmq,bipart,dp,connected comp etc)
->solve or leave it (- tutorial improves you in minimal way -)
*/
public static void main (String []args) {
//precomp();
int times=1;while(times-->0){solve();}out.close();}
static void solve(){
int n=ni();
long a[]=new long[n+1];
for(int i=1;i<=n;i++)a[i]=ni();
long ans=0;
for(int i=1;i<=n;i++){
if(i%2==1){
long r=a[i];
for(int j=i+1;j<=n&&r>=0;j++){
if(j%2==0){
long delta=r-a[i];
long curr=a[j];
if(delta>a[j]){}
else{
if(delta>0){ans++;curr-=delta;}
ans+=Math.min(a[i],curr);
a[i]-=curr;
}
r-=a[j];
}
else{r+=a[j];}//out.println(i+" "+j+" "+ans);
}
}
}
out.println(ans);
return;
}
//-----------------Utility--------------------------------------------
static long gcd(long a,long b){if(b==0)return a; return gcd(b,a%b);}
static int Max=Integer.MAX_VALUE; static long mod=1000000007;
//static int v(char c){return (int)(c-'a');}
public static long power(long x, long y )
{
//0^0 = 1
long res = 1L;
x = x%mod;
while(y > 0)
{
if((y&1)==1)
res = (res*x)%mod;
y >>= 1;
x = (x*x)%mod;
}
return res;
}
static class Pair implements Comparable<Pair>{
int id;int value;Pair next;
public Pair(int id,int value) {
this.id=id;this.value=value;next=null;
}
@Override
public int compareTo(Pair p){return Long.compare(value,p.value);}
}
//----------------------I/O---------------------------------------------
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static FastReader in=new FastReader(inputStream);
static PrintWriter out=new PrintWriter(outputStream);
static class FastReader
{
BufferedReader br;
StringTokenizer st;
FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
public String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
/*static int ni() {
try {
boolean in = false;
int res = 0;
for (;;) {
int b = System.in.read() - '0';
if (b >= 0) {
in = true;
res = 10 * res + b;
}
else if (in)
return res;
}
} catch (IOException e) {
throw new Error(e);
}
}*/
static int ni(){return in.nextInt();}
static long nl(){return in.nextLong();}
static double nd(){return in.nextDouble();}
static String ns(){return in.nextLine();}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | f4590b69c633dd6e6829264969c0e0fe | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | //package codechef;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
public class cp_2 {
static int mod=(int)1e9+7;
// static Reader sc=new Reader();
static FastReader sc=new FastReader(System.in);
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//FastReader sc=new FastReader();
// int t=sc.nextInt();
// while(t-->0)
// {
int n=sc.nextInt();
long[] arr=new long[n];
for (int i = 0; i < arr.length; i++) {
arr[i]=sc.nextLong();
}
long ans=0;
for(int i=1;i<n;i+=2)
{
long extra=0,c=arr[i-1];
for(int j=i;j<n && c>=0;j+=2)
{
if(j>i)
extra+=arr[j-1];
if(extra<=arr[j])
{
ans+=Math.min(c, arr[j]-extra);
if(extra<=arr[j] && c!=arr[i-1])
ans++;
}
extra-=arr[j];
if(extra<0)
{
c+=extra;
extra=0;
}
}
}
out.println(ans);
// }
out.flush();
out.close();
System.gc();
}
/*
...SOLUTION ENDS HERE...........SOLUTION ENDS HERE...
*/
static void swap(int arr[],int i,int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
static boolean check(String num,String x)
{
int i=0,pos=0;
while(i<num.length() && pos<x.length())
{
if(num.charAt(i)==x.charAt(pos))
pos++;
i++;
}
if(pos==x.length())
return true;
return false;
}
static boolean util(int a,int b,int c)
{
if(b>a)util(b, a, c);
while(c>=a)
{
c-=a;
if(c%b==0)
return true;
}
return (c%b==0);
}
static boolean check(int arr[])
{
for (int i = 1; i < arr.length; i++) {
if(arr[i-1]>arr[i])
return false;
}
return true;
}
static void flag(boolean flag)
{
out.println(flag ? "YES" : "NO");
out.flush();
}
static ArrayList<Long> luckNums;
static void luckyNum(long x,long p10)
{
luckNums.add(x);
if(x>(long)1e10)
return;
luckyNum(x+4*p10, p10*10);
luckyNum(x+7*p10, p10*10);
}
/*
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> it = set.iterator();
while(it.hasNext())
{
int x=it.next();
}
*/
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 int LowerBound(int a[], int x)
{ // x is the target value or key
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 lowerIndex(int arr[], int n, int x)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] >= x)
h = mid - 1;
else
l = mid + 1;
}
return l;
}
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 class DisjointUnionSets
{
int[] rank, parent;
int n;
// Constructor
public DisjointUnionSets(int n)
{
rank = new int[n];
parent = new int[n];
this.n = n;
makeSet();
}
// Creates n sets with single item in each
void makeSet()
{
for (int i = 0; i < n; i++)
parent[i] = i;
}
int find(int x)
{
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
// Unites the set that includes x and the set
// that includes x
void union(int x, int y)
{
int xRoot = find(x), yRoot = find(y);
if (xRoot == yRoot)
return;
if (rank[xRoot] < rank[yRoot])
parent[xRoot] = yRoot;
else if (rank[yRoot] < rank[xRoot])
parent[yRoot] = xRoot;
else // if ranks are the same
{
parent[yRoot] = xRoot;
rank[xRoot] = rank[xRoot] + 1;
}
}
}
// static class countClass{
// int cnt;
// public countClass() {
// this.cnt=0;
// // TODO Auto-generated constructor stub
// }
// }
static class Graph
{
int v;
ArrayList<Integer> list[];
Graph(int v)
{
this.v=v;
list=new ArrayList[v+1];
for(int i=1;i<=v;i++)
list[i]=new ArrayList<Integer>();
}
void addEdge(int a, int b)
{
this.list[a].add(b);
}
}
// static class GraphMap{
// Map<String,ArrayList<String>> graph;
// GraphMap() {
// // TODO Auto-generated constructor stub
// graph=new HashMap<String,ArrayList<String>>();
//
// }
// void addEdge(String a,String b)
// {
// if(graph.containsKey(a))
// this.graph.get(a).add(b);
// else {
// this.graph.put(a, new ArrayList<>());
// this.graph.get(a).add(b);
// }
// }
// }
// static void dfsMap(GraphMap g,HashSet<String> vis,String src,int ok)
// {
// vis.add(src);
//
// if(g.graph.get(src)!=null)
// {
// for(String each:g.graph.get(src))
// {
// if(!vis.contains(each))
// {
// dfsMap(g, vis, each, ok+1);
// }
// }
// }
//
// cnt=Math.max(cnt, ok);
// }
static double sum[];
static double cnt;
static void DFS(Graph g, boolean[] visited, int u,double path)
{
visited[u]=true;
sum[u]=0;
int k=0;
for(int i=0;i<g.list[u].size();i++)
{
int v=g.list[u].get(i);
if(!visited[v])
{
DFS(g, visited, v,path+1);
sum[u]+=sum[v];
k++;
}
}
if(k!=0)
sum[u]=sum[u]/(double)k +1;
}
static class Pair
{
int x,y;
Pair(int x,int y)
{
this.x=x;
this.y=y;
}
}
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 modInverse(long a, long m)
{
long g = gcd(a, 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 = (int)((p * (long)p) % m);
if (y % 2 == 0)
return p;
else
return (int)((x * (long)p) % m);
}
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;
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | fe80c2bce8eff5616b481e0a335ac097 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | /*
ID: abdelra29
LANG: JAVA
PROG: zerosum
*/
/*
TO LEARN
2-euler tour
*/
/*
TO SOLVE
*/
/*
bit manipulation shit
1-Computer Systems: A Programmer's Perspective
2-hacker's delight
*/
/*
TO WATCH
*/
import java.util.*;
import java.math.*;
import java.io.*;
import java.util.stream.Collectors;
public class A{
static FastScanner scan=new FastScanner();
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
static boolean isprime(int x)
{
if(x%2==0)
return false;
if(x==1)
return false;
if(x==2||x==3)
return true;
for(int i=3;i*i<=x;i+=2)
if(x%i==0)
return false;
return true;
}
static int hidden[]={0,181,21,53};
static Pair askand(int x)
{
System.out.println("and "+x+" "+(x-1));
//int fi=scan.nextInt();
int fi=hidden[x]&hidden[x-1];
System.out.println("and "+x+" "+(x-2));
int sec=hidden[x]&hidden[x-2];
//int sec=scan.nextInt();
return new Pair(fi,sec);
}
static Pair askor(int x)
{
System.out.println("or "+x+" "+(x-1));
//int fi=scan.nextInt();
int fi=hidden[x]|hidden[x-1];
System.out.println("or "+x+" "+(x-2));
int sec=hidden[x]|hidden[x-2];
//int sec=scan.nextInt();
return new Pair(fi,sec);
}
static int binaryToDecimal(String n)
{
String num = n;
int dec_value = 0;
// Initializing base value to 1,
// i.e 2^0
int base = 1;
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
if (num.charAt(i) == '1')
dec_value += base;
base = base * 2;
}
return dec_value;
}
static boolean areBracketsBalanced(String expr)
{
// Using ArrayDeque is faster than using Stack class
Deque<Character> stack
= new ArrayDeque<Character>();
// Traversing the Expression
for (int i = 0; i < expr.length(); i++)
{
char x = expr.charAt(i);
if (x == '(' || x == '[' || x == '{')
{
// Push the element in the stack
stack.push(x);
continue;
}
// If current character is not opening
// bracket, then it must be closing. So stack
// cannot be empty at this point.
if (stack.isEmpty())
return false;
char check;
switch (x) {
case ')':
check = stack.pop();
if (check == '{' || check == '[')
return false;
break;
case '}':
check = stack.pop();
if (check == '(' || check == '[')
return false;
break;
case ']':
check = stack.pop();
if (check == '(' || check == '{')
return false;
break;
}
}
// Check Empty Stack
return (stack.isEmpty());
}
static long brute(int n,long arr[])
{
StringBuilder res=new StringBuilder("");
for(int i=0;i<n;i++)
{
if(i%2==0)
{
for(int j=0;j<arr[i];j++)
res.append("(");
}
else {
for(int j=0;j<arr[i];j++)
res.append(")");
}
}
long ans=0;
// System.out.println(res.substring(0,1));
for(int i=0;i<res.length();i++)
{
for(int j=i+1;j<res.length();j++)
{
if(areBracketsBalanced(res.substring(i,j+1).toString()))
{
System.out.println(i+" "+j);
ans++;
}
}
}
return ans;
}
public static void main(String[] args) throws Exception
{
/*
very important tips
1-just fucking think backwards once in your shitty life
2-consider brute forcing and finding some patterns and observations
3-when you're in contest don't get out because you think there is no enough time
4-don't get stuck on one approach
*/
// File file=new File("D:\\input\\out.txt");
// scan=new FastScanner("D:\\input\\xs_and_os_input.txt");
// out = new PrintWriter(new File("D:\\input\\out.txt"));
/*
READING
3-Introduction to DP with Bitmasking codefoces
4-Bit Manipulation hackerearth
5-read more about mobious and inculsion-exclusion
*/
/*
1-
*/
/*for(int i=3;i<=30;i++)
{
int sh=(i%3==0)?i/3:i/3+1;
System.out.println(i*2+" "+(sh*4));
}*/
int tt =1;
//tt=scan.nextInt();
int T=1;
outer:while(tt-->0)
{
int n=scan.nextInt();
if(n%2==1)
n--;
long arr[]=new long[n];
for(int i=0;i<n;i++)
arr[i]=scan.nextLong();
// brute(n,arr);
/* StringBuilder sb=new StringBuilder("");
for(int i=0;i<n;i++)
{
if(i%2==0)
{
for(int j=0;j<arr[i];j++)
sb.append("(");
}
else {
for(int j=0;j<arr[i];j++)
sb.append(")");
}
}*/
// System.out.println(sb);
long res=0;
for(int i=0;i<n;i+=2)
{
//start from i to j
long cur=0,to=arr[i];
for(int j=i+1;j<n;j+=2)
{
if(j==i+1)
{
res+=Math.min(arr[i],arr[j]);
to-=arr[j];
if(arr[i]<arr[j])
break;
//cur+=arr[i]-arr[j];
}
else
{
cur+=arr[j-1];
long tmpa=arr[j];
if(cur<=arr[j]){
res++;
}
long rem=arr[j]-cur;
if(rem<0){
cur-=Math.min(cur,arr[j]);
continue;
}
cur-=Math.min(cur,arr[j]);
res+=Math.min(rem,to);
if(to<rem)
break;
to-=rem;
}
}
//out.println("RES "+res);
}
out.println(res);
}
out.close();
}
static class special implements Comparable<special>{
int cnt,idx;
String s;
public special(int cnt,int idx,String s)
{
this.cnt=cnt;
this.idx=idx;
this.s=s;
}
@Override
public int hashCode() {
return (int)42;
}
@Override
public boolean equals(Object o){
// System.out.println("FUCK");
if (o == this) return true;
if (o.getClass() != getClass()) return false;
special t = (special)o;
return t.cnt == cnt && t.idx == idx;
}
public int compareTo(special o1)
{
if(o1.cnt==cnt)
{
return o1.idx-idx;
}
return o1.cnt-cnt;
}
}
static long binexp(long a,long n)
{
if(n==0)
return 1;
long res=binexp(a,n/2);
if(n%2==1)
return res*res*a;
else
return res*res;
}
static long powMod(long base, long exp, long mod) {
if (base == 0 || base == 1) return base;
if (exp == 0) return 1;
if (exp == 1) return (base % mod+mod)%mod;
long R = (powMod(base, exp/2, mod) % mod+mod)%mod;
R *= R;
R %= mod;
if ((exp & 1) == 1) {
return (base * R % mod+mod)%mod;
}
else return (R %mod+mod)%mod;
}
static double dis(double x1,double y1,double x2,double y2)
{
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
static long mod(long x,long y)
{
if(x<0)
x=x+(-x/y+1)*y;
return x%y;
}
public static long pow(long b, long e) {
long r = 1;
while (e > 0) {
if (e % 2 == 1) r = r * b ;
b = b * b;
e >>= 1;
}
return r;
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int object : arr) list.add(object);
Collections.sort(list);
//Collections.reverse(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private static void sort2(long[] arr) {
List<Long> list = new ArrayList<>();
for (Long object : arr) list.add(object);
Collections.sort(list);
Collections.reverse(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
static class FastScanner
{
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
static class Pair implements Comparable<Pair>{
public long x, y,z;
public Pair(long x1, long y1,long z1) {
x=x1;
y=y1;
z=z1;
}
public Pair(long x1, long y1) {
x=x1;
y=y1;
}
@Override
public int hashCode() {
return (int)(x + 31 * y);
}
public String toString() {
return x + " " + y+" "+z;
}
@Override
public boolean equals(Object o){
if (o == this) return true;
if (o.getClass() != getClass()) return false;
Pair t = (Pair)o;
return t.x == x && t.y == y&&t.z==z;
}
public int compareTo(Pair o)
{
return (int)(x-o.x);
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | ae2339228a2482f2a78c7e716b9c82db | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Practice
{
static int mod=1000000007;
static HashSet<Integer> pr;
static FastReader sc=new FastReader(System.in);
// static Reader sc=new Reader();
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args) throws IOException
{
int t=1;
// t=sc.nextInt();
// StringBuilder sb=new StringBuilder();
while(t-->0)
{
solve();
// sb.append("\n");
}
// out.print(sb);
out.close();
out.flush();
}
static class Pair
{
int x;
int y;
Pair(int a,int b)
{
x=a;
y=b;
}
@Override
public String toString()
{
return x+" "+y;
}
}
static void arraySort(int arr[])
{
ArrayList<Integer> a=new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
a.add(arr[i]);
}
Collections.sort(a);
for (int i = 0; i < arr.length; i++) {
arr[i]=a.get(i);
}
}
static void solve() throws IOException
{
int n=sc.nextInt();
n=n-(n&1);
long arr[]=new long[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
long ans=0;
for(int i=0;i<n;i+=2) //opening brackets
{
long extra=0,c=0;
for(int j=i+1;j<n && arr[i]>=0;j+=2) //closing brackets
{
// out.println(i+" "+j+" "+arr[i]+" "+extra+" "+ans);
if(extra>arr[j])
{
extra-=arr[j];
if(j+1<n)
extra+=arr[j+1];
continue;
}
long toClose=Math.min(arr[i], arr[j]-extra);
ans+=(toClose+c);
if(arr[j]-extra>toClose)
break;
arr[i]-=toClose;
if(j+1<n)
extra=arr[j+1];
c=1;
// out.println(i+" "+j+" "+arr[i]+" "+extra+" "+ans);
}
}
out.println(ans);
}
static double dfs(Node ver,Node par)
{
if(ver.adj.size()==1 && ver.adj.get(0)==par)
{
return 0;
}
else
{
double sum=0,c=0;
for(Node child: ver.adj)
if(child!=par)
{
c++;
sum+=dfs(child, ver);
}
return 1+(sum/c);
}
}
static int LOWER_BOUND(ArrayList<Integer> arr,int x)
{
int low=0,high=arr.size();
int ans=0;
while(low<=high)
{
int mid=(low+high)/2;
if(arr.get(mid)==x)
{
ans=arr.get(mid);
break;
}
else if(arr.get(mid)<x)
{
ans=arr.get(mid);
low=mid+1;
}
else
high=mid-1;
}
return ans;
}
static int gcd(int a,int b)
{
return (b==0)?a:gcd(b,a%b);
}
static class UnionFind
{
private int size;
private int[] sz;
private int[] parent;
private int numComponents;
public UnionFind(int size,Pair dancers[])
{
if (size <= 0) throw new IllegalArgumentException("Size <= 0 is not allowed");
this.size = numComponents = size;
sz = new int[size];
parent = new int[size];
for (int i = 0; i < size; i++)
{
parent[i] = i; // Link to itself (self root)
sz[i] = 1; // Each component is originally of size one
}
}
public int find(int p)
{
int root = p;
while (root != parent[root])
root = parent[root];
while (p != root)
{
int next = parent[p];
parent[p] = root;
p = next;
}
return root;
}
public boolean isConnected(int p, int q)
{
return find(p) == find(q);
}
public int componentSize(int x)
{
return sz[find(x)];
}
public int size()
{
return size;
}
public int components()
{
return numComponents;
}
public void unify(int p, int q)
{
int root1 = find(p);
int root2 = find(q);
if(root1==root2)
{
return;
}
else if (sz[root1] < sz[root2])
{
parent[root1] = root2;
sz[root2] += sz[root1];
sz[root1] = 0;
}
else
{
parent[root2] = root1;
sz[root1] += sz[root2];
sz[root2] = 0;
}
numComponents--;
}
void calc()
{
for(int i=0;i<size;i++)
find(i);
}
}
static class Node
{
int vertex;
ArrayList<Node> adj;
boolean rec;
boolean vis;
int color;
Node(int v)
{
vertex=v;
adj=new ArrayList<Node>();
vis=false;
color=-1;
}
@Override
public String toString()
{
return vertex+" ";
}
}
static class Edge
{
Node a;
Node b;
Edge(Node m,Node n)
{
this.a=m;
this.b=n;
}
@Override
public String toString() {
return a.vertex+" "+b.vertex;
}
}
static long power(long x, long y)
{
long res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % mod;
y = y >> 1; // y = y/2
x = (x * x) % mod;
}
return res;
}
static long binomialCoeff(long n, long k)
{
long res = 1;
if(k>n)
return 0;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (long i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
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') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
public void printarray(int arr[])
{
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 70169c71a621867ea4091d412ba3c695 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
/**
* @author Mubtasim Shahriar
*/
public class CompressedBracSeq {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
// int t = sc.nextInt();
int t = 1;
while (t-- != 0) {
solver.solve(sc, out);
}
out.close();
}
static class Solver {
public void solve(InputReader sc, PrintWriter out) {
int n = sc.nextInt();
long ans = 0;
long[] arr = sc.nextLongArray(n);
for(int i = 0; i < n; i += 2) {
if(i==n-1) break;
long firstleft = arr[i];
long firstright = arr[i+1];
long firstmin = Math.min(firstleft,firstright);
ans += firstmin;
firstleft -= firstmin;
firstright -= firstmin;
if(firstright>0) continue;
long laterleft = 0;
for(int j = i+2; j < n; j++) {
if(j%2==0) {
laterleft += arr[j];
} else {
long right = arr[j];
long min = Math.min(laterleft,right);
if(right>=laterleft) {
ans++;
}
laterleft -= min;
right -= min;
if(right>0 && firstleft==0) break;
if(right>laterleft) {
min = Math.min(firstleft,right);
ans += min;
firstleft -= min;
right -= min;
if(firstleft==0 && right>0) break;
}
}
}
}
out.println(ans);
}
}
static void sort(int[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i) continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
}
static void sort(long[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i) continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
}
static void sortDec(int[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i) continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
int l = 0;
int r = n - 1;
while (l < r) {
arr[l] ^= arr[r];
arr[r] ^= arr[l];
arr[l] ^= arr[r];
l++;
r--;
}
}
static void sortDec(long[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i) continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
int l = 0;
int r = n - 1;
while (l < r) {
arr[l] ^= arr[r];
arr[r] ^= arr[l];
arr[l] ^= arr[r];
l++;
r--;
}
}
static class InputReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
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 long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
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;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public BigInteger readBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public int[] nextSortedIntArray(int n) {
int array[] = nextIntArray(n);
Arrays.sort(array);
return array;
}
public int[] nextSumIntArray(int n) {
int[] array = new int[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; ++i) array[i] = nextLong();
return array;
}
public long[] nextSumLongArray(int n) {
long[] array = new long[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextSortedLongArray(int n) {
long array[] = nextLongArray(n);
Arrays.sort(array);
return array;
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 5c14ea8b3ae7d5f24b75123eb7cac48b | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
public class Deltix {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int t = 1;
while (t-- > 0) {
int n = sc.nextInt();
long [] c = new long[n];
for (int i = 0; i < n; i++) c[i] = sc.nextLong();
long res = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 1) continue;
long in = 0;
long low = 1;
// impose at least 1 from each
for (int j = i + 1; j < n; j++) {
if (j % 2 == 1) {
// in positive means more left
if (in == 0) {
res += Math.max(0, Math.min(c[i], c[j]) - low + 1);
} else {
res += ans(c[i], c[j], -in, low);
}
}
if (j % 2 == 1) {
low = Math.max(low, c[j] - in);
in -= c[j];
} else {
in += c[j];
}
}
}
out.println(res);
}
out.close();
}
static long ans(long a, long b, long dif, long minA) {
if (minA > a) return 0;
if (dif > 0) {
// a ranges from minA to a
// b ranges from 1 to b
// want to create dif of dif
long start = Math.max(minA, dif + 1);
long end = Math.min(a, b + dif);
return Math.max(0, end - start + 1);
} else {
dif = -dif;
long start = minA;
long end = Math.min(a, b - dif);
return Math.max(0, end - start + 1);
}
}
static void sort(int[] a) {
ArrayList<Integer> q = new ArrayList<>();
for (int i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
static void sort(long[] a) {
ArrayList<Long> q = new ArrayList<>();
for (long i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
//-----------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 | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 6a5df8b6300e5c257d21e5c0727d6078 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.List;
import java.util.*;
public class realfast implements Runnable
{
private static final int INF = (int) 1e9;
long in= 1000000007;
long fac[]= new long[1000001];
long inv[]=new long[1000001];
public void solve() throws IOException
{
int n = readInt();
int arr[]=new int[n+1];
for(int i =0;i<n;i++)
arr[i]= readInt();
long ans=0;
for(int i =0;i<n;i=i+2)
{
long sum = 1;
long extra =arr[i]-1;
for(int j=i+1;j<n;j++)
{
if(j%2==1)
{
if(arr[j]>=sum)
{
ans++;
ans= ans+Math.min(extra,arr[j]-sum);
}
sum= sum-arr[j];
if(sum<0)
{
if(Math.abs(sum)>extra)
{
break;
}
extra = extra - Math.abs(sum);
sum=0;
}
}
else
{
sum= sum+arr[j];
}
}
}
out.println(ans);
}
public long query(long seg[] , int left, int right , int index, int l , int r)
{
long inf = 100000000;
inf = inf*inf;
if(left>=l&&right<=r)
{
return seg[index];
}
if(l>right||left>r)
return inf;
int mid = left+(right-left)/2;
return Math.min(query(seg,left,mid,2*index+1,l,r),query(seg,mid+1,right,2*index+2,l,r));
}
public int value (int seg[], int left , int right ,int index, int l, int r)
{
if(left>right)
{
return -100000000;
}
if(right<l||left>r)
return -100000000;
if(left>=l&&right<=r)
return seg[index];
int mid = left+(right-left)/2;
int val = value(seg,left,mid,2*index+1,l,r);
int val2 = value(seg,mid+1,right,2*index+2,l,r);
return Math.max(val,val2);
}
public int gcd(int a , int b )
{
if(a<b)
{
int t =a;
a=b;
b=t;
}
if(a%b==0)
return b ;
return gcd(b,a%b);
}
public long pow(long n , long p,long m)
{
if(p==0)
return 1;
long val = pow(n,p/2,m);;
val= (val*val)%m;
if(p%2==0)
return val;
else
return (val*n)%m;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
new Thread(null, new realfast(), "", 128 * (1L << 20)).start();
}
private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
private BufferedReader reader;
private StringTokenizer tokenizer;
private PrintWriter out;
@Override
public void run() {
try {
if (ONLINE_JUDGE || !new File("input.txt").exists()) {
reader = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
reader = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
solve();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
reader.close();
} catch (IOException e) {
// nothing
}
out.close();
}
}
private String readString() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
@SuppressWarnings("unused")
private int readInt() throws IOException {
return Integer.parseInt(readString());
}
@SuppressWarnings("unused")
private long readLong() throws IOException {
return Long.parseLong(readString());
}
@SuppressWarnings("unused")
private double readDouble() throws IOException {
return Double.parseDouble(readString());
}
}
class edge implements Comparable<edge>{
int u ;
int v;
edge(int u, int v)
{
this.u=u;
this.v=v;
}
public int compareTo(edge e)
{
return this.v-e.v;
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 26d8229276589e617683247d9442dd07 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
public class DeltixRound {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////// /////////
//////// /////////
//////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE /////////
//////// HHHH HHHH EEEEEEEEEEEEE MMMMMM MMMMMM OOO OOO SSSS SSS EEEEEEEEEEEEE /////////
//////// HHHH HHHH EEEEE MMMM MMM MMM MMMM OOO OOO SSSS SSS EEEEE /////////
//////// HHHH HHHH EEEEE MMMM MMMMMM MMMM OOO OOO SSSS EEEEE /////////
//////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSSSSS EEEEE /////////
//////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSS EEEEEEEEEEE /////////
//////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSSS EEEEEEEEEEE /////////
//////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSS EEEEE /////////
//////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSS SSSS EEEEE /////////
//////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOO OOO SSS SSSS EEEEEEEEEEEEE /////////
//////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE /////////
//////// /////////
//////// /////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = 1;
while (t-- > 0) {
int n = sc.nextInt();
int[] a = sc.nextIntArr(n);
long ans = 0;
for (int i = 0; i < n; i += 2) {
long balnce = 0;
long needclosed = 0;
for (int j = i + 1; j < n; j++) {
if (needclosed > a[i])
break;
if (j % 2 == 1) {
long rem = a[i] - needclosed;
long rem2 = a[j] - balnce;
if (needclosed > 0 && balnce > 0 && rem >= 0 && rem2 >= 0)
ans++;
if (rem > 0 && rem2 > 0)
ans += Math.min(rem, rem2);
balnce -= a[j];
if (balnce < 0) {
needclosed += Math.abs(balnce);
balnce = 0;
}
} else {
balnce += a[j];
}
}
}
pw.println(ans);
}
pw.flush();
}
static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int[] nextIntArr(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 5db8d59eb8f88d8f9beaccb6bedc263b | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Solution {
public static long gcd(long a,long b) {
if(a==0) return b;
return gcd(b%a,a);
}
public static long lcm(long a,long b) {
return (a*b)/gcd(a,b);
}
public static long [] input(BufferedReader br,int n) throws java.lang.Exception{
long ans[]=new long[n];
String input[]=br.readLine().split(" ");
for(int i=0;i<n;i++) {
ans[i]=Long.parseLong(input[i]);
}
return ans;
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
// int testCases=Integer.parseInt(br.readLine());
int testCases=1;
while(testCases-->0) {
int n=(int)input(br,1)[0];
long arr[]=input(br,n);
long ct=0;
for(int i=0;i<n;i+=2) {
int k=1;
long bClose=0;
long bOpen=0;
long count=0;
for(int j=i+1;j<n;j++) {
if(k%2==0) bOpen+=arr[j];
else {
if(bClose<=arr[i]&&bOpen<=arr[j]) {
if(bClose==arr[i]||bOpen==arr[j]) count++;
else {
count+=Math.min(arr[i]-bClose, arr[j]-bOpen);
if(bClose>0&&bOpen>0) count++;
}
}
long b=arr[j];
long min=Math.min(bOpen,b);
bOpen-=min;
b-=min;
bClose+=b;
}
k++;
}
ct+=count;
}
out.println(ct);
}
out.close();
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 862472e16f79c1496c3e5b569ddc9cf6 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution
{
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
static int sum(int idx , int bit[])
{
int sum = 0;
while(idx > 0)
{
sum += bit[idx];
idx -= idx&(-idx);
}
return sum;
}
static void update(int n , int idx , int val , int bit[])
{
while(idx <= n)
{
bit[idx] += val;
idx += idx&(-idx);
}
}
static long mod = 1000000007;
static long pow(long a , long b)
{
if(b == 0)
return 1;
long ans = pow(a,b/2);
if(b%2 == 0)
return ans*ans%mod;
return ans*ans%mod*a%mod;
}
static long f(long v)
{
if(v <= 0)
return 0;
return (v*(v+1)/2);
}
public static void main(String []args) throws IOException
{
Reader sc = new Reader();
int n = sc.nextInt();
int arr[] = new int[n];
long ans = 0;
// long open = 0;
for(int i = 0 ; i < n ; i++)
{
arr[i] = sc.nextInt();
}
for(int i = 0 ; i < n ; i += 2)
{
//long open = arr[i];
long x = 0;
//long min = 0;
long open = arr[i];
for(int j = i+1 ; j < n ; j++)
{
if(open < 0)
break;
if(j%2 == 0)
{
x += arr[j];
// open += arr[j];
}
else
{
long y = arr[j];
y -= x;
if(y >= 0)
{
long last = ans;
if(i+1 == j)
ans += Math.min(open,y);
else
ans += Math.min(open,y)+1;
//System.out.println((ans-last) + " " + i + " " + j);
}
x -= arr[j];
if(x < 0)
{
open += x;
x = 0;
}
}
}
}
System.out.println(ans);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 45468d6c5efe02d0e4d4f8148c6c8631 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes |
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = Integer.parseInt(sc.nextLine());
String secondLine[] = sc.nextLine().trim().split("\\s+");
int arr[] = new int[N];
for (int index = 0; index < N; index++) {
arr[index] = Integer.parseInt(secondLine[index]);
}
long result = 0;
for (int openInd = 0; openInd < N; openInd += 2) {
long min = arr[openInd], curr = 0;
for (int closeInd = openInd + 1; closeInd < N; closeInd += 2) {
result += Math.max(0L, Math.min(min, curr + arr[closeInd - 1] - 1)
- Math.max(0L, curr + arr[closeInd - 1] - arr[closeInd]) + 1);
curr += arr[closeInd - 1] - arr[closeInd];
min = Math.min(min, curr);
}
}
System.out.println(result);
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 1ab24b20e7acf73fd76cc067e4e1d052 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | // ----------------------------------- Duniya Madarchod Hai -----------------------------------
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
/*
public String wrd() throws IOException{return(read.next());}
public int ni() throws IOException{return(read.nextInt());}
public double nd() throws IOException{return(read.nextDouble());}
public long nl() throws IOException{return(read.nextLong());}
public int[] ai(int n) throws IOException{int arr[] = new int[n];for(int i = 0; i<n; i++)arr[i] = read.nextInt();return(arr);}
public double[] ad(int n) throws IOException
{double arr[] = new double[n];for(int i = 0; i<n; i++)arr[i] = read.nextDouble();return(arr);}
public long[] al(int n) throws IOException
{long arr[] = new long[n];for(int i = 0; i<n; i++)arr[i] = read.nextLong();return(arr);}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
if (System.getProperty("ONLINE_JUDGE") == null)
{
try
{
InputStream inputStream = new FileInputStream("input.txt");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
br = new BufferedReader(inputStreamReader);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
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;}}
*/
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);
if (System.getProperty("ONLINE_JUDGE") == null)
{
try
{
din = new DataInputStream(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
}
catch (Exception e)
{
e.printStackTrace();
}
}
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public String readLine() throws IOException{return(read.readLine());}
public int ni() throws IOException{return(read.nextInt());}
public double nd() throws IOException{return(read.nextDouble());}
public long nl() throws IOException{return(read.nextLong());}
public static void main(String[] args) throws IOException{Main obj = new Main();obj.solve();}
long gcd(long a, long b)
{
while (b != 0)
{
long t = a;
a = b;
b = t % b;
}
return a;
}
long getSum(long arr[], int start, int end)
{
if(start == 0)
return arr[end];
return(arr[end] - arr[start-1]);
}
long MODULO = 1000*1000l*1000l + 7l;
StringBuilder sb;
Reader read;
//FastReader read;
int count = 0;
public void solve() throws IOException
{
//Scanner sn = new Scanner(System.in);
sb = new StringBuilder();
//read = new FastReader();
read = new Reader();
int tt = 1;
for(int tk = 0; tk < tt; tk++)
{
int n = ni();
long arr[] = new long[n];
for(int i = 0; i<n; i++)
arr[i] = nl();
long mod[] = new long[n];
for(int i = 0; i<n; i++)
{
if(i % 2 == 0)
mod[i] = arr[i];
else
mod[i] = - arr[i];
}
long prefixSum[] = new long[n];
prefixSum[0] = mod[0];
for(int i = 1; i<n; i++)
prefixSum[i] = prefixSum[i-1] + mod[i];
//System.out.println(Arrays.toString(mod));
long rmq[][] = new long[n][n];
for(int start = 0; start < n; start++)
{
long min = Long.MAX_VALUE;
long sum = 0;
for(int end = start; end < n; end++)
{
sum += mod[end];
min = Math.min(min, sum);
rmq[start][end] = min;
}
}
//System.out.println(rmq[3][4]);
long count = 0;
for(int start = 0; start < n; start+=2)
{
for(int end = start+1; end < n; end+=2)
{
// Special case
if(end == start+1)
{
long temp = Math.min(arr[start], arr[end]);
//System.out.println("START = "+start+" ENDING = "+end+" ADDING = "+temp);
count += temp;
continue;
}
long minReq = (long) Math.abs(rmq[start+1][end-1]);
long option1 = Math.max(0, arr[start] - minReq + 1);
long last = getSum(prefixSum, start+1, end-1) + minReq;
long option2 = Math.max(0, arr[end] - last + 1);
long temp = Math.min(option1, option2);
//System.out.println("START = "+start+" ENDING = "+end+" MINREQ = " +minReq+" LAST = "+last + " ADDING = "+temp);
count += temp;
//if(op1 >= 0 && op2 >= 0)
// count++;
}
}
sb.append(count);
}
System.out.println(sb);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | e14b9b07cada8d91e4288880b0c65850 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//int cases = Integer.parseInt(br.readLine());
//o:while(cases-- > 0) {
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int[] c = new int[n];
str = br.readLine().split(" ");
for(int i=0; i<n; i++) {
c[i] = Integer.parseInt(str[i]);
}
long ans = 0;
for(int i=0; i+1<n; i+=2) { // ck - open,, ck1 or ck+1 - close
ans += Math.min(c[i], c[i+1]);
if(c[i+1] > c[i]) { // cant go further if there r pending close brackets here itself
continue;
}
long ci = c[i] - c[i+1]; // pending opens at i
long cj = 0; // represents the pending opens till the j or j+1 from i+2
for(int j=i+2; j+1<n; j+=2) {
cj += c[j];
if(cj > c[j+1]) { // pending opens > closes available here, so no points here
// close how many ever u can and move on
cj -= c[j+1];
continue;
}
// we can close some ci if ci arent 0.
long cj1 = c[j+1] - cj; // spare ones left after closing all opens not including i opens
cj = 0; // since we just closed all pending opens except at i
ans++; // we can consider i to j+1 as regular, so ans++ even if cj1 == 0
if(cj1 > ci) {
ans += ci;
break; // if we have closes even after we close opens at i, we cant move further
}
ci -= cj1; // might become 0 or stay positive
ans += cj1;
}
}
System.out.println(ans);
//}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 6166331c817091daf710fce1ccb28850 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | // Problem: C. Compressed Bracket Sequence
// Contest: Codeforces - Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)
// URL: https://codeforces.com/problemset/problem/1556/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Collections;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.*;
public class Main {
public static void main(String[] args) throws IOException {
// try {
// Scanner in = new Scanner(System.in) ;
FastScanner in = new FastScanner();
int n = in.nextInt() ;
int a[] = in.readArray(n) ;
long ans = 0 ;
for(int i=0 ; i<n ;i+=2){
long open1 = a[i] ;
long open2 = 0 ;
long add = 0 ;
for(int j=i+1 ;j<n ; j++ ){
if(j%2 == 0){
open2 += a[j] ;
}
else{
long close = a[j] ;
long d = min(open2 , close) ;
open2 -= d ;
close -= d;
if(open2 == 0){
add += min(open1 , close) + (add> 0 ?1 : 0) ;
}
d = min(open1 , close) ;
open1 -= d ;
close -= d ;
if(close > 0)break ;
}
}
ans += add ;
}
System.out.println(ans) ;
out.flush();
out.close();
// } catch (Exception e) {
// return;
// }
}
static long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
static int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
static void sort(int ar[]) {
int n = ar.length;
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static void sort1(long ar[]) {
int n = ar.length;
ArrayList<Long> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static long ncr(long n, long r, long mod) {
if (r == 0)
return 1;
long val = ncr(n - 1, r - 1, mod);
val = (n * val) % mod;
val = (val * modInverse(r, mod)) % mod;
return val;
}
static long fast_pow(long base, long n, long M) {
if (n == 0)
return 1;
if (n == 1)
return base % M;
long halfn = fast_pow(base, n / 2, M);
if (n % 2 == 0)
return (halfn * halfn) % M;
else
return (((halfn * halfn) % M) * base) % M;
}
static long modInverse(long n, long M) {
return fast_pow(n, M - 2, M);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static PrintWriter out = new PrintWriter(System.out);
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 720926bb8c3e28e5981044e3b65e9bd8 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
//static final long mod=(long)1e9+7;
/*static final long mod=998244353L;
public static long pow(long a,long p)
{
long res=1;
while(p>0)
{
if(p%2==1)
{
p--;
res*=a;
res%=mod;
}
else
{
a*=a;
a%=mod;
p/=2;
}
}
return res;
}*/
/*static class Pair
{
int u,v;
Pair(int u,int v)
{
this.u=u;
this.v=v;
}
}*/
/*static class Pair implements Comparable<Pair>
{
int v,l;
Pair(int v,int l)
{
this.v=v;
this.l=l;
}
public int compareTo(Pair p)
{
return l-p.l;
}
}*/
/*static long gcd(long a,long b)
{
if(b%a==0)
return a;
return gcd(b%a,a);
}
public static void dfs(int u,ArrayList<Integer> edge[],boolean vis[])
{
vis[u]=true;
for(int v:edge[u])
{
if(!vis[v])
dfs(v,edge,vis);
}
}
static class DSU
{
int par[],rank[],n;
DSU(int n)
{
this.n=n;
par=new int[n+1];
rank=new int[n+1];
for(int i=1;i<=n;i++)
par[i]=i;
}
public void union(int u,int v)
{
u=find(u);
v=find(v);
if(u==v)
return;
if(rank[u]>rank[v])
par[v]=u;
else if(rank[u]<rank[v])
par[u]=v;
else
{
rank[v]++;
par[u]=v;
}
}
public int find(int u)
{
if(u==par[u])
return u;
return par[u]=find(par[u]);
}
}
static class Edge
{
int v,ind;
long w;
Edge(int v,int w,int ind)
{
this.ind=ind;
this.v=v;
this.w=1L*w;
}
}
static class Pair implements Comparable<Pair>
{
int u,v;
Pair(int u,int v)
{
this.u=u;
this.v=v;
}
public int compareTo(Pair p)
{
if(this.u!=p.u)
return this.u-p.u;
return p.v-this.v;
}
}*/
public static void main(String args[])throws Exception
{
FastReader fs=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
//int tc=fs.nextInt();
int tc=1;
while(tc-->0)
{
int n=fs.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=fs.nextInt();
long ans=0;
for(int i=0;i<n;i+=2)
{
long s=a[i],prev=a[i];
for(int j=i+1;j<n;j++)
{
if(j%2==1)
{
if(j==i+1)
{
if(s<a[j])
{
ans+=s;
break;
}
else
{
s-=a[j];
ans+=a[j];
prev=s;
}
}
else
{
if(s-a[j]<0)
{
ans+=prev+1;
break;
}
if(s-a[j]<=prev)
{
ans+=(prev-s+a[j]+1);
s-=a[j];
prev=s;
}
else
s-=a[j];
}
}
else
s+=a[j];
}
}
pw.println(ans);
}
pw.flush();
pw.close();
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | eb4bcc44b5036efcd326ba7befce15e0 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
public class A
{
static int n;
static int[] arr;
static char[] s;
public static void main(String[] args) throws IOException
{
f = new Flash();
out = new PrintWriter(System.out);
int T = 1;//ni();
for(int tc = 1; tc <= T; tc++){
n = ni(); arr = arr(n); sop(fn());
}
out.flush(); out.close();
}
//https://www.youtube.com/watch?v=7AHvvTWSsqY
static long fn()
{
long[] open = new long[n];
long[] close = new long[n];
for(int i = 0; i < n; i++) {
if(i%2 == 0) open[i] += arr[i];
else close[i] += arr[i];
}
for(int i = 1; i < n; i++) {
open[i] += open[i-1];
close[i] += close[i-1];
}
long[][] ap = new long[n][n];
for(int j = 0; j < n; j++) {
for(int i = j; i >= 0; i--) {
if(i==j) {
ap[i][j] = arr[i];
if(i%2 == 1) ap[i][j] *= -1;
continue;
}
long cur = arr[i];
if(i%2 == 1) cur *= -1;
ap[i][j] = min(cur, cur + ap[i+1][j]);
}
}
long[][] dp = new long[n][n];
for(int j = 0; j < n; j++) {
for(int i = j-1; i >= 0; i--) {
if(j == i+1) {
if(i%2 == 0) dp[i][j] = min(arr[i], arr[j]);
continue;
}
dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1];
if(i%2 == 1 || j%2 == 0) continue;
long mv = ap[i+1][j-1];
mv = abs(mv);
long totalOpen = open[j-1] - open[i];
long totalClose = close[j-1] - close[i];
if(arr[i] < mv) continue;
totalOpen += mv;
if(arr[j] < (totalOpen-totalClose)) continue;
dp[i][j] += 1;
long extraLeft = arr[i]-mv;
long extraRight = arr[j] - (totalOpen-totalClose);
dp[i][j] += min(extraLeft, extraRight);
}
}
return dp[0][n-1];
}
static Flash f;
static PrintWriter out;
static final long mod = (long)1e9+7;
static final long inf = (long)1e18;
static final int _inf = (int)1e9;
static final int maxN = (int)1e6;
static long[] fact, inv;
static class cpr implements Comparator<int[]>{
public int compare(int[] a, int[] b) {
return Integer.compare(a[0], b[0]);
}
}
static void sort(int[] a){
List<Integer> A = new ArrayList<>();
for(int i : a) A.add(i);
Collections.sort(A);
for(int i = 0; i < A.size(); i++) a[i] = A.get(i);
}
static void sort(long[] a){
List<Long> A = new ArrayList<>();
for(long i : a) A.add(i);
Collections.sort(A);
for(int i = 0; i < A.size(); i++) a[i] = A.get(i);
}
static void print(int[] a){
StringBuilder sb = new StringBuilder();
for(int v : a) sb.append(v + " ");
out.println(sb);
}
static void print(long[] a){
StringBuilder sb = new StringBuilder();
for(long v : a) sb.append(v + " ");
out.println(sb);
}
static void print(List<Integer> A){
StringBuilder sb = new StringBuilder();
for(int v : A) sb.append(v + " ");
out.println(sb);
}
static int swap(int itself, int dummy){return itself;}
static long swap(long itself, long dummy){return itself;}
static char swap(char itself, char dummy){return itself;}
static void sop(Object o){out.println(o);}
static int ni(){return f.ni();}
static long nl(){return f.nl();}
static double nd(){return f.nd();}
static String next(){return f.next();}
static String ns(){return f.ns();}
static String yes = "YES", no = "NO";
static char[] nc(){return f.nc();}
static int[] arr(int len){return f.arr(len);}
static int gcd(int a, int b){if(b == 0) return a; return gcd(b, a%b);}
static long gcd(long a, long b){if(b == 0) return a; return gcd(b, a%b);}
static int fcm(int a, int b){return (a*b)/gcd(a, b);}
static long fcm(long a, long b){return (a*b)/gcd(a, b);}
static double log2(long N) {return Math.log(N) / Math.log(2);}
static void debug(int[] a) {sop(Arrays.toString(a));}
static void debug(long[] a) {sop(Arrays.toString(a));}
static class Flash
{
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 ns(){
String s = new String();
try{
s = br.readLine().trim();
}catch(IOException e){
e.printStackTrace();
}
return s;
}
int[] arr(int n){
int[] a = new int[n];
for(int i = 0; i < n; i++) a[i] = ni();
return a;
}
char[] nc(){return ns().toCharArray();}
int ni(){return Integer.parseInt(next());}
long nl(){return Long.parseLong(next());}
double nd(){return Double.parseDouble(next());}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 5485f3d4848b5031946dcded0806d5eb | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Codechef {
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = 1;
for (int t = 0; t < test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
long res = 0;
for (int i = 0; i < n; i += 2) {
long min = arr[i];
long curr = arr[i];
for (int j = i + 1; j < n; j++) {
if (j % 2 == 0) {
curr += arr[j];
}else {
long l = Math.max(0, curr - arr[j]);
long r = Math.min(min, Math.min(curr - 1, arr[i] - 1L));
res += Math.max(0, r - l + 1);
curr -= arr[j];
min = Math.min(min, curr);
}
}
}
out.println(res);
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 7071d15d524f5d6a722aee69cdc25628 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
/**
* @author Naitik
*
*/
public class Main
{
static FastReader sc=new FastReader();
static int dp[][][];
static int mod=1000000007;
static int mod1=998244353;
static int max;
static long bit[];
// static long seg[];
// static long fact[];
// static long A[];
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args)
{
// StringBuffer sb=new StringBuffer("");
int ttt=1;
// ttt =i();
outer :while (ttt-- > 0)
{
int n=i();
long A[]=new long[n+1];
for(int i=1;i<=n;i++) {
A[i]=i();
}
if(n%2!=0)
n--;
long ans=0;
for(int i=1;i<=n;i+=2) {
if(A[i+1]>A[i]) {
ans+=A[i];
continue;
}
ans+=A[i+1];
long y=A[i]-A[i+1];
long left=0;
for(int j=i+2;j<=n;j+=2) {
if(A[j+1]>=left+A[j]) {
ans++;
long u=A[j+1]-(left+A[j]);
if(u>y) {
ans+=y;
break;
}
left=0;
ans+=u;
y-=u;
}
else {
left=(left+A[j])-A[j+1];
}
}
}
System.out.println(ans);
}
//System.out.println(sb.toString());
out.close();
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1
}
static class Pair implements Comparable<Pair>
{
int x;
int y;
// int z;
Pair(int x,int y,int z){
this.x=x;
this.y=y;
//this.z=z;
}
@Override
public int compareTo(Pair o) {
if(this.x>o.x)
return +1;
else if(this.x<o.x)
return -1;
else {
if(this.y>o.y)
return -1;
else if(this.y<o.y)
return +1;
else
return 0;
}
}
// public int hashCode()
// {
// final int temp = 14;
// int ans = 1;
// ans =x*31+y*13;
// return ans;
// }
// @Override
// public boolean equals(Object o)
// {
// if (this == o) {
// return true;
// }
// if (o == null) {
// return false;
// }
// if (this.getClass() != o.getClass()) {
// return false;
// }
// Pair other = (Pair)o;
// if (this.x != other.x || this.y!=other.y) {
// return false;
// }
// return true;
// }
//
/* FOR TREE MAP PAIR USE */
// public int compareTo(Pair o) {
// if (x > o.x) {
// return 1;
// }
// if (x < o.x) {
// return -1;
// }
// if (y > o.y) {
// return 1;
// }
// if (y < o.y) {
// return -1;
// }
// return 0;
// }
}
static int find(int A[],int a) {
if(A[a]<0)
return a;
return A[a]=find(A, A[a]);
}
//static int find(int A[],int a) {
// if(A[a]==a)
// return a;
// return A[a]=find(A, A[a]);
//}
//FENWICK TREE
static void update(int i, int x){
for(; i < bit.length; i += (i&-i))
bit[i] += x;
}
static int sum(int i){
int ans = 0;
for(; i > 0; i -= (i&-i))
ans += bit[i];
return ans;
}
//END
//static void add(int v) {
// if(!map.containsKey(v)) {
// map.put(v, 1);
// }
// else {
// map.put(v, map.get(v)+1);
// }
//}
//static void remove(int v) {
// if(map.containsKey(v)) {
// map.put(v, map.get(v)-1);
// if(map.get(v)==0)
// map.remove(v);
// }
//}
public static int upper(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
ans=mid;
l=mid+1;
}
else {
u=mid-1;
}
}
return ans;
}
public static int lower(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
l=mid+1;
}
else {
ans=mid;
u=mid-1;
}
}
return ans;
}
static int[] copy(int A[]) {
int B[]=new int[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static long[] copy(long A[]) {
long B[]=new long[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void reverse(long A[]) {
int n=A.length;
long B[]=new long[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void reverse(int A[]) {
int n=A.length;
int B[]=new int[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void input(int A[],int B[]) {
for(int i=0;i<A.length;i++) {
A[i]=sc.nextInt();
B[i]=sc.nextInt();
}
}
static int[][] input(int n,int m){
int A[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=i();
}
}
return A;
}
static char[][] charinput(int n,int m){
char A[][]=new char[n][m];
for(int i=0;i<n;i++) {
String s=s();
for(int j=0;j<m;j++) {
A[i][j]=s.charAt(j);
}
}
return A;
}
static int max(int A[]) {
int max=Integer.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static int min(int A[]) {
int min=Integer.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long max(long A[]) {
long max=Long.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static long min(long A[]) {
long min=Long.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long [] prefix(long A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] prefix(int A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] suffix(long A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static long [] suffix(int A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static void fill(int dp[]) {
Arrays.fill(dp, -1);
}
static void fill(int dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(int dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(int dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static void fill(long dp[]) {
Arrays.fill(dp, -1);
}
static void fill(long dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(long dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(long dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long power(long x, long y, long p)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static void print(int A[]) {
for(int i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static void sort(int[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static String sort(String s) {
Character ch[]=new Character[s.length()];
for(int i=0;i<s.length();i++) {
ch[i]=s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st=new StringBuffer("");
for(int i=0;i<s.length();i++) {
st.append(ch[i]);
}
return st.toString();
}
static HashMap<Integer,Integer> hash(int A[]){
HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Integer,Integer> tree(int A[]){
TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 9194c6909d8cd02e30b81e02ab924bc5 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.util.InputMismatchException;
public class Main {
static PrintWriter out;
static Reader in;
public static void main(String[] args) throws IOException {
input_output();
Main solver = new Main();
solver.solve();
out.close();
out.flush();
}
static long INF = (long)1e18;
static int MAXN = (int)1e5 + 5;
static int MOD = (int)1e9 + 7;
static int q, t, n, m, k;
static double pi = Math.PI;
void solve() throws IOException {
n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
if (i%2 == 0) arr[i] = in.nextInt();
else arr[i] = -in.nextInt();
}
long[] pre = new long[n];
pre[0] = arr[0];
for (int i = 1; i < n; i++) pre[i] = arr[i]+pre[i-1];
long ans = 0;
for (int i = 1; i < n; i+=2) {
for (int j = i-1; j < n; j+= 2) {
long sum = 0, min = 0;
for (int x = i; x <= j; x++) {
sum += arr[x];
min = Math.min(min, sum);
}
if (arr[i-1] + min < 0) continue;
else {
long pos = arr[i-1]+min,
neg = 0;
sum += -min;
if (j+1 < n) neg = -arr[j+1];
else continue;
//out.println(i+" "+j+", pos: "+pos+", neg: "+neg+", min: "+min+", sum: "+sum);
if (sum < 0) {
long tmp = Math.min(-sum, pos);
sum += tmp;
pos -= tmp;
} else {
long tmp = Math.min(sum, neg);
sum -= tmp;
neg -= tmp;
}
if (sum != 0) continue;
//out.println(1+Math.min(neg, pos));
ans += 1+Math.min(neg, pos);
}
}
}
ans -= n/2;
out.println(ans);
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static void input_output() throws IOException {
File f = new File("in.txt");
if (f.exists() && !f.isDirectory()) {
in = new Reader(new FileInputStream("in.txt"));
} else in = new Reader();
f = new File("out.txt");
if (f.exists() && !f.isDirectory()) {
out = new PrintWriter(new File("out.txt"));
} else out = new PrintWriter(System.out);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 49f3fef08dd1bd2df058fd7079bd9e10 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int n=Integer.parseInt(bu.readLine());
int a[]=new int[n],i;
String s[]=bu.readLine().split(" ");
for(i=0;i<n;i++) a[i]=Integer.parseInt(s[i]);
long ans=0;
for(i=0;i<n;i++)
if(i%2==0)
{
Stack<Integer> st=new Stack<>();
int c,d,j; long cur=a[i];
st.add(a[i]);
for(j=i+1;j<n;j++)
{
if(j%2==0) {st.add(a[j]); cur+=a[j];}
else
{
cur-=a[j];
c=a[j];
while(st.size()>1 && c-st.peek()>=0) c-=st.pop();
if(st.size()>1)
{
d=st.pop();
d-=c;
st.add(d);
}
else if(st.size()==1)
{
if(c!=a[j]) ans++;
d=st.pop();
ans+=Math.min(d,c);
d-=c;
st.add(Math.max(0,d));
}
}
if(cur<0) break;
}
//System.out.println();
}
System.out.println(ans);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 40441e4dee3c370b84ec93cdb3582d04 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
long[] ar = new long[n];
for (int i = 0; i < n; i++){
ar[i] = s.nextLong();
}
long ans = 0;
for (int i = 0; i < n; i+=2){
long bal = 0, minbal = 0;
for (int j = i+1; j < n; j++){
if (i%2 != j%2){
long left = -minbal, right = bal-minbal;
left = Math.max(left, 1);
right = Math.max(right, 1);
if (left <= ar[i] && right <= ar[j]){
ans += Math.min(ar[i]-left, ar[j]-right)+1;
}
}
if (j%2 == 0){
bal += ar[j];
} else {
bal -= ar[j];
}
minbal = Math.min(minbal, bal);
}
}
System.out.println(ans);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 708dbea4b4d6e1036d22a049e6bb6321 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
public class Main implements Runnable {
int n, m, k;
long n1, s;
static boolean use_n_tests = false;
long mod = 1_000_000_007;
long mod1 = 998244353L;
// Long[][] dp;
double scale = 1e+6;
char[] s1;
Integer[][] dp;
int steps = 0;
List<Integer> primes = Collections.emptyList();//generatePrimes(31623);
boolean[] mark;
List<Integer> curComp;
Graph g;
int[][] ls;
int[] coord1, coord2;
int[] lasts;
boolean[] end1, end2;
int ensz1, ensz2;
List<Integer> ensz2list;
int it = 0;
void solve(FastScanner in, PrintWriter out, int testNumber) {
long ans = 0;
int n = in.nextInt();
List<Long> v1 = new ArrayList<>();
int last = 0;
for (int i = 0; i < n; i++) {
long b = in.nextInt();
if (i % 2 == 0) {
v1.add(b);
} else {
long cnt = 0;
while (!v1.isEmpty() && b > 0) {
if (v1.get(v1.size() - 1) == -1) {
cnt++;
v1.remove(v1.size() - 1);
} else if (v1.get(v1.size() - 1) > 0 ) {
if (cnt > 0) {
long x = cnt - 1;
long y = (x * (x + 1)) / 2;
ans += y;
cnt = 0;
}
long mn = Math.min(v1.get(v1.size() - 1), b);
b -= mn;
v1.set(v1.size() - 1, v1.get(v1.size() - 1) - mn);
ans += mn;
if (v1.get(v1.size() - 1) == 0) {
v1.remove(v1.size() - 1);
}
v1.add(-1L);
} else {
break;
}
}
if (cnt > 0) {
long x = cnt - 1;
long y = (x * (x + 1)) / 2;
ans += y;
cnt = 0;
}
}
}
for (int i = 0; i < v1.size(); i++) {
if (v1.get(i) == -1) {
long cnt = 0;
for (; i < v1.size(); i++) {
if (v1.get(i) != -1) {
break;
}
cnt++;
}
if (cnt > 0) {
long x = cnt - 1;
long y = (x * (x + 1)) / 2;
ans += y;
cnt = 0;
}
}
}
out.println(ans);
}
// ****************************** template code ***********
static public class LcaSparseTable {
int len;
int[][] up;
int[] tin;
int[] tout;
int time;
static List<Integer>[] tree;
static LcaSparseTable t;
void dfs(List<Integer>[] tree, int u, int p) {
tin[u] = time++;
up[0][u] = p;
for (int i = 1; i < len; i++)
up[i][u] = up[i - 1][up[i - 1][u]];
for (int v : tree[u])
if (v != p)
dfs(tree, v, u);
tout[u] = time++;
}
public LcaSparseTable(List<Integer>[] tree, int root) {
int n = tree.length;
len = 1;
while ((1 << len) <= n) ++len;
up = new int[len][n];
tin = new int[n];
tout = new int[n];
dfs(tree, root, root);
}
boolean isParent(int parent, int child) {
return tin[parent] <= tin[child] && tout[child] <= tout[parent];
}
public int lca(int a, int b) {
if (isParent(a, b))
return a;
if (isParent(b, a))
return b;
for (int i = len - 1; i >= 0; i--)
if (!isParent(up[i][a], b))
a = up[i][a];
return up[0][a];
}
public static void adaptGraph(Graph graph) {
int n = graph.size() + 1;
tree = new List[n];
for (int i = 0; i < n; i++) {
tree[i] = new ArrayList<>();
}
List<Integer> edges = graph.getEdges();
for (int i = 0; i < edges.size() / 2; i++) {
int v = edges.get(i * 2);
int u = edges.get(i * 2 + 1);
tree[v].add(u);
tree[u].add(v);
}
t = new LcaSparseTable(tree, 0);
}
public static int getLca(int a, int b) {
return t.lca(a, b);
}
}
List<Integer> factorize(int a) {
List<Integer> res = new ArrayList<>();
for (int i = 0; i < primes.size(); i++) {
int p = primes.get(i);
while (a % p == 0) {
res.add(p);
a /= p;
}
if (a == 1 && p > a) {
break;
}
}
if (a != 1) {
res.add(a);
}
return res;
}
void impossible() {
out.println(-1);
}
void yes() {
out.println("YES");
}
void no() {
out.println("NO");
}
static boolean next_permutation(char[] p) {
for (int a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (int b = p.length - 1; ; --b)
if (p[b] > p[a]) {
char t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true;
}
return false;
}
public static class DisjointSets {
int[] p;
DisjointSets(int size) {
p = new int[size];
for (int i = 0; i < size; i++)
p[i] = i;
}
public int root(int x) {
return x == p[x] ? x : (p[x] = root(p[x]));
}
public void unite(int a, int b) {
a = root(a);
b = root(b);
if (a != b)
p[a] = b;
}
}
boolean triangleCheck(int a, int b, int c) {
return a + b > c && a + c > b && b + c > a;
}
Map<Integer, Integer> numberCompression(List<Integer> ls) {
Collections.sort(ls);
int id = 1;
Map<Integer, Integer> comp = new HashMap<>();
for (int num : ls) {
if (!comp.containsKey(num)) {
comp.put(num, id++);
}
}
return comp;
}
long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
class Pt {
int x, y;
Pt(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
boolean sameAxis(Pt b) {
return b.x == x || b.y == y;
}
int manxDist(Pt b) {
return Math.abs(x - b.x) + Math.abs(y - b.y);
}
void read() {
x = in.nextInt();
y = in.nextInt();
}
}
void swap(Integer[] a, int i, int j) {
Integer tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void swap(List<Integer> a, int i, int j) {
Integer tmp = a.get(i);
a.set(i, a.get(j));
a.set(j, tmp);
}
long manhDist(long x, long y, long x1, long y1) {
return Math.abs(x - x1) + Math.abs(y - y1);
}
double dist(double x, double y, double x1, double y1) {
return Math.sqrt(Math.pow(x - x1, 2.0) + Math.pow(y - y1, 2.0));
}
public static class FW {
public static void add(long[] t, int i, long value) {
for (; i < t.length; i |= i + 1)
t[i] += value;
}
public static long sum(long[] t, int i) {
long res = 0;
for (; i >= 0; i = (i & (i + 1)) - 1)
res += t[i];
return res;
}
public static void add(long[] t, int a, int b, long value) {
add(t, a, value);
add(t, b + 1, -value);
}
}
int sign(int a) {
if (a < 0) {
return -1;
}
return 1;
}
long binpow(long a, int b) {
long res = 1;
while (b != 0) {
if (b % 2 == 0) {
b /= 2;
a *= a;
a %= mod;
}
b--;
res *= a;
res %= mod;
}
return res;
}
List<Integer> getDigits(long n) {
List<Integer> res = new ArrayList<>();
while (n != 0) {
res.add((int) (n % 10L));
n /= 10;
}
return res;
}
List<Integer> generatePrimes(int n) {
List<Integer> res = new ArrayList<>();
boolean[] sieve = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
if (!sieve[i]) {
res.add(i);
}
if ((long) i * i <= n) {
for (int j = i * i; j <= n; j += i) {
sieve[j] = true;
}
}
}
return res;
}
static int stack_size = 1 << 29;
static class Coeff {
long mod;
long[][] C;
long[] fact;
boolean cycleWay = false;
Coeff(int n, long mod) {
this.mod = mod;
fact = new long[n + 1];
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = i;
fact[i] %= mod;
fact[i] *= fact[i - 1];
fact[i] %= mod;
}
}
Coeff(int n, int m, long mod) {
// n > m
cycleWay = true;
this.mod = mod;
C = new long[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= Math.min(i, m); j++) {
if (j == 0 || j == i) {
C[i][j] = 1;
} else {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
C[i][j] %= mod;
}
}
}
}
public long C(int n, int m) {
if (cycleWay) {
return C[n][m];
}
return fC(n, m);
}
private long fC(int n, int m) {
return (fact[n] * inv(fact[n - m] * fact[m] % mod)) % mod;
}
private long inv(long r) {
if (r == 1)
return 1;
return ((mod - mod / r) * inv(mod % r)) % mod;
}
}
class Pair {
int first;
long second;
Pair(int f, long s) {
first = f;
second = s;
}
public int getFirst() {
return first;
}
public long getSecond() {
return second;
}
}
class MultisetTree<T> {
int size = 0;
TreeMap<T, Integer> mp = new TreeMap<>();
void add(T x) {
mp.merge(x, 1, Integer::sum);
size++;
}
void remove(T x) {
if (mp.containsKey(x)) {
mp.merge(x, -1, Integer::sum);
if (mp.get(x) == 0) {
mp.remove(x);
}
size--;
}
}
boolean contains(T x) {
return mp.containsKey(x);
}
T greatest() {
return mp.lastKey();
}
T higher(T x) {
return mp.higherKey(x);
}
T smallest() {
return mp.firstKey();
}
T biggest() {
return mp.lastKey();
}
int size() {
return size;
}
int diffSize() {
return mp.size();
}
}
class Multiset<T> {
int size = 0;
Map<T, Integer> mp = new HashMap<>();
void add(T x) {
mp.merge(x, 1, Integer::sum);
size++;
}
boolean contains(T x) {
return mp.containsKey(x);
}
void remove(T x) {
if (mp.containsKey(x)) {
mp.merge(x, -1, Integer::sum);
if (mp.get(x) == 0) {
mp.remove(x);
}
size--;
}
}
int size() {
return size;
}
int diffSize() {
return mp.size();
}
}
static class Range {
int l, r;
int id;
public int getL() {
return l;
}
public int getR() {
return r;
}
public Range(int l, int r, int id) {
this.l = l;
this.r = r;
this.id = id;
}
}
static class Array {
static Range[] readRanges(int n, FastScanner in) {
Range[] result = new Range[n];
for (int i = 0; i < n; i++) {
result[i] = new Range(in.nextInt(), in.nextInt(), i);
}
return result;
}
static List<List<Integer>> intInit2D(int n) {
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
res.add(new ArrayList<>());
}
return res;
}
static boolean isSorted(Integer[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static public long sum(List<Integer> a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static public long sum(int[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static public long sum(long[] a) {
long sum = 0;
for (long x : a) {
sum += x;
}
return sum;
}
static public long sum(Integer[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static public int min(Integer[] a) {
int mn = Integer.MAX_VALUE;
for (int x : a) {
mn = Math.min(mn, x);
}
return mn;
}
static public int min(int[] a) {
int mn = Integer.MAX_VALUE;
for (int x : a) {
mn = Math.min(mn, x);
}
return mn;
}
static public int max(Integer[] a) {
int mx = Integer.MIN_VALUE;
for (int x : a) {
mx = Math.max(mx, x);
}
return mx;
}
static public int max(int[] a) {
int mx = Integer.MIN_VALUE;
for (int x : a) {
mx = Math.max(mx, x);
}
return mx;
}
static public int[] readint(int n, FastScanner in) {
int[] out = new int[n];
for (int i = 0; i < out.length; i++) {
out[i] = in.nextInt();
}
return out;
}
}
class Graph {
List<List<Integer>> graph;
List<Integer> edges;
Graph(int n) {
create(n);
}
private void create(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
this.graph = graph;
edges = new ArrayList<>();
}
int size() {
return graph.size();
}
List<Integer> abj(int v) {
return graph.get(v);
}
void read(int m, FastScanner in) {
for (int i = 0; i < m; i++) {
int v = in.nextInt() - 1;
int u = in.nextInt() - 1;
graph.get(v).add(u);
}
}
void readBi(int m, FastScanner in) {
for (int i = 0; i < m; i++) {
int v = in.nextInt() - 1;
int u = in.nextInt() - 1;
addEdge(v, u);
}
}
public void addEdge(int v, int u) {
graph.get(v).add(u);
graph.get(u).add(v);
edges.add(v);
edges.add(u);
}
public List<Integer> getEdges() {
return edges;
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream io) {
br = new BufferedReader(new InputStreamReader(io));
}
public String line() {
String result = "";
try {
result = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public char[] nextc() {
return next().toCharArray();
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextInt();
}
return res;
}
public int[] nextArray() {
int n = in.nextInt();
return nextArray(n);
}
public long[] nextArrayL(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextLong();
}
return res;
}
public Long[] nextArrayL2(int n) {
Long[] res = new Long[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextLong();
}
return res;
}
public Integer[] nextArray2(int n) {
Integer[] res = new Integer[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextInt();
}
return res;
}
public long nextLong() {
return Long.parseLong(next());
}
}
void run_t_tests() {
int t = in.nextInt();
int i = 0;
while (t-- > 0) {
solve(in, out, i++);
}
}
void run_one() {
solve(in, out, -1);
}
@Override
public void run() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
if (use_n_tests) {
run_t_tests();
} else {
run_one();
}
out.close();
}
static FastScanner in;
static PrintWriter out;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(null, new Main(), "", stack_size);
thread.start();
thread.join();
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | fdea3341f617f85512ed1513e19c79d9 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import static java.util.Map.Entry.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
static long mod=(long) (1e9+7);
public static void main (String[] args) throws Exception
{
final long mod1=(long) 1e9+7;
Reader s=new Reader();
PrintWriter pt=new PrintWriter(System.out);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// int T=Integer.parseInt(br.readLine());
// int T=s.nextInt();
// System.out.println(T);
int T=1;
ol:while(T-->0) {
int n=s.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++) {
arr[i]=s.nextInt();
}
long ans=0;
for(int i=0;i<n;i+=2) {
// System.out.println("--");
long c=arr[i];
long st=0;
for(int j=i+1;j<n;j++) {
if(j%2==0) {
st+=arr[j];
}
else {
if(st==0) {
ans+=Math.min(c, arr[j]);
// System.out.println(j+" "+c+" "+arr[j]);
c-=arr[j];
if(c<0) {
break;
}
}
else if(arr[j]>=st) {
int temp=arr[j];
temp-=st;
st=0;
ans++;
ans+=Math.min(c, temp);
c-=temp;
if(c<0) {
break;
}
}
else {
st-=arr[j];
}
}
}
// System.out.println(ans);
}
pt.println(ans);
}
pt.close();
}
/**
* Seive
*
* @param n length
* @param factors factors[num] is smallest prime divisor of num
* @param ar list of primes
*/
static void sieveOfEratosthenes(int n, int factors[], ArrayList<Integer> ar)
{
factors[1]=1;
int p;
for(p = 2; p*p <=n; p++)
{
if(factors[p] == 0)
{
ar.add(p);
factors[p]=p;
for(int i = p*p; i <= n; i += p)
if(factors[i]==0)
factors[i] = p;
}
}
for(;p<=n;p++){
if(factors[p] == 0)
{
factors[p] = p;
ar.add(p);
}
}
}
static int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was
// not present
return -1;
}
public static int getFreq(int arr[], int n) {
int k=0;
for(int i=0;i<arr.length;i++) {
if(arr[i]==n)
k++;
}
return k;
}
public static void primeFactors(int n)
{
// Print the number of 2s that divide n
HashMap<Integer, Integer> hm=new HashMap<Integer, Integer>();
while (n%2==0)
{
hm.put(2, hm.getOrDefault(2, 0)+1);
n/=2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
n /= i;
hm.put(i, hm.getOrDefault(i, 0)+1);
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n > 2)
hm.put(n, hm.getOrDefault(n, 0)+1);
}
static boolean isPartition(int arr[], int n) {
int sum = 0;
int i, j;
// Calculate sum of all elements
for (i = 0; i < n; i++)
sum += arr[i];
if (sum % 2 != 0)
return false;
boolean part[][]=new boolean[sum/2+1][n+1];
// initialize top row as true
for (i = 0; i <= n; i++)
part[0][i] = true;
// initialize leftmost column, except part[0][0], as false
for (i = 1; i <= sum / 2; i++)
part[i][0] = false;
// Fill the partition table in bottom up manner
for (i = 1; i <= sum / 2; i++) {
for (j = 1; j <= n; j++) {
part[i][j] = part[i][j - 1];
if (i >= arr[j - 1])
part[i][j] = part[i][j]
|| part[i - arr[j - 1]][j - 1];
}
}
return part[sum / 2][n];
}
static int setBit(int S, int j) { return S | 1 << j; }
static int clearBit(int S, int j) { return S & ~(1 << j); }
static int toggleBit(int S, int j) { return S ^ 1 << j; }
static boolean isOn(int S, int j) { return (S & 1 << j) != 0; }
static int turnOnLastZero(int S) { return S | S + 1; }
static int turnOnLastConsecutiveZeroes(int S) { return S | S - 1; }
static int turnOffLastBit(int S) { return S & S - 1; }
static int turnOffLastConsecutiveBits(int S) { return S & S + 1; }
static int lowBit(int S) { return S & -S; }
static int setAll(int N) { return (1 << N) - 1; }
static int modulo(int S, int N) { return (S & N - 1); } //S%N, N is a power of 2
static boolean isPowerOfTwo(int S) { return (S & S - 1) == 0; }
static boolean isWithin(long x, long y, long d, long k) {
return x*k*x*k + y*k*y*k <= d*d;
}
static long modFact(long n,
long p)
{
if (n >= p)
return 0;
long result = 1;
for (int i = 1; i <= n; i++)
result = (result * i) % p;
return result;
}
static int sum(int[] arr, int n)
{
int inc[]=new int[n+1];
int dec[]=new int[n+1];
inc[0] = arr[0];
dec[0] = arr[0];
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[i]) {
dec[i] = max(dec[i], inc[j] + arr[i]);
}
else if (arr[i] > arr[j]) {
inc[i] = max(inc[i], dec[j] + arr[i]);
}
}
}
return max(inc[n - 1], dec[n - 1]);
}
static long nc2(long a) {
return a*(a-1)/2;
}
public static int numberOfprimeFactors(int n)
{
// Print the number of 2s that divide n
HashSet<Integer> hs = new HashSet<Integer>();
while (n%2==0)
{
hs.add(2);
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
hs.add(i);
n /= i;
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n > 2)
hs.add(n);
return hs.size();
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void reverse(int arr[],int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
static void reverse(long arr[],int start, int end)
{
long temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int p2(int n) {
int k=0;
while(n>1) {
if(n%2!=0)
return k;
n/=2;
k++;
}
return k;
}
static boolean isp2(int n) {
while(n>1) {
if(n%2==1)
return false;
n/=2;
}
return true;
}
static int binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
return mid;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
return -1;
}
static void print(int a[][]) {
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
static int max (int x, int y) {
return (x > y)? x : y;
}
static int search(Pair[] p, Pair pair) {
int l=0, r=p.length;
while (l <= r) {
int m = l + (r - l) / 2;
if (p[m].compareTo(pair)==0)
return m;
if (p[m].compareTo(pair)<0)
l = m + 1;
else
r = m - 1;
}
return -1;
}
static void pa(int a[])
{
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
static void pa(long a[])
{
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
static void reverseArray(int arr[],
int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
static boolean isPalindrome(String s) {
int l=s.length();
for(int i=0;i<l/2;i++)
{
if(s.charAt(i)!=s.charAt(l-i-1))
return false;
}
return true;
}
static long nc2(long n, long m) {
return (n*(n-1)/2)%m;
}
static long c(long a) {
return a*(a+1)/2;
}
static int next(int[] arr, int target)
{
int start = 0, end = arr.length - 1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
// Move to right side if target is
// greater.
if (arr[mid] < target) {
start = mid + 1;
}
// Move left side.
else {
ans = mid;
end = mid - 1;
}
}
return ans;
}
static int prev(Pair[] arr, int target)
{
int start = 0, end = arr.length - 1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
// Move to left side if target is
// lesser.
if (arr[mid].a > target) {
end = mid - 1;
}
// Move right side.
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
static long power(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return power(n, p-2, p);
}
static long nCrModP(long n, long r,
long p)
{
if(r>n)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int) (n+1)];
fac[0] = 1;
for (int i = 1 ;i <= n; i++)
fac[i] = fac[i-1] * i % p;
return (fac[(int) n]* modInverse(fac[(int) r], p)
% p * modInverse(fac[(int) (n-r)], p)
% p) % p;
}
static String reverse(String str)
{
return new StringBuffer(str).reverse().toString();
}
static long fastpow(long x, long y, long m)
{
if (y == 0)
return 1;
long p = fastpow(x, y / 2, m) % m;
p = (p * p) % m;
if (y % 2 == 0)
return p;
else
return (x * p) % m;
}
static boolean isPerfectSquare(long l)
{
return Math.pow((long)Math.sqrt(l),2)==l;
}
static void merge(long[] arr, int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long [n1];
long R[] = new long [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static class Pair implements Comparable<Pair>{
int a;
int b;
Pair(int a,int b){
this.a=a;
this.b=b;
}
public int compareTo(Pair p){
if(a>p.a)
return 1;
if(a==p.a)
return (b-p.b);
return -1;
}
}
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[128]; // 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 | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | e0022c246c3df952e60b7497b87f2747 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
var sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
var c = new long[n];
for(int i = 0; i < n; i++){
c[i] = Long.parseLong(sc.next());
}
long ans = 0;
for(int i = 0; i < n; i += 2){
if(i+1 == n) break;
ans += Math.min(c[i], c[i+1]);
if(c[i] < c[i+1]) continue;
long ci = c[i] - c[i+1];
long cj = 0;
for(int j = i+2; j < n; j += 2){
if(j+1 == n) break;
cj += c[j];
if(c[j+1] < cj){
cj -= c[j+1];
continue;
}
long cj1 = c[j+1] - cj;
cj = 0;
ans++;
if(cj1 > ci){
ans += ci;
break;
}
ans += cj1;
ci -= cj1;
}
}
System.out.println(ans);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 44b75d0ada8e46ff488e12e391a8f2f4 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
public class _1556_C {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(in.readLine());
StringTokenizer line = new StringTokenizer(in.readLine());
int[] a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = Integer.parseInt(line.nextToken());
}
long res = 0;
for(int i = 0; i < n; i += 2) {
long sum = 0;
long min = Long.MAX_VALUE;
for(int j = i; j < n; j++) {
long most = min;
if(j % 2 == 0) {
sum += a[j];
}else {
sum -= a[j];
}
if(j == i) {
min = Math.min(min, sum - 1);
}else {
min = Math.min(min, sum);
}
long least = Math.max(0, sum);
if(j % 2 == 1) {
if(most >= least) {
res += most - least + 1;
}
}
}
}
out.println(res);
in.close();
out.close();
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 974a0ffce80e0c25c33fed337b9c08c4 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int n=Integer.parseInt(bu.readLine());
int a[]=new int[n],i;
String s[]=bu.readLine().split(" ");
for(i=0;i<n;i++) a[i]=Integer.parseInt(s[i]);
long ans=0;
for(i=0;i<n;i++)
if(i%2==0)
{
Stack<Integer> st=new Stack<>();
int c,d,j; long cur=a[i];
st.add(a[i]);
for(j=i+1;j<n;j++)
{
if(j%2==0) {st.add(a[j]); cur+=a[j];}
else
{
cur-=a[j];
c=a[j];
while(st.size()>1 && c-st.peek()>=0) c-=st.pop();
if(st.size()>1)
{
d=st.pop();
d-=c;
st.add(d);
}
else if(st.size()==1)
{
if(c!=a[j]) ans++;
d=st.pop();
ans+=Math.min(d,c);
d-=c;
st.add(Math.max(0,d));
}
}
if(cur<0) break;
}
//System.out.println();
}
System.out.println(ans);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 8641cbec10efc16f4187533cf5b8dc80 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class C{
public static void main(String[] args) throws IOException {
// br = new BufferedReader(new FileReader(".in"));
// out = new PrintWriter(new FileWriter(".out"));
//new Thread(null, new (), "peepee", 1<<28).start();
long ans = 0;
int n =readInt();
long[] a = new long[n];
for (int i = 0; i <n ;i++) a[i]=readInt();
long[][] left = new long[n][n];
long[][] right =new long[n][n];
// Got my dfirections mixed up
// Left is (, right is ).
for (int i = 0; i <n; i++) {
for (int j = i; j < n;j++) {
if (i==j) {
if (i%2==0) left[i][j] = a[i];
else right[i][j] = a[i];
}
else {
left[i][j] += left[i][j-1];
right[i][j] += right[i][j-1];
if (j%2==0) {
// Left facing can never cancel out right facing
left[i][j] += a[j];
}
else {
// Right facing cancels out left facing
long sub = min(left[i][j], a[j]);
left[i][j] -= sub;
right[i][j] += a[j]-sub;
}
}
}
}
//System.out.println(left[1][2] + " " + right[1][2]);
for (int i = 0; i < n; i++) {
for (int j = i+1 ; j < n; j++) {
if (i+1==j) {
if (i%2==0) {
// System.out.println("on " + i + " " + j + ", cheeky add " + min(a[i],a[j]));
ans += min(a[i],a[j]);
}
continue;
}
if (i%2!=0 || j%2!=1) continue;
// Okay, so now you have the middle segment.
// Now do some big brain shit/
// Cancel out first, then add and gg
long tempR = a[j] - left[i+1][j-1];
long tempL = a[i] - right[i+1][j-1];
if (tempR < 0||tempL < 0) continue;
long add = min(tempR, tempL)+1;
//System.out.println(a[i] + " " + a[j]);
//System.out.println("Hello " + left[i+1][j-1] + " " + right[i+1][j-1] + " ");
//System.out.println("Yo " + i + " " + j + " " + tempL + " " + tempR + " added " +add);
ans += add;
}
}
out.println(ans);
out.close();
}
/* Stupid things to try if stuck:
* n=1, expand bsearch range
* brute force small patterns
* submit stupid intuition
*/
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static StringTokenizer st = new StringTokenizer("");
static String read() throws IOException{return st.hasMoreTokens() ? st.nextToken():(st = new StringTokenizer(br.readLine())).nextToken();}
static int readInt() throws IOException{return Integer.parseInt(read());}
static long readLong() throws IOException{return Long.parseLong(read());}
static double readDouble() throws IOException{return Double.parseDouble(read());}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 94ca6d0ea9211d64fce7e4d9449c57a7 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf1556c_2 {
public static void main(String[] args) throws IOException {
int n = ri(), c[] = ria(n);
long bal = 0, ans = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
bal += c[i];
} else {
long tbal = bal, mbal = bal;
bal -= c[i];
for (int j = i - 1; j >= 0; --j) {
if (j % 2 == 0) {
ans += max(0, min(tbal, mbal) - max(tbal - c[j], bal));
tbal -= c[j];
mbal = min(mbal, tbal + 1);
if (tbal < bal) {
break;
}
} else {
tbal += c[j];
}
}
}
// prln(ans);
}
prln(ans);
close();
}
static BufferedReader __i = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __o = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __r = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e9
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final long LMAX = 9223372036854775807L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int fli(double d) {return (int) d;}
static int cei(double d) {return (int) ceil(d);}
static long fll(double d) {return (long) d;}
static long cel(double d) {return (long) ceil(d);}
static int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b);}
static long gcd(long a, long b) {return b == 0 ? a : gcd(b, a % b);}
static int[] exgcd(int a, int b) {if (b == 0) return new int[] {1, 0}; int[] y = exgcd(b, a % b); return new int[] {y[1], y[0] - y[1] * (a / b)};}
static long[] exgcd(long a, long b) {if (b == 0) return new long[] {1, 0}; long[] y = exgcd(b, a % b); return new long[] {y[1], y[0] - y[1] * (a / b)};}
static int randInt(int min, int max) {return __r.nextInt(max - min + 1) + min;}
static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static void rsort(double[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
// input
static void r() throws IOException {input = new StringTokenizer(rline());}
static int ri() throws IOException {return Integer.parseInt(rline());}
static long rl() throws IOException {return Long.parseLong(rline());}
static double rd() throws IOException {return Double.parseDouble(rline());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
static void ria(int[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni();}
static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
static void riam1(int[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
static void rla(long[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nl();}
static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
static void rda(double[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nd();}
static char[] rcha() throws IOException {return rline().toCharArray();}
static void rcha(char[] a) throws IOException {int n = a.length, i = 0; for (char c : rline().toCharArray()) a[i++] = c;}
static String rline() throws IOException {return __i.readLine();}
static String n() {return input.nextToken();}
static int rni() throws IOException {r(); return ni();}
static int ni() {return Integer.parseInt(n());}
static long rnl() throws IOException {r(); return nl();}
static long nl() {return Long.parseLong(n());}
static double rnd() throws IOException {r(); return nd();}
static double nd() {return Double.parseDouble(n());}
// output
static void pr(int i) {__o.print(i);}
static void prln(int i) {__o.println(i);}
static void pr(long l) {__o.print(l);}
static void prln(long l) {__o.println(l);}
static void pr(double d) {__o.print(d);}
static void prln(double d) {__o.println(d);}
static void pr(char c) {__o.print(c);}
static void prln(char c) {__o.println(c);}
static void pr(char[] s) {__o.print(new String(s));}
static void prln(char[] s) {__o.println(new String(s));}
static void pr(String s) {__o.print(s);}
static void prln(String s) {__o.println(s);}
static void pr(Object o) {__o.print(o);}
static void prln(Object o) {__o.println(o);}
static void prln() {__o.println();}
static void pryes() {prln("yes");}
static void pry() {prln("Yes");}
static void prY() {prln("YES");}
static void prno() {prln("no");}
static void prn() {prln("No");}
static void prN() {prln("NO");}
static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;};
static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;}
static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;}
static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
static void h() {prln("hlfd"); flush();}
static void flush() {__o.flush();}
static void close() {__o.close();}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 9e53e170bc74ec00d88a72176a178462 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
public class C {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int n = sc.nextInt();
long[] arr = new long[n];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
}
if(n % 2 == 1) n--;
long[] brr = new long[n+1];
brr[0] = 0;
TreeSet<Long> vals = new TreeSet<>();
vals.add(0L);
for(int i = 1; i <= n; i++) {
if(i % 2 == 1) {
brr[i] = brr[i-1] + arr[i-1];
}
else {
brr[i] = brr[i-1] - arr[i-1];
}
vals.add(brr[i]);
}
long res = 0;
for(long h: vals) {
long count = 1;
boolean start = false;
for(int i = 0; i < n; i += 2) {
if(brr[i] < h && brr[i+1] > h) {
if(start) {
res += count; count = 1;
}
start = true;
}
else if(brr[i] == h) {
if(start) {
res += count; count++;
}
start = true;
}
else {
//nothing
}
}
if(start && brr[n] <= h) {
res += count;
}
}
long gaps = 0;
long prevh = Long.MIN_VALUE;
for(long h: vals) {
res += gaps * (h - prevh - 1);
gaps = 0;
for(int i = 0; i < n; i += 2) {
if(brr[i] <= h && brr[i+1] > h) gaps++;
}
if(brr[n] > h) gaps--;
prevh = h;
}
System.out.println(res);
}
static class FastScanner {
public BufferedReader reader;
public StringTokenizer tokenizer;
public FastScanner() {
reader = new BufferedReader(new InputStreamReader(System.in), 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() {
try {
return reader.readLine();
} catch(IOException e) {
throw new RuntimeException(e);
}
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 140a0d88ce806ddd01fc317fa806bd58 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | //package com.company.codeforces.v210829;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class C {
FastScanner in;
PrintWriter out;
public static void main(String[] args) {
new C().solve();
}
private void solve() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
out.println(solveCase());
out.flush();
}
private long solveCase() {
int n = in.nextInt();
List<Long> c = IntStream.range(0, n).mapToLong(i -> in.nextLong()).boxed().collect(Collectors.toList());
long res = 0;
for (int start = 0; start < n; start += 2) {
long in = 0;
long inAfterStart = 0;
long minStartRequired = 0;
for (int i = start; i < n; i++) {
if (i % 2 == 0) {
in += c.get(i);
if (i > start) inAfterStart += c.get(i);
} else {
if (inAfterStart + minStartRequired <= c.get(i)) {
res += Math.min(in, c.get(i)) - Math.max(1, inAfterStart + minStartRequired) + 1;
}
if (c.get(i) > in) {
break;
} else {
in -= c.get(i);
inAfterStart -= c.get(i);
minStartRequired = Math.max(minStartRequired, -inAfterStart);
}
}
}
}
return res;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 88db46bd41bc54c94439f112f123fc79 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Math.min;
import static java.lang.System.exit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class C {
static void solve() throws Exception {
int n = scanInt();
long depth = 0, ans = 0, mvals[] = new long[n];
int mcnt = 0, mcnts[] = new int[n];
for (int i = 0; i < n; i++) {
int c = scanInt();
if ((i & 1) == 0) {
depth += c;
} else {
ans += min(depth, c);
depth -= c;
while (mcnt > 0 && mvals[mcnt - 1] > depth) {
ans += mcnts[mcnt - 1];
--mcnt;
}
if (mcnt > 0 && mvals[mcnt - 1] == depth) {
ans += mcnts[mcnt - 1];
++mcnts[mcnt - 1];
} else if (depth < 0) {
depth = 0;
} else {
mvals[mcnt] = depth;
mcnts[mcnt] = 1;
++mcnt;
}
}
}
out.print(ans);
}
static int scanInt() throws IOException {
return parseInt(scanString());
}
static long scanLong() throws IOException {
return parseLong(scanString());
}
static String scanString() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
public static void main(String[] args) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | edd6bc7813e3b44eeea31d4f741de73d | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
CCompressedBracketSequence solver = new CCompressedBracketSequence();
solver.solve(1, in, out);
out.close();
}
}
static class CCompressedBracketSequence {
Debug debug = new Debug(false);
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.ri();
long[] c = in.rl(n);
long ans = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 1) {
continue;
}
long l = 1;
long r = c[i];
long local = 0;
for (int j = i + 1; j < n && r >= 0; j++) {
if (j % 2 == 0) {
l += c[j];
r += c[j];
} else {
l -= c[j];
r -= c[j];
if (l <= 0) {
if (r >= 0) {
local += -l + 1;
} else {
local += r - l + 1;
}
l = 0;
}
}
}
debug.debug("i", i);
debug.debug("local", local);
ans += local;
}
out.println(ans);
}
}
static class FastOutput implements AutoCloseable, Closeable, Appendable {
private static final int THRESHOLD = 32 << 10;
private OutputStream writer;
private StringBuilder cache = new StringBuilder(THRESHOLD * 2);
private static Field stringBuilderValueField;
private char[] charBuf = new char[THRESHOLD * 2];
private byte[] byteBuf = new byte[THRESHOLD * 2];
static {
try {
stringBuilderValueField = StringBuilder.class.getSuperclass().getDeclaredField("value");
stringBuilderValueField.setAccessible(true);
} catch (Exception e) {
stringBuilderValueField = null;
}
stringBuilderValueField = null;
}
public FastOutput append(CharSequence csq) {
cache.append(csq);
return this;
}
public FastOutput append(CharSequence csq, int start, int end) {
cache.append(csq, start, end);
return this;
}
private void afterWrite() {
if (cache.length() < THRESHOLD) {
return;
}
flush();
}
public FastOutput(OutputStream writer) {
this.writer = writer;
}
public FastOutput append(char c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(long c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput println(long c) {
return append(c).println();
}
public FastOutput println() {
return append('\n');
}
public FastOutput flush() {
try {
if (stringBuilderValueField != null) {
try {
byte[] value = (byte[]) stringBuilderValueField.get(cache);
writer.write(value, 0, cache.length());
} catch (Exception e) {
stringBuilderValueField = null;
}
}
if (stringBuilderValueField == null) {
int n = cache.length();
if (n > byteBuf.length) {
//slow
writer.write(cache.toString().getBytes(StandardCharsets.UTF_8));
// writer.append(cache);
} else {
cache.getChars(0, n, charBuf, 0);
for (int i = 0; i < n; i++) {
byteBuf[i] = (byte) charBuf[i];
}
writer.write(byteBuf, 0, n);
}
}
writer.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
writer.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class Debug {
private boolean offline;
private PrintStream out = System.err;
public Debug(boolean enable) {
offline = enable && System.getSecurityManager() == null;
}
public Debug debug(String name, int x) {
if (offline) {
debug(name, "" + x);
}
return this;
}
public Debug debug(String name, long x) {
if (offline) {
debug(name, "" + x);
}
return this;
}
public Debug debug(String name, String x) {
if (offline) {
out.printf("%s=%s", name, x);
out.println();
}
return this;
}
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
public void populate(long[] data) {
for (int i = 0; i < data.length; i++) {
data[i] = readLong();
}
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int ri() {
return readInt();
}
public long[] rl(int n) {
long[] ans = new long[n];
populate(ans);
return ans;
}
public int readInt() {
boolean rev = false;
skipBlank();
if (next == '+' || next == '-') {
rev = next == '-';
next = read();
}
int val = 0;
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
return rev ? val : -val;
}
public long readLong() {
boolean rev = false;
skipBlank();
if (next == '+' || next == '-') {
rev = next == '-';
next = read();
}
long val = 0;
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
return rev ? val : -val;
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 5cb18a248ef012ec1cff92df9ec7ea47 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
import java.text.*;
public class CF_1556_C{
//SOLUTION BEGIN
void pre() throws Exception{}
void solve(int TC) throws Exception{
int N = ni();
long[] C = new long[N];
for(int i = 0; i< N; i++)C[i] = nl();
long ans = 0;
for(int i = 0; i+1< N; i+= 2){
long neg = 0, sum = 0;
ans += Math.min(C[i], C[i+1]);
for(int j = i+3; j< N; j += 2){
sum -= C[j-2];
neg = Math.min(neg, sum);
sum += C[j-1];
if(-neg > C[i])break;
long lo = -neg, hi = C[i];
long nlo = lo+sum, nhi = hi+sum;
ans += Math.max(0, Math.min(nhi, C[j])-Math.max(0, nlo)+1);
// ans += Math.max(0, Math.min(C[i], -neg))
// ans += Math.max(0, Math.min(C[i], C[j])+neg);
}
}
pn(ans);
}
//SOLUTION END
void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
void exit(boolean b){if(!b)System.exit(0);}
static void dbg(Object... o){System.err.println(Arrays.deepToString(o));}
final long IINF = (long)1e17;
final int INF = (int)1e9+2;
DecimalFormat df = new DecimalFormat("0.00000000000");
double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-8;
static boolean multipleTC = false, memory = true, fileIO = false;
FastReader in;PrintWriter out;
void run() throws Exception{
long ct = System.currentTimeMillis();
if (fileIO) {
in = new FastReader("");
out = new PrintWriter("");
} else {
in = new FastReader();
out = new PrintWriter(System.out);
}
//Solution Credits: Taranpreet Singh
int T = multipleTC? ni():1;
pre();
for (int t = 1; t <= T; t++) solve(t);
out.flush();
out.close();
System.err.println(System.currentTimeMillis() - ct);
}
public static void main(String[] args) throws Exception{
if(memory)new Thread(null, new Runnable() {public void run(){try{new CF_1556_C().run();}catch(Exception e){e.printStackTrace();System.exit(1);}}}, "1", 1 << 28).start();
else new CF_1556_C().run();
}
int[][] make(int n, int e, int[] from, int[] to, boolean f){
int[][] g = new int[n][];int[]cnt = new int[n];
for(int i = 0; i< e; i++){
cnt[from[i]]++;
if(f)cnt[to[i]]++;
}
for(int i = 0; i< n; i++)g[i] = new int[cnt[i]];
for(int i = 0; i< e; i++){
g[from[i]][--cnt[from[i]]] = to[i];
if(f)g[to[i]][--cnt[to[i]]] = from[i];
}
return g;
}
int[][][] makeS(int n, int e, int[] from, int[] to, boolean f){
int[][][] g = new int[n][][];int[]cnt = new int[n];
for(int i = 0; i< e; i++){
cnt[from[i]]++;
if(f)cnt[to[i]]++;
}
for(int i = 0; i< n; i++)g[i] = new int[cnt[i]][];
for(int i = 0; i< e; i++){
g[from[i]][--cnt[from[i]]] = new int[]{to[i], i, 0};
if(f)g[to[i]][--cnt[to[i]]] = new int[]{from[i], i, 1};
}
return g;
}
int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));}
int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;}
long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
void p(Object... o){for(Object oo:o)out.print(oo+" ");}
void pn(Object... o){for(int i = 0; i< o.length; i++)out.print(o[i]+(i+1 < o.length?" ":"\n"));}
void pni(Object... o){for(Object oo:o)out.print(oo+" ");out.println();out.flush();}
String n()throws Exception{return in.next();}
String nln()throws Exception{return in.nextLine();}
int ni()throws Exception{return Integer.parseInt(in.next());}
long nl()throws Exception{return Long.parseLong(in.next());}
double nd()throws Exception{return Double.parseDouble(in.next());}
class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception{
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception{
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch (IOException e){
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception{
String str;
try{
str = br.readLine();
}catch (IOException e){
throw new Exception(e.toString());
}
return str;
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | ec87427a8a1aead7beac2a8256b2761b | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | //package deltixs2021;
import java.io.*;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Queue;
public class C {
InputStream is;
FastWriter out;
String INPUT = "";
void solve()
{
int n = ni();
int[] a = na(n);
long ans = 0;
for(int i = 0;i < n;i+=2){
long h = 0;
long min = 0;
for(int j = i+1;j < n;j+=2){
if(j > i+1){
h -= a[j-2];
min = Math.min(min, h);
h += a[j-1];
}
if(h >= 0){
long both = Math.max(0, Math.min(a[i]+min, a[j]-h+min) + (i+1 < j ? 1 : 0));
ans += both;
}else{
long both = Math.max(0, Math.min(a[i]+min, a[j]+(min-h)) + (i+1 < j ? 1 : 0));
ans += both;
}
}
}
out.println(ans);
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new FastWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new C().run(); }
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private long[] nal(int n)
{
long[] a = new long[n];
for(int i = 0;i < n;i++)a[i] = nl();
return a;
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[][] nmi(int n, int m) {
int[][] map = new int[n][];
for(int i = 0;i < n;i++)map[i] = na(m);
return map;
}
private int ni() { return (int)nl(); }
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
public static class FastWriter
{
private static final int BUF_SIZE = 1<<13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter(){out = null;}
public FastWriter(OutputStream os)
{
this.out = os;
}
public FastWriter(String path)
{
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b)
{
buf[ptr++] = b;
if(ptr == BUF_SIZE)innerflush();
return this;
}
public FastWriter write(char c)
{
return write((byte)c);
}
public FastWriter write(char[] s)
{
for(char c : s){
buf[ptr++] = (byte)c;
if(ptr == BUF_SIZE)innerflush();
}
return this;
}
public FastWriter write(String s)
{
s.chars().forEach(c -> {
buf[ptr++] = (byte)c;
if(ptr == BUF_SIZE)innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastWriter write(int x)
{
if(x == Integer.MIN_VALUE){
return write((long)x);
}
if(ptr + 12 >= BUF_SIZE)innerflush();
if(x < 0){
write((byte)'-');
x = -x;
}
int d = countDigits(x);
for(int i = ptr + d - 1;i >= ptr;i--){
buf[i] = (byte)('0'+x%10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastWriter write(long x)
{
if(x == Long.MIN_VALUE){
return write("" + x);
}
if(ptr + 21 >= BUF_SIZE)innerflush();
if(x < 0){
write((byte)'-');
x = -x;
}
int d = countDigits(x);
for(int i = ptr + d - 1;i >= ptr;i--){
buf[i] = (byte)('0'+x%10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision)
{
if(x < 0){
write('-');
x = -x;
}
x += Math.pow(10, -precision)/2;
// if(x < 0){ x = 0; }
write((long)x).write(".");
x -= (long)x;
for(int i = 0;i < precision;i++){
x *= 10;
write((char)('0'+(int)x));
x -= (int)x;
}
return this;
}
public FastWriter writeln(char c){
return write(c).writeln();
}
public FastWriter writeln(int x){
return write(x).writeln();
}
public FastWriter writeln(long x){
return write(x).writeln();
}
public FastWriter writeln(double x, int precision){
return write(x, precision).writeln();
}
public FastWriter write(int... xs)
{
boolean first = true;
for(int x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs)
{
boolean first = true;
for(long x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln()
{
return write((byte)'\n');
}
public FastWriter writeln(int... xs)
{
return write(xs).writeln();
}
public FastWriter writeln(long... xs)
{
return write(xs).writeln();
}
public FastWriter writeln(char[] line)
{
return write(line).writeln();
}
public FastWriter writeln(char[]... map)
{
for(char[] line : map)write(line).writeln();
return this;
}
public FastWriter writeln(String s)
{
return write(s).writeln();
}
private void innerflush()
{
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush()
{
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) { return write(b); }
public FastWriter print(char c) { return write(c); }
public FastWriter print(char[] s) { return write(s); }
public FastWriter print(String s) { return write(s); }
public FastWriter print(int x) { return write(x); }
public FastWriter print(long x) { return write(x); }
public FastWriter print(double x, int precision) { return write(x, precision); }
public FastWriter println(char c){ return writeln(c); }
public FastWriter println(int x){ return writeln(x); }
public FastWriter println(long x){ return writeln(x); }
public FastWriter println(double x, int precision){ return writeln(x, precision); }
public FastWriter print(int... xs) { return write(xs); }
public FastWriter print(long... xs) { return write(xs); }
public FastWriter println(int... xs) { return writeln(xs); }
public FastWriter println(long... xs) { return writeln(xs); }
public FastWriter println(char[] line) { return writeln(line); }
public FastWriter println(char[]... map) { return writeln(map); }
public FastWriter println(String s) { return writeln(s); }
public FastWriter println() { return writeln(); }
}
public void trnz(int... o)
{
for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" ");
System.out.println();
}
// print ids which are 1
public void trt(long... o)
{
Queue<Integer> stands = new ArrayDeque<>();
for(int i = 0;i < o.length;i++){
for(long x = o[i];x != 0;x &= x-1)stands.add(i<<6|Long.numberOfTrailingZeros(x));
}
System.out.println(stands);
}
public void tf(boolean... r)
{
for(boolean x : r)System.out.print(x?'#':'.');
System.out.println();
}
public void tf(boolean[]... b)
{
for(boolean[] r : b) {
for(boolean x : r)System.out.print(x?'#':'.');
System.out.println();
}
System.out.println();
}
public void tf(long[]... b)
{
if(INPUT.length() != 0) {
for (long[] r : b) {
for (long x : r) {
for (int i = 0; i < 64; i++) {
System.out.print(x << ~i < 0 ? '#' : '.');
}
}
System.out.println();
}
System.out.println();
}
}
public void tf(long... b)
{
if(INPUT.length() != 0) {
for (long x : b) {
for (int i = 0; i < 64; i++) {
System.out.print(x << ~i < 0 ? '#' : '.');
}
}
System.out.println();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 11 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | b2aa070ef44e6d8912b026923a27a2ad | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | /*
Goal: Become better in CP!
Key: Consistency and Discipline
Desire: SDE @ Google USA
Motto: Do what i Love <=> Love what i do
If you don't use your brain 100%, it deteriorates gradually
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class Solution {
static StringBuffer str=new StringBuffer();
static BufferedReader bf;
static PrintWriter pw;
static int n;
static long c[];
// https://codeforces.com/blog/entry/94384?#comment-838048
static void solve(int te) throws Exception{
long ans=0;
for(int l=0;l<n;l+=2){
long min_balance=0;
long balance=0;
for(int r=l+1;r<n;r++){
if(r%2==0){
balance+=c[r];
}else{
if(r==l+1) ans+=Math.min(c[l], c[r]);
else if(c[l]>=min_balance && c[r]>=min_balance+balance){
ans+=Math.min(c[l]-min_balance, c[r]-(min_balance+balance))+1;
}else if(c[l]<min_balance) break;
balance-=c[r];
min_balance=Math.max(-balance, min_balance);
}
}
}
str.append(ans).append("\n");
}
public static void main(String[] args) throws java.lang.Exception {
boolean lenv=false;
int te=1;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
// int q1 = Integer.parseInt(bf.readLine().trim());
// for(te=1;te<=q1;te++) {
n=Integer.parseInt(bf.readLine().trim());
String s[]=bf.readLine().trim().split("\\s+");
c=new long[n];
for(int i=0;i<n;i++) c[i]=Long.parseLong(s[i]);
solve(te);
// }
pw.print(str);
pw.flush();
// System.out.println(str);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 23cd59e98849e5444328e658f53743b1 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | /*
Goal: Become better in CP!
Key: Consistency and Discipline
Desire: SDE @ Google USA
Motto: Do what i Love <=> Love what i do
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class Coder {
static StringBuffer str=new StringBuffer();
static int n;
static long []c;
static void solve(){
long ans=0;
for(int l=0;l<n;l+=2){
long min_balance=c[l];
long balance=c[l];
for(int r=l+1;r<n;r++){
if(r%2==0){
// adding the open brackets to cur
balance+=c[r];
}else{
//
long left=Math.max(0, balance-c[r]);
long right=Math.min(min_balance, Math.min(balance-1, c[l]-1));
if(left<=right){
ans+=right-left+1;
}
// removing closed brackets from open
balance-=c[r];
min_balance=Math.min(min_balance, balance);
}
}
}
str.append(ans);
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader bf;
PrintWriter pw;
boolean lenv=false;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
// int q1 = Integer.parseInt(bf.readLine().trim());
// while (q1-- > 0) {
n=Integer.parseInt(bf.readLine().trim());
String s[]=bf.readLine().trim().split("\\s+");
c=new long[n];
for(int i=0;i<n;i++) c[i]=Long.parseLong(s[i]);
solve();
// }
pw.println(str);
pw.flush();
// System.outin.print(str);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | efa720e1ecdeae8adf74d4a6235fa1e1 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
/*
Goal: Become better in CP!
Key: Consistency!
*/
public class Coder {
static StringBuffer str=new StringBuffer();
static int n;
static long c[];
static long dp[][];
static long ap[][];
static long open[];
static long closed[];
static void solve(){
dp=new long[n][n];
ap=new long[n][n];
open=new long[n];
closed=new long[n];
for(int i=0;i<n;i++){
if(i%2==0) open[i]+=c[i];
else closed[i]+=c[i];
}
for(int i=1;i<n;i++){
open[i]+=open[i-1];
closed[i]+=closed[i-1];
}
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
int j=i+k;
if(j>=n) continue;
long cur=c[i];
if(i%2==1) cur=-cur;
if(i==j){
ap[i][j]=cur;
continue;
}
ap[i][j]=Math.min(ap[i+1][j]+cur,cur);
}
}
for(int k=1;k<n;k++){
for(int i=0;i<n;i++){
int j=i+k;
if(j>=n) continue;
if((i == (j-1))){
if(i%2 == 0) dp[i][j] = Math.min(c[i],c[j]);
continue;
}
dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1];
if(i%2==1 || j%2==0) continue;
long mn=ap[i+1][j-1];
mn=Math.abs(mn);
long op = open[j-1]-open[i];
long cl= closed[j-1]-closed[i];
if(c[i]<mn) continue;
op+=mn;
if((op<cl) || c[j]<(op-cl)) continue;
dp[i][j]+=1;
long cll=c[j]-(op-cl);
long opl=c[i]-mn;
dp[i][j]+=Math.min(opl, cll);
}
}
str.append(dp[0][n-1]);
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader bf;
PrintWriter pw;
boolean lenv=false;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
// int t = Integer.parseInt(bf.readLine().trim());
// while (t-- > 0) {
n=Integer.parseInt(bf.readLine().trim());
c=new long[n];
String s[]=bf.readLine().trim().split("\\s+");
for(int i=0;i<n;i++){
c[i]=Long.parseLong(s[i]);
}
solve();
// }
pw.print(str);
pw.flush();
// System.out.print(str);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 39cc18750c0b7eb1a907e43734362d42 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Objects;
import java.util.StringTokenizer;
public class Codeforces4 {
static class Node
{
int val;
Node left;
Node right;
public Node(int x) {
// TODO Auto-generated constructor stub
this.val=x;
this.left=null;
this.right=null;
}
}
static class Pair<U, V> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair<U, V> o) {
int value = ((Comparable<U>) x).compareTo(o.x);
if (value != 0) return value;
return ((Comparable<V>) y).compareTo(o.y);
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return x.equals(pair.x) && y.equals(pair.y);
}
public int hashCode() {
return Objects.hash(x, y);
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int[] nextArray(int n)
{
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=nextInt();
return arr;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader fs=new FastReader();
StringBuilder sb=new StringBuilder();
// int T=fs.nextInt();
// while(T-->0)
{
int n=fs.nextInt();
int[] arr=fs.nextArray(n);
long ans=0;
for(int i=0;i<n;i+=2)
{
long curr=arr[i],extra=0,prev=0;
for(int j=i+1;j<n;j+=2)
{
if(j==i+1)
{
if(arr[j]>arr[i])
{
ans+=arr[i];
break;
}
else {
// ans+=arr[j];
curr-=arr[j];
}
}
else {
int val=arr[j]-arr[j-1];
// prev++;
if(curr+extra-val<0)
{
curr=0;
ans++;
break;
}
// prev++;
extra-=val;
if(extra<=0)
{
curr+=extra;
ans++;
prev=0;
extra=0;
}
}
}
ans+=arr[i]-curr;
// System.out.println(ans+","+curr);
}
sb.append(ans);
sb.append("\n");
}
System.out.println(sb);
}
}
/*
18,18
29,30
51,19
80,18
104,3
121,30
156,-1
197,0
211,14
237,26
254,1
299,-5
321,0
337,0
343,6
383,40
396,0
409,0
443,0
457,14
508,-2
557,0
608,0
644,0
654,10
654
*/
/*
18,18
23,30
39,19
62,18
80,3
91,30
117,3
158,4
172,14
198,26
211,1
227,22
249,3
265,9
271,6
311,40
324,2
337,2
371,20
385,14
406,28
455,17
506,3
542,10
552,10
552
*/
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 7e23e11b39aad1ab2c9ed32ebd2ba861 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class practiceb{
static FastScanner sc;
static TreeSet<Integer> ans;
static long[] arr,dp;
static char[][] board;
static long mul,mul1;
static int[] spf;
static char[] ch;
static int n,q,m;
static long k,a,x,c;
static StringBuilder sb,sb1;
static long b,sum;
static Map<Integer,List<Integer>> map;
static Map<Long,List<Integer>> map2;
static Map<Long,List<Integer>> map3;
static Map<Integer,Integer> shortest;
static Map<Integer,List<Integer>> map1;
static List<Long> list,list1;
static int[] count;
// static class Node{
// long dist;
// Set<Integer> set;
// Node(){
// this.set = new HashSet<>();
// }
// Node(int f){
// this.dist = f;
// this.set = new HashSet<>();
// }
// }
// public static class Lca {
// int[] depth;
// int[] dfs_order;
// int cnt;
// int[] first;
// int[] minPos;
// int n;
// void dfs(List<Integer>[] tree, int u, int d) {
// depth[u] = d;
// dfs_order[cnt++] = u;
// for (int v : tree[u])
// if (depth[v] == -1) {
// dfs(tree, v, d + 1);
// dfs_order[cnt++] = u;
// }
// }
// void buildTree(int node, int left, int right) {
// if (left == right) {
// minPos[node] = dfs_order[left];
// return;
// }
// int mid = (left + right) >> 1;
// buildTree(2 * node + 1, left, mid);
// buildTree(2 * node + 2, mid + 1, right);
// minPos[node] = depth[minPos[2 * node + 1]] < depth[minPos[2 * node + 2]] ? minPos[2 * node + 1] : minPos[2 * node + 2];
// }
// public Lca(List<Integer>[] tree, int root) {
// int nodes = tree.length;
// depth = new int[nodes];
// Arrays.fill(depth, -1);
// n = 2 * nodes - 1;
// dfs_order = new int[n];
// cnt = 0;
// dfs(tree, root, 0);
// minPos = new int[4 * n];
// buildTree(0, 0, n - 1);
// first = new int[nodes];
// Arrays.fill(first, -1);
// for (int i = 0; i < dfs_order.length; i++)
// if (first[dfs_order[i]] == -1)
// first[dfs_order[i]] = i;
// }
// public int lca(int a, int b) {
// return minPos(Math.min(first[a], first[b]), Math.max(first[a], first[b]), 0, 0, n - 1);
// }
// int minPos(int a, int b, int node, int left, int right) {
// if (a == left && right == b)
// return minPos[node];
// int mid = (left + right) >> 1;
// if (a <= mid && b > mid) {
// int p1 = minPos(a, Math.min(b, mid), 2 * node + 1, left, mid);
// int p2 = minPos(Math.max(a, mid + 1), b, 2 * node + 2, mid + 1, right);
// return depth[p1] < depth[p2] ? p1 : p2;
// } else if (a <= mid) {
// return minPos(a, Math.min(b, mid), 2 * node + 1, left, mid);
// } else if (b > mid) {
// return minPos(Math.max(a, mid + 1), b, 2 * node + 2, mid + 1, right);
// } else {
// throw new RuntimeException();
// }
// }
// }
// private static long dfs(int v, int parent,long ht) {
// List<Integer> list = map.get(v);
// hyt[v] = ht;
// if(list.size() == 1){
// if(!map3.containsKey(ht))
// map3.put(ht,new ArrayList<>());
// map3.get(ht).add(v);
// return ht;
// }
// long maxl = -1;
// for(Integer i : list){
// if(i == parent)
// continue;
// maxl = Math.max(maxl,dfs(i,v,ht+1));
// }
// return maxl;
// }
// public static int N = 100005;
// long a[N];
// long sfx[N];
// long pfx[N];
// public static boolean f(int width){
//
// // long[] a = new long[n];
// long[] sfx = new long[n];
// long[] pfx = new long[n];
// for (int j = 0; j < n; j++){
// if (j % width == 0){
// pfx[j] = a[j];
// }
// else pfx[j] = gcd(pfx[j - 1], a[j]);
// }
// for (int j = n - 2; j >= 0; j--){
// if (j % width == width - 1){
// sfx[j] = a[j];
// }
// else sfx[j] = gcd(sfx[j + 1], a[j]);
// }
//
// for(int j = 0; j < n - width + 1 ; j++){
// if (gcd(sfx[j], pfx[j + width - 1]) >= k) return true;
// if (((j % width) == width - 1) && pfx[j] >= k){
// return true;
// }
// }
// return false;
// }
public static void solve() {
long ans = 0;
int[] close = new int[n];
for(int i = 0 ; i < n ; i++) {
if(i%2 == 1) {
long closings = arr[i];
for(int j = i-1 ; j > -1; ) {
if(arr[j] >= closings) {
ans += closings;
if(arr[i] > 0)
close[i] = j;
arr[i] = 0;
arr[j] -= closings;
closings = 0;
}
else {
closings -= arr[j];
arr[i] -= arr[j];
ans += arr[j];
arr[j] = 0;
}
if(j == 0)
break;
if((arr[j] > 0) || (arr[j-1] > 0))
break;
if(arr[j - 1] == 0) {
ans += 1;
j = close[j-1];
}
}
}
}
System.out.println(ans);
}
private static long find(long num, long a) {
String sa = String.valueOf(a);
String snum = String.valueOf(num);
char[] chNum = snum.toCharArray();
char[] cha = sa.toCharArray();
int curr = 0;
long op = 0;
for(int i = 0 ; i < chNum.length; i++) {
while(curr < cha.length && chNum[i] != cha[curr]) {
curr += 1;
op += 1;
}
if(curr >= cha.length)
op += 1;
curr += 1;
}
if(curr < cha.length)
op += cha.length-curr;
return op;
}
public static void main(String[] args) {
sc = new FastScanner();
// Scanner sc = new Scanner(System.in);
// SpecialSieve(5000000+5);
// long num = 1;
// list = new ArrayList<>();
// for(int i = 0 ; i <= 60 ; i++) {
// list.add(num);
// num *= 2;
// }
// int t = sc.nextInt();
sb = new StringBuilder();
int t = 1;
while(t > 0){
// a = sc.nextLong();
// b = sc.nextLong();
// c = sc.nextLong();
n = sc.nextInt();
// x = sc.nextLong();
// m = sc.nextInt();
// mat = new int[2][m];
// for(int i = 0 ; i < 2 ; i++)
// for(int j = 0 ; j < m ; j++)
// mat[i][j] = sc.nextLong();
// for(int i = 1 ; i <= 15 ; i++){
// m = i;
// solve();
// // }
// board = new char[3][3];
// for(int i = 0 ; i < 3 ; i++){
// String s = sc.next();
// for(int j = 0 ; j < 3 ; j++){
// board[i][j] = s.charAt(j);
// }
// }
// x = sc.nextInt();
// ch = sc.next().toCharArray();
// q = sc.nextInt();
// ch = sc.next().toCharArray();
arr = new long[n];
for(int i = 0 ; i < n ; i++)
arr[i] = sc.nextLong();
//
// a = new long[n-1];
// for(int i = 1 ; i < n ; i++)
// a[i-1] = Math.abs(arr[i] - arr[i-1]);
//
// n -= 1;
solve();
t -= 1;
}
// System.out.print(sb);
}
// Use this instead of Arrays.sort() on an array of ints. Arrays.sort() is n^2
// worst case since it uses a version of quicksort. Although this would never
// actually show up in the real world, in codeforces, people can hack, so
// this is needed.
static void ruffleSort(long[] a) {
//ruffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n);long temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
public static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int log(double n,double base){
if(n == 0 || n == 1)
return 0;
if(n == base)
return 1;
double num = Math.log(n);
double den = Math.log(base);
if(den == 0)
return 0;
return (int)(num/den);
}
public static long mod(long x, long mod)
{
long result = x % mod;
if (result < 0)
{
result += mod;
}
return result;
}
// Use this to input code since it is faster than a Scanner
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | a73bc13d413af9143f51076b1aaa9a35 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | /*
stream Butter!
eggyHide eggyVengeance
I need U
xiao rerun when
*/
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class x1556C
{
public static void main(String hi[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int[] arr = readArr(N, infile, st);
long res = 0L;
for(int left=0; left < N; left+=2)
{
long middle = 0L;
long worst = 0L;
for(int right=left+1; right < N; right+=2)
{
if(arr[left] >= -1*worst)
{
long minRight = -1*worst+middle;
if(minRight <= arr[right])
{
long low = -1*worst;
long high = arr[left];
while(low != high)
{
long mid = (low+high+1)>>1;
if(mid+middle <= arr[right])
low = mid;
else
high = mid-1;
}
//System.out.println(left+" "+right+" "+low+" "+(-1*worst));
res += low+worst;
if(worst != 0)
res++;
}
}
middle -= arr[right];
worst = min(worst, middle);
if(right+1 < N)
middle += arr[right+1];
}
}
System.out.println(res);
}
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;
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | f3fab94b265aa75d7ddc644269df248f | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* #
*
* @author pttrung
*/
public class C_Deltix_Summer_2021 {
public static long MOD = 1000000007;
public static void main(String[] args) throws FileNotFoundException {
// PrintWriter out = new PrintWriter(new FileOutputStream(new File(
// "output.txt")));
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
int n = in.nextInt();
long[] data = new long[n];
long[] dp = new long[n];
long re = 0;
for (int i = 0; i < n; i++) {
data[i] = in.nextInt();
if (i % 2 == 0) {
continue;
}
long close = 0;
dp[i] = Long.min(data[i], data[i - 1]);
long left = data[i] - data[i - 1];
for (int j = i - 2; j > 0 && left >= 0; j -= 2) {
if (data[j - 1] > data[j]) {
long a = data[j - 1] - data[j];
if (a >= close) {
dp[i] += Long.min(a - close, left) + 1;
left -= a - close;
close = 0;
} else {
close -= a;
}
continue;
}
close += data[j] - data[j - 1];
if (close == 0) {
dp[i]++;
}
}
re += dp[i];
}
out.println(re);
out.close();
}
static long abs(long a) {
return a < 0 ? -a : a;
}
public static int[] KMP(String val) {
int i = 0;
int j = -1;
int[] result = new int[val.length() + 1];
result[0] = -1;
while (i < val.length()) {
while (j >= 0 && val.charAt(j) != val.charAt(i)) {
j = result[j];
}
j++;
i++;
result[i] = j;
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static int digit(long n) {
int result = 0;
while (n > 0) {
n /= 10;
result++;
}
return result;
}
public static double dist(long a, long b, long x, long y) {
double val = (b - a) * (b - a) + (x - y) * (x - y);
val = Math.sqrt(val);
double other = x * x + a * a;
other = Math.sqrt(other);
return val + other;
}
public static class Point implements Comparable<Point> {
int x;
long y;
public Point(int start, long end) {
this.x = start;
this.y = end;
}
@Override
public int compareTo(Point o) {
return Integer.compare(x, o.x);
}
}
public static class FT {
long[] data;
FT(int n) {
data = new long[n];
}
public void update(int index, long value) {
while (index < data.length) {
data[index] += value;
index += (index & (-index));
}
}
public long get(int index) {
long result = 0;
while (index > 0) {
result += data[index];
index -= (index & (-index));
}
return result;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long pow(long a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return val * val;
} else {
return val * (val * a);
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
// System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
//br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | d9dab339a2a6b33a88fb5c5080d75c46 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | /*==========================================================================
* AUTHOR: RonWonWon
* CREATED: 03.09.2021 11:21:20
/*==========================================================================*/
import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) throws IOException {
int n = in.nextInt();
int a[] = in.readArray(n);
if(n%2==1) n--;
long ans = 0;
for(int i=0;i<n;i+=2){
ans += Math.min(a[i],a[i+1]);
long sum = a[i]-a[i+1], neg = -sum;
int j = i-2;
//out.println(i+" "+ans);
while(j>-1 && sum<=0){
int d = a[j] - a[j+1];
sum += d;
if(d>=0){
if(sum<=0){
if(-sum<=neg){
ans += Math.min(d+1,neg+sum+1);
neg = -sum;
}
}
else{
ans += Math.min(d+1,neg+1);
}
}
j-=2;
}
//out.println(i+" "+ans);
}
out.println(ans);
out.flush();
}
static FastScanner in = new FastScanner();
static PrintWriter out = new PrintWriter(System.out);
static int oo = Integer.MAX_VALUE;
static long ooo = Long.MAX_VALUE;
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
void ini() throws FileNotFoundException {
br = new BufferedReader(new FileReader("input.txt"));
}
String next() {
while(!st.hasMoreTokens())
try { st = new StringTokenizer(br.readLine()); }
catch(IOException e) {}
return st.nextToken();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
long[] readArrayL(int n) {
long a[] = new long[n];
for(int i=0;i<n;i++) a[i] = nextLong();
return a;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] readArrayD(int n) {
double a[] = new double[n];
for(int i=0;i<n;i++) a[i] = nextDouble();
return a;
}
}
static final Random random = new Random();
static void ruffleSort(int[] a){
int n = a.length;
for(int i=0;i<n;i++){
int j = random.nextInt(n), temp = a[j];
a[j] = a[i]; a[i] = temp;
}
Arrays.sort(a);
}
static void ruffleSortL(long[] a){
int n = a.length;
for(int i=0;i<n;i++){
int j = random.nextInt(n);
long temp = a[j];
a[j] = a[i]; a[i] = temp;
}
Arrays.sort(a);
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 500f0bd78666b69e87128052fd6febd7 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
// array[i] = random.nextInt(100000)+1;
// System.out.println(array[i]);
}
if(n==1){
System.out.println(0);
return;
}
long cnt = 0;
Stack<node> stack = new Stack<>();
int[] provide = new int[n];
int lc = array[0];
int rc = array[1];
if (lc == rc) {
cnt += lc;
provide[0] = 1;
} else if (lc > rc) {
cnt += rc;
stack.push(new node(lc - rc, 0));
provide[0] = 1;
} else if (lc < rc) {
cnt += lc;
provide[0] = 0;
}
for (int i = 2; i < (array.length % 2 == 0 ? array.length : array.length - 1); i += 2) {
int l = array[i];
int r = array[i + 1];
if (l == r) {
cnt += l;
cnt += provide[i - 2];
provide[i] = provide[i - 2] + 1;
} else if (l > r) {
cnt += r;
stack.push(new node(l - r, i));
provide[i] = 1;
} else if (l < r) {
cnt += l;
cnt+=provide[i-2];
int cur = r - l;
while (true) {
if(stack.empty()){
provide[i] = 0;
break;
}
node peek = stack.peek();
if (peek.count > cur){
cnt+=cur;
peek.count -= cur;
provide[i] = 1;
break;
}else if(peek.count==cur){
cnt+=cur;
cnt+=peek.index==0?0:provide[peek.index-2];
stack.pop();
provide[i] = peek.index==0?0:provide[peek.index-2]+1;
break;
}else{
cnt+=peek.count;
cnt+=peek.index==0?0:provide[peek.index-2];
stack.pop();
cur-=peek.count;
}
}
}
}
System.out.println(cnt);
}
static class node {
long count;
int index;
public node(long count, int index) {
this.count = count;
this.index = index;
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | ebfe74325d7f4c50548886a19c0bb186 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | //package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner scanner = new Scanner(System.in);
//. int t = scanner.nextInt();
//int casen = 1;
int n = scanner.nextInt();
long[] arr = new long[n];
for(int i =0; i < n;i++)
arr[i] = scanner.nextLong();
long sol=0;
for(int i =1; i < n;i++)
{
long open = 0;
long from=0;
long closes=0;
from+=Math.min(arr[i-1],arr[i]);
open = arr[i-1]-arr[i];
int j =i+2;
long total = 0;
while(j<n&&open>=0)
{
closes +=arr[j]-arr[j-1];
if(closes>=0)
{
if(arr[j]-arr[j-1]>=0)
from += Math.min(open,Math.min(arr[j]-arr[j-1],closes))+1;
open -= closes;
closes=0;
}
j+=2;
}
sol += from;
i++;
}
System.out.println(sol);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | a9ca925af5eaab93abba5b51ae5711fe | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.math.BigInteger;
public final class A
{
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans=new StringBuilder();
static FastReader in=new FastReader();
static ArrayList<Pair> g[];
static long mod=(long)998244353,INF=Long.MAX_VALUE;
static boolean set[],col[];
static int par[],tot[],partial[];
static int D[],P[][];
static int dp[][],sum=0,size[];
static int seg[];
static ArrayList<Long> A;
// static HashSet<Integer> set;
// static node1 seg[];
//static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)};
public static void main(String args[])throws IOException
{
// int T=i();
// outer:while(T-->0)
//
// 8
// 1 1 1 1 3 1 1 3
// {
int N=i();
long A[]=inputLong(N);
Stack<Integer> st=new Stack<>();
int dp[]=new int[N+5];
long s=0;
for(int i=0; i<N-1; i+=2)
{
long a=A[i],b=A[i+1];
if(a==b)
{
s+=Math.min(a, b);
dp[i+2]=1+dp[i];
s+=dp[i];
}
else if(a>b)
{
s+=Math.min(a, b);
A[i]-=Math.min(a, b);
st.push(i);
dp[i+2]=1;
}
else
{
st.push(i);
while(st.size()>0 && b>0)
{
int index=st.pop();
//System.out.println(index);
if(A[index]<=b)
{
s+=A[index];
b-=A[index];
s+=dp[index];
dp[i+2]=dp[index]+1;
}
else
{
s+=b;
A[index]-=b;
st.push(index);
b=0;
dp[i+2]=1;
}
//System.out.println("s--> "+s);
}
if(b>0)
dp[i+2]=0;
}
}
//print(dp);
out.println(s);
out.close();
}
static long and(int i,int j)
{
System.out.println("and "+i+" "+j);
return l();
}
static long or(int i,int j)
{
System.out.println("or "+i+" "+j);
return l();
}
static int len=0,number=0;
static void f(char X[],int i,int num,int l)
{
if(i==X.length)
{
if(num==0)return;
//update our num
if(isPrime(num))return;
if(l<len)
{
len=l;
number=num;
}
return;
}
int a=X[i]-'0';
f(X,i+1,num*10+a,l+1);
f(X,i+1,num,l);
}
static boolean is_Sorted(int A[])
{
int N=A.length;
for(int i=1; i<=N; i++)if(A[i-1]!=i)return false;
return true;
}
static boolean f(StringBuilder sb,String Y,String order)
{
StringBuilder res=new StringBuilder(sb.toString());
HashSet<Character> set=new HashSet<>();
for(char ch:order.toCharArray())
{
set.add(ch);
for(int i=0; i<sb.length(); i++)
{
char x=sb.charAt(i);
if(set.contains(x))continue;
res.append(x);
}
}
String str=res.toString();
return str.equals(Y);
}
static boolean all_Zero(int f[])
{
for(int a:f)if(a!=0)return false;
return true;
}
static long form(int a,int l)
{
long x=0;
while(l-->0)
{
x*=10;
x+=a;
}
return x;
}
static int count(String X)
{
HashSet<Integer> set=new HashSet<>();
for(char x:X.toCharArray())set.add(x-'0');
return set.size();
}
static int f(long K)
{
long l=0,r=K;
while(r-l>1)
{
long m=(l+r)/2;
if(m*m<K)l=m;
else r=m;
}
return (int)l;
}
// static void build(int v,int tl,int tr,long A[])
// {
// if(tl==tr)
// {
// seg[v]=A[tl];
// }
// else
// {
// int tm=(tl+tr)/2;
// build(v*2,tl,tm,A);
// build(v*2+1,tm+1,tr,A);
// seg[v]=Math.min(seg[v*2], seg[v*2+1]);
// }
// }
static long ask(int v,int tl,int tr,int l,int r)
{
if(l>r)return (long)(1e10);
if(tl==l && tr==r)
{
return seg[v];
}
int tm=(tl+tr)/2;
return Math.min(ask(v*2,tl,tm,l,Math.min(tm, r)), ask(v*2+1,tm+1,tr,Math.max(l,tm+1),r));
}
static int [] sub(int A[],int B[])
{
int N=A.length;
int f[]=new int[N];
for(int i=N-1; i>=0; i--)
{
if(B[i]<A[i])
{
B[i]+=26;
B[i-1]-=1;
}
f[i]=B[i]-A[i];
}
for(int i=0; i<N; i++)
{
if(f[i]%2!=0)f[i+1]+=26;
f[i]/=2;
}
return f;
}
static int[] f(int N)
{
char X[]=in.next().toCharArray();
int A[]=new int[N];
for(int i=0; i<N; i++)A[i]=X[i]-'a';
return A;
}
static int max(int a ,int b,int c,int d)
{
a=Math.max(a, b);
c=Math.max(c,d);
return Math.max(a, c);
}
static int min(int a ,int b,int c,int d)
{
a=Math.min(a, b);
c=Math.min(c,d);
return Math.min(a, c);
}
static HashMap<Integer,Integer> Hash(int A[])
{
HashMap<Integer,Integer> mp=new HashMap<>();
for(int a:A)
{
int f=mp.getOrDefault(a,0)+1;
mp.put(a, f);
}
return mp;
}
static long mul(long a, long b)
{
return ( a %mod * 1L * b%mod )%mod;
}
static void swap(int A[],int a,int b)
{
int t=A[a];
A[a]=A[b];
A[b]=t;
}
static int find(int a)
{
if(par[a]<0)return a;
return par[a]=find(par[a]);
}
static void union(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
{
par[a]+=par[b];
par[b]=a;
}
}
static boolean isSorted(int A[])
{
for(int i=1; i<A.length; i++)
{
if(A[i]<A[i-1])return false;
}
return true;
}
static boolean isDivisible(StringBuilder X,int i,long num)
{
long r=0;
for(; i<X.length(); i++)
{
r=r*10+(X.charAt(i)-'0');
r=r%num;
}
return r==0;
}
static int lower_Bound(int A[],int low,int high, int x)
{
if (low > high)
if (x >= A[high])
return A[high];
int mid = (low + high) / 2;
if (A[mid] == x)
return A[mid];
if (mid > 0 && A[mid - 1] <= x && x < A[mid])
return A[mid - 1];
if (x < A[mid])
return lower_Bound( A, low, mid - 1, x);
return lower_Bound(A, mid + 1, high, x);
}
static String f(String A)
{
String X="";
for(int i=A.length()-1; i>=0; i--)
{
int c=A.charAt(i)-'0';
X+=(c+1)%2;
}
return X;
}
static void sort(long[] a) //check for long
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static String swap(String X,int i,int j)
{
char ch[]=X.toCharArray();
char a=ch[i];
ch[i]=ch[j];
ch[j]=a;
return new String(ch);
}
static int sD(long n)
{
if (n % 2 == 0 )
return 2;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0 )
return i;
}
return (int)n;
}
static void setGraph(int N)
{
tot=new int[N+1];
partial=new int[N+1];
D=new int[N+1];
P=new int[N+1][(int)(Math.log(N)+10)];
// set=new boolean[N+1];
g=new ArrayList[N+1];
for(int i=0; i<=N; i++)
{
g[i]=new ArrayList<>();
D[i]=Integer.MAX_VALUE;
//D2[i]=INF;
}
}
static long pow(long a,long b)
{
//long mod=1000000007;
long pow=1;
long x=a;
while(b!=0)
{
if((b&1)!=0)pow=(pow*x)%mod;
x=(x*x)%mod;
b/=2;
}
return pow;
}
static long toggleBits(long x)//one's complement || Toggle bits
{
int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
return ((1<<n)-1)^x;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
static int kadane(int A[])
{
int lsum=A[0],gsum=A[0];
for(int i=1; i<A.length; i++)
{
lsum=Math.max(lsum+A[i],A[i]);
gsum=Math.max(gsum,lsum);
}
return gsum;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static void print(char A[])
{
for(char c:A)System.out.print(c+" ");
System.out.println();
}
static void print(boolean A[])
{
for(boolean c:A)System.out.print(c+" ");
System.out.println();
}
static void print(int A[])
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static void print(long A[])
{
for(long i:A)System.out.print(i+ " ");
System.out.println();
}
static void print(boolean A[][])
{
for(boolean a[]:A)print(a);
}
static void print(long A[][])
{
for(long a[]:A)print(a);
}
static void print(int A[][])
{
for(int a[]:A)print(a);
}
static void print(ArrayList<Integer> A)
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
static long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
}
class Pair implements Comparable<Pair>
{
long min,size;
Pair(long min,long size)
{
this.min=min;
this.size=size;
}
public int compareTo(Pair x)
{
if(this.min==x.min)
{
return 0;
}
if(this.min>x.min)return 1;
else return -1;
}
}
//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st==null || !st.hasMoreElements())
{
try
{
st=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | d9dd7665f6f5b97e0fbc4f3c81aff816 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public final class Solution {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st;
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(br.readLine()), i, j;
st = new StringTokenizer(br.readLine());
int arr[] = new int[n];
for (i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
long tot = 0l;
for (i = 1; i < n; i += 2) {
if (arr[i - 1] > arr[i]) {
tot += 1l * arr[i];
} else {
tot += 1l * arr[i - 1];
long diff = 1l * (arr[i] - arr[i - 1]);
long temp = 0l;
for (j = i - 2; j >= 0; j--) {
if (j % 2 == 0) {
temp += 1l * arr[j];
} else {
temp -= 1l * arr[j];
}
if (temp == 0) {
tot++;
} else if (temp > 0) {
if (temp <= diff) {
tot += temp + 1l;
diff -= temp;
temp = 0;
} else {
tot += diff + 1l;
break;
}
}
}
}
}
bw.write(Long.toString(tot) + "\n");
bw.flush();
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 0b6ffdcc16578a99d579b25e61f995ec | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public final class Solution {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st;
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(br.readLine()), i, j;
int arr[] = new int[n];
st = new StringTokenizer(br.readLine());
for (i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
long tot = 0l;
for (i = 1; i < n; i += 2) {
if (arr[i - 1] > arr[i]) {
tot += 1l * arr[i];
} else {
tot += 1l * arr[i - 1];
long diff = 1l * (arr[i] - arr[i - 1]);
// System.out.println(diff);
long sum = 0l;
for (j = i - 2; j >= 0; j--) {
if (j % 2 != 0) {
sum -= 1l * arr[j];
} else {
sum += 1l * arr[j];
}
if (sum == 0) {
tot += 1;
} else if (sum > 0) {
if (sum <= diff) {
tot += 1l * (sum) + 1l;
diff -= (sum);
sum = 0;
} else {
tot += 1l * (diff) + 1l;
diff = -1;
break;
}
}
}
}
// System.out.println("tot: " + tot);
}
bw.write(Long.toString(tot) + "\n");
bw.flush();
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 338331776f4a40678dc595a6b5be6548 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigDecimal;
import java.math.*;
public class Main{
static int total=0;
public static void main(String[] args) {
TaskA solver = new TaskA();
// int t = in.nextInt();
// for (int i = 1; i <= t ; i++) {
// solver.solve(i, in, out);
// }
solver.solve(1, in, out);
out.flush();
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n= in.nextInt();
long[]inp=new long[n];
for(int i=0;i<n;i++) {
inp[i]=in.nextInt();
}
long total=0;
for(int i=0;i<n;i+=2) {
for(int j=i+1;j<n;j+=2) {
if(j==i+1) {
total+=(Math.min(inp[i], inp[j]));
continue;
}
long balance=0;
long maxPos=inp[i]-inp[i+1];
if (maxPos<0){
break;
}
for(int k=i+2;k<j;k++) {
if(k%2==1) {
balance-=inp[k];
if(balance<0) {
if(balance+maxPos<0) {
break;
}
maxPos+=balance;
balance=0;
}
}
if(k%2==0) {balance+=inp[k];}
}
if(balance+maxPos<0||inp[j]-balance<0) {
continue;
}
long temp= inp[j];
temp-=balance;
total+=(1+Math.min(maxPos, temp));
}
}
out.println(total);
total=0;
}
}
static int bs(int size,int[]arr) {
int x = -1;
for (int b = size/2; b >= 1; b /= 2) {
while (!ok(arr));
}
int k = x+1;
return k;
}
static boolean ok(int[]x) {
return false;
}
public static int solve1(ArrayList<Integer> A) {
long[]arr =new long[A.size()+1];
int n=A.size();
for(int i=1;i<=A.size();i++) {
arr[i]=((i%2)*((n-i+1)%2))%2;
arr[i]%=2;
}
int ans=0;
for(int i=0;i<A.size();i++) {
if(arr[i+1]==1) {
ans^=A.get(i);
}
}
return ans;
}
public static String printBinary(long a) {
String str="";
for(int i=31;i>=0;i--) {
if((a&(1<<i))!=0) {
str+=1;
}
if((a&(1<<i))==0 && !str.isEmpty()) {
str+=0;
}
}
return str;
}
public static String reverse(long a) {
long rev=0;
String str="";
int x=(int)(Math.log(a)/Math.log(2))+1;
for(int i=0;i<32;i++) {
rev<<=1;
if((a&(1<<i))!=0) {
rev|=1;
str+=1;
}
else {
str+=0;
}
}
return str;
}
////////////////////////////////////////////////////////
static void sortF(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
return p1.first - p2.first;
}
});
}
static void sortS(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
return p1.second - p2.second;
}
});
}
static class Pair
{
int first ;int second ;
public Pair(int x, int y)
{
this.first = x ;this.second = y ;
}
@Override
public boolean equals(Object obj)
{
if(obj == this)return true ;
if(obj == null)return false ;
if(this.getClass() != obj.getClass()) return false ;
Pair other = (Pair)(obj) ;
if(this.first != other.first)return false ;
if(this.second != other.second)return false ;
return true ;
}
@Override
public int hashCode()
{
return this.first^this.second ;
}
@Override
public String toString() {
String ans = "" ;ans += this.first ; ans += " "; ans += this.second ;
return ans ;
}
}
//////////////////////////////////////////////////////////////////////////
static int nD(long num) {
String s=String.valueOf(num);
return s.length();
}
static int CommonDigits(int x,int y) {
String s=String.valueOf(x);
String s2=String.valueOf(y);
return 0;
}
static int lcs(String str1, String str2, int m, int n)
{
int L[][] = new int[m + 1][n + 1];
int i, j;
// Following steps build L[m+1][n+1] in
// bottom up fashion. Note that L[i][j]
// contains length of LCS of str1[0..i-1]
// and str2[0..j-1]
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (str1.charAt(i - 1)
== str2.charAt(j - 1))
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = Math.max(L[i - 1][j],
L[i][j - 1]);
}
}
// L[m][n] contains length of LCS
// for X[0..n-1] and Y[0..m-1]
return L[m][n];
}
/////////////////////////////////
boolean IsPowerOfTwo(int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
////////////////////////////////
static long power(long a,long b,long m ) {
long ans=1;
while(b>0) {
if(b%2==1) {
ans=((ans%m)*(a%m))%m; b--;
}
else {
a=(a*a)%m;b/=2;
}
}
return ans%m;
}
///////////////////////////////
public static boolean repeatedSubString(String string) {
return ((string + string).indexOf(string, 1) != string.length());
}
static int search(char[]c,int start,int end,char x) {
for(int i=start;i<end;i++) {
if(c[i]==x) {return i;}
}
return -2;
}
////////////////////////////////
static int gcd(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
static long fac(long a)
{
if(a== 0L || a==1L)return 1L ;
return a*fac(a-1L) ;
}
static ArrayList al() {
ArrayList<Integer>a =new ArrayList<>();
return a;
}
static HashSet h() {
return new HashSet<Integer>();
}
static void debug(int[][]a) {
int n= a.length;
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
out.print(a[i][j]+" ");
}
out.print("\n");
}
}
static void debug(int[]a) {
out.println(Arrays.toString(a));
}
static void debug(ArrayList<Integer>a) {
out.println(a.toString());
}
static boolean[]seive(int n){
boolean[]b=new boolean[n+1];
for (int i = 2; i <= n; i++)
b[i] = true;
for(int i=2;i*i<=n;i++) {
if(b[i]) {
for(int j=i*i;j<=n;j+=i) {
b[j]=false;
}
}
}
return b;
}
static int longestIncreasingSubseq(int[]arr) {
int[]sizes=new int[arr.length];
Arrays.fill(sizes, 1);
int max=1;
for(int i=1;i<arr.length;i++) {
for(int j=0;j<i;j++) {
if(arr[j]<arr[i]) {
sizes[i]=Math.max(sizes[i],sizes[j]+1);
max=Math.max(max, sizes[i]);
}
}
}
return max;
}
public static ArrayList primeFactors(long n)
{
ArrayList<Long>h= new ArrayList<>();
// Print the number of 2s that divide n
if(n%2 ==0) {h.add(2L);}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (long i = 3; i <= Math.sqrt(n); i+= 2)
{
if(n%i==0) {h.add(i);}
}
if (n > 2)
h.add(n);
return h;
}
static boolean Divisors(long n){
if(n%2==1) {
return true;
}
for (long i=2; i<=Math.sqrt(n); i++){
if (n%i==0 && i%2==1){
return true;
}
}
return false;
}
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(outputStream);
public static void superSet(int[]a,ArrayList<String>al,int i,String s) {
if(i==a.length) {
al.add(s);
return;
}
superSet(a,al,i+1,s);
superSet(a,al,i+1,s+a[i]+" ");
}
public static long[] makeArr() {
long size=in.nextInt();
long []arr=new long[(int)size];
for(int i=0;i<size;i++) {
arr[i]=in.nextInt();
}
return arr;
}
public static long[] arr(int n) {
long []arr=new long[n+1];
for(int i=1;i<n+1;i++) {
arr[i]=in.nextLong();
}
return arr;
}
public static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static void print(int c) {
out.print(c);
}
static void println(int x) {
out.println(x);
}
static void print(String s) {
out.print(s);
}
static void println(String s) {
out.println(s);
}
public static void merge(long arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long[n1];
long R[] = new long[n2];
//Copy data to temp arrays
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
public static void reverse(int[] array)
{
int n = array.length;
for (int i = 0; i < n / 2; i++) {
int temp = array[i];
array[i] = array[n - i - 1];
array[n - i - 1] = temp;
}
}
public static long nCr(int n,int k)
{
long ans=1L;
k=k>n-k?n-k:k;
for( int j=1;j<=k;j++,n--)
{
if(n%j==0)
{
ans*=n/j;
}else
if(ans%j==0)
{
ans=ans/j*n;
}else
{
ans=(ans*n)/j;
}
}
return ans;
}
static int searchMax(int index,long[]inp) {
while(index+1<inp.length &&inp[index+1]>inp[index]) {
index+=1;
}
return index;
}
static int searchMin(int index,long[]inp) {
while(index+1<inp.length &&inp[index+1]<inp[index]) {
index+=1;
}
return index;
}
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 897fff971296fe079f5c963b80d8900e | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.Random;
import java.io.FileWriter;
import java.io.PrintWriter;
/*
Solution Created: 19:59:09 29/08/2021
Custom Competitive programming helper.
*/
public class Main {
public static void solve() {
int n = in.nextInt();
int[] a = in.na(n);
long ans = 0;
for(int i = 0; i<n; i+=2) {
long bal = 0;
boolean open = true;
long leftToBeTaken = a[i];
for(int j = i; j<n; j++) {
if(open) {
bal += a[j];
}else {
long rangeL = 0, rangeR = leftToBeTaken;
// long extras = Math.max(0, bal-leftToBeTaken);
// long clearTake = Math.max(0, a[j]-extras);
long canL = Math.max(0, bal-a[j]), canR = Math.max(bal-1, 0);
if(!(canL>rangeR || canR<rangeL)) {
long take = Math.min(rangeR, canR)-Math.max(rangeL, canL)+1;
ans+=take;
}
bal -= a[j];
leftToBeTaken = Math.min(leftToBeTaken, bal);
if(bal<0) break;
}
open = !open;
}
}
out.println(ans);
}
public static void main(String[] args) {
in = new Reader();
out = new Writer();
int t = 1;
while(t-->0) solve();
out.exit();
}
static Reader in; static Writer out;
static class Reader {
static BufferedReader br;
static StringTokenizer st;
public Reader() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(String f){
try {
this.br = new BufferedReader(new FileReader(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
public double[] nd(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextDouble();
return a;
}
public long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
public char[] nca() {
return next().toCharArray();
}
public String[] ns(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) a[i] = next();
return a;
}
public int nextInt() {
ensureNext();
return Integer.parseInt(st.nextToken());
}
public double nextDouble() {
ensureNext();
return Double.parseDouble(st.nextToken());
}
public Long nextLong() {
ensureNext();
return Long.parseLong(st.nextToken());
}
public String next() {
ensureNext();
return st.nextToken();
}
public String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private void ensureNext() {
if (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static class Util{
private static Random random = new Random();
static long[] fact;
public static void initFactorial(int n, long mod) {
fact = new long[n+1];
fact[0] = 1;
for (int i = 1; i < n+1; i++) fact[i] = (fact[i - 1] * i) % mod;
}
public static long modInverse(long a, long MOD) {
long[] gcdE = gcdExtended(a, MOD);
if (gcdE[0] != 1) return -1; // Inverted doesn't exist
long x = gcdE[1];
return (x % MOD + MOD) % MOD;
}
public static long[] gcdExtended(long p, long q) {
if (q == 0) return new long[] { p, 1, 0 };
long[] vals = gcdExtended(q, p % q);
long tmp = vals[2];
vals[2] = vals[1] - (p / q) * vals[2];
vals[1] = tmp;
return vals;
}
public static long nCr(int n, int r, long MOD) {
if (r == 0) return 1;
return (fact[n] * modInverse(fact[r], MOD) % MOD * modInverse(fact[n - r], MOD) % MOD) % MOD;
}
public static long nCr(int n, int r) {
return (fact[n]/fact[r])/fact[n-r];
}
public static long nPr(int n, int r, long MOD) {
if (r == 0) return 1;
return (fact[n] * modInverse(fact[n - r], MOD) % MOD) % MOD;
}
public static long nPr(int n, int r) {
return fact[n]/fact[n-r];
}
public static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static boolean[] getSieve(int n) {
boolean[] isPrime = new boolean[n+1];
for (int i = 2; i <= n; i++) isPrime[i] = true;
for (int i = 2; i*i <= n; i++) if (isPrime[i])
for (int j = i; i*j <= n; j++) isPrime[i*j] = false;
return isPrime;
}
static long pow(long x, long pow, long mod){
long res = 1;
x = x % mod;
if (x == 0) return 0;
while (pow > 0){
if ((pow & 1) != 0) res = (res * x) % mod;
pow >>= 1;
x = (x * x) % mod;
}
return res;
}
public static int gcd(int a, int b) {
int tmp = 0;
while(b != 0) {
tmp = b;
b = a%b;
a = tmp;
}
return a;
}
public static long gcd(long a, long b) {
long tmp = 0;
while(b != 0) {
tmp = b;
b = a%b;
a = tmp;
}
return a;
}
public static int random(int min, int max) {
return random.nextInt(max-min+1)+min;
}
public static void dbg(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public static void reverse(int[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
int tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(int[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(long[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
long tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(long[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(float[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
float tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(float[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(double[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
double tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(double[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(char[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
char tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(char[] s) {
reverse(s, 0, s.length-1);
}
public static <T> void reverse(T[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
T tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static <T> void reverse(T[] s) {
reverse(s, 0, s.length-1);
}
public static void shuffle(int[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
int t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(long[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
long t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(float[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
float t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(double[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
double t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(char[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
char t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static <T> void shuffle(T[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
T t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void sortArray(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(long[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(float[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(double[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(char[] a) {
shuffle(a);
Arrays.sort(a);
}
public static <T extends Comparable<T>> void sortArray(T[] a) {
Arrays.sort(a);
}
}
static class Writer {
private PrintWriter pw;
public Writer(){
pw = new PrintWriter(System.out);
}
public Writer(String f){
try {
pw = new PrintWriter(new FileWriter(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public void yesNo(boolean condition) {
println(condition?"YES":"NO");
}
public void printArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void printArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void print(Object o) {
pw.print(o.toString());
}
public void println(Object o) {
pw.println(o.toString());
}
public void println() {
pw.println();
}
public void flush() {
pw.flush();
}
public void exit() {
pw.close();
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 3c525351df3027098c911419c75722ee | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
//RATING 1800
//Actually a C? but in a Deltix round, whatever that means
//ERRICHTO SOL: https://www.youtube.com/watch?v=7rtZEXAVzmk
public class D2D_CompressedBracketSeq {
static FastReader scan;
public static void main(String[] args) throws Exception{
scan = new FastReader();
int tests = 1;
boolean db = false;
if(tests == 0){
db = true;
tests = 100;
}
BufferedWriter print = new BufferedWriter(new OutputStreamWriter(System.out));
for(int testNum = 0; testNum < tests; testNum++){
solve(testNum, scan, print, db);
print.flush();
System.out.flush();
}
print.close();
}
public static void solve(int testNum, FastReader scan, BufferedWriter print, boolean db) throws Exception{
int n = scan.nextInt();
List<Integer> nums = readList(n, scan);
long[] runningL = new long[n];
long[] runningR = new long[n];
long[] diff = new long[n];
for (int i = 0; i < n; i++) {
if(i % 2 == 0){
long total = nums.get(i);
if(i - 2 >= 0) total += runningL[i - 2];
runningL[i] = total;
if(i > 0) runningR[i] = runningR[i-1];
}
else{
long total = nums.get(i);
if(i - 2 >= 0) total += runningR[i - 2];
runningR[i] = total;
if(i > 0) runningL[i] = runningL[i-1];
}
diff[i] = runningL[i] - runningR[i];
}
long[][] minDiff = new long[n][n];
for (int l = 0; l < n; l++) {
long curMin = 0;
for (int r = l; r < n; r++) {
if(l == r) curMin = diff[r];
curMin = Math.min(diff[r], curMin);
minDiff[l][r] = curMin;
}
}
long ans = 0;
for (int l = 0; l < n; l++) {
if(l % 2 == 1) continue;
for (int r = l + 1; r < n; r++) {
if(r % 2 == 0) continue;
long localR = runningR[r - 1] - runningR[l];
long localL = runningL[r] - runningL[l];
long localDiff = localL - localR;
long globalMin = minDiff[l + 1][r - 1];
long localMin = globalMin - (diff[l]);
if(r - l == 1) localMin = 0;
long minPl = Math.max(-localMin, 1);
long maxPl = Math.min(nums.get(l), (long)nums.get(r) - localDiff);
long numOptions = maxPl - minPl + 1;
if(numOptions < 0) continue;
//System.out.println("combo " + l + " " + r + " " + numOptions);
ans += numOptions;
}
}
//if(db) System.out.println("debug?");
print.write(ans + "\n");
}
//PREWRITTEN FUNCTIONS
public static void writeList(BufferedWriter print, int[] arr) throws Exception{
for (int i = 0; i < arr.length; i++) {
if(i < arr.length-1) print.write(arr[i] + " ");
else print.write(arr[i] + "");
}
}
public static void writeList(BufferedWriter print, List<Integer> arr) throws Exception{
for (int i = 0; i < arr.size(); i++) {
if(i < arr.size()-1) print.write(arr.get(i) + " ");
else print.write(arr.get(i) + "");
}
}
public static List<Integer> readList(int n, FastReader scan){
List<Integer> res = new ArrayList<Integer>();
for(int i = 0; i < n; i++){
int next = scan.nextInt();
res.add(next);
}
return res;
}
public List<List<Integer>> read2DListSpace(int r, int c, FastReader scan){
List<List<Integer>> list2d = new ArrayList<>();
for(int i = 0; i < r; i++){
List<Integer> curRow = new ArrayList<>();
for(int j = 0; j < c; j++){
int next = scan.nextInt();
curRow.add(next);
}
list2d.add(curRow);
}
return list2d;
}
//FastReader from https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/
//i really should switch to c++ but i keep finding things like this to convince myself Java is fast enough lol
static class FastReader {
BufferedReader br;
StringTokenizer st;
static boolean customInput = false;
static List<String> queue;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
queue = new LinkedList<>();
customInput = false;
}
public static void setInputMode(boolean custom){
customInput = custom;
}
public static void addToQueue(String item){
queue.add(item);
}
String next()
{
if(customInput){
String str = queue.get(0);
queue.remove(0);
return str;
}
else{
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 {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 34696f9fb86bbb8d0889e1f8782d3669 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
//--------------------------INPUT READER---------------------------------//
static class fs {
public BufferedReader br;
StringTokenizer st = new StringTokenizer("");
public fs() { this(System.in); }
public fs(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
String next() {
while (!st.hasMoreTokens()) {
try { st = new StringTokenizer(br.readLine()); }
catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String ns() { return next(); }
int[] na(long nn) {
int n = (int) nn;
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
long[] nal(long nn) {
int n = (int) nn;
long[] l = new long[n];
for(int i = 0; i < n; i++) l[i] = nl();
return l;
}
}
//-----------------------------------------------------------------------//
//---------------------------PRINTER-------------------------------------//
static class Printer {
static PrintWriter w;
public Printer() {this(System.out);}
public Printer(OutputStream os) {
w = new PrintWriter(os);
}
public void p(int i) {w.println(i);}
public void p(long l) {w.println(l);}
public void p(double d) {w.println(d);}
public void p(String s) { w.println(s);}
public void pr(int i) {w.print(i);}
public void pr(long l) {w.print(l);}
public void pr(double d) {w.print(d);}
public void pr(String s) { w.print(s);}
public void pl() {w.println();}
public void close() {w.close();}
}
//-----------------------------------------------------------------------//
//--------------------------VARIABLES------------------------------------//
static fs sc = new fs();
static OutputStream outputStream = System.out;
static Printer w = new Printer(outputStream);
static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE;
static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE;
static long mod = 1000000007;
//-----------------------------------------------------------------------//
//--------------------------ADMIN_MODE-----------------------------------//
private static void ADMIN_MODE() throws IOException {
if (System.getProperty("ONLINE_JUDGE") == null) {
w = new Printer(new FileOutputStream("output.txt"));
sc = new fs(new FileInputStream("input.txt"));
}
}
//-----------------------------------------------------------------------//
//----------------------------START--------------------------------------//
public static void main(String[] args)
throws IOException {
ADMIN_MODE();
//int t = sc.ni();while(t-->0)
solve();
w.close();
}
static void solve() throws IOException {
int n = sc.ni();
long[] arr = sc.nal(n);
long ans = 0;
ArrayDeque<Long> open = new ArrayDeque<>(), cont = new ArrayDeque<>();
for(int i = 0; i < n; i++) {
if(i%2==0) open.push(arr[i]);
else {
long curr = arr[i];
while(curr != 0) {
if(!open.isEmpty() && curr < open.peek()) {
long popped = open.pop();
open.push(popped - curr);
ans += curr;
cont.push(1L);
curr = 0;
} else if(!open.isEmpty() && curr == open.peek()) {
long popped = open.pop();
ans += curr;
curr = 0;
if(!cont.isEmpty()) cont.push(cont.pop()+1);
else cont.push(1L);
} else {
if(open.isEmpty()) {
while(!cont.isEmpty()) {
ans += calc(cont.pop());
}
curr = 0;
} else {
long popped = open.pop();
ans += popped;
if(!cont.isEmpty()) cont.push(cont.pop()+1);
if(!cont.isEmpty()) ans += calc(cont.pop());
curr -= popped;
}
}
}
}
}
while(!cont.isEmpty()) {
ans += calc(cont.pop());
}
w.p(ans);
}
static long calc(long n) {
return (n*(n+1))/2-n;
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 3279fa98292c31ae1c4bb802332dec7b | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class TaskD {
static long mod = 1000000007;
static FastScanner scanner;
static final StringBuilder result = new StringBuilder();
static int[] a, res, best;
// 1 3 5 3 1
// 1 3 4 6
// 1 1 3 3 4 5 3 1
// 1 1 3 3 4 5 6 3 1
public static void main(String[] args) {
scanner = new FastScanner();
solve();
}
static void solve() {
int n = scanner.nextInt();
int[] a = scanner.nextIntArray(n);
if (n == 1) {
System.out.println(0);
return;
}
LinkedList<Long> survived = new LinkedList<>();
long level = 0;
long res = 0;
Map<Long, Integer> cntPerLevel = new HashMap<>();
for (int i = 0; i < (n / 2) * 2; i += 2) {
level += a[i];
long nextLevel = level - a[i + 1];
while (!survived.isEmpty()) {
long prevSurvived = survived.peek();
if (nextLevel > prevSurvived) {
break;
}
res += cntPerLevel.get(prevSurvived);
if (nextLevel < prevSurvived) {
cntPerLevel.remove(prevSurvived);
}
survived.poll();
}
res += level - Math.max(nextLevel, 0);
level = Math.max(nextLevel, 0);
if (nextLevel >= 0) {
survived.push(level);
cntPerLevel.put(level, cntPerLevel.getOrDefault(level, 0) + 1);
}
}
System.out.println(res);
}
static boolean verify(int[][] notFilled, int[][] colors, int[][] a, int i, int j) {
if (i < 0 || i >= a.length || j < 0 || j >= a[0].length) {
return true;
}
if (a[i][j] != 0) {
return true;
}
int sum = sumAround(colors, i, j);
if (sum > 10) return false;
if (sum == 3) return false;
return true;
}
static void modify(TreeSet<WithIdx> toProcess, int[][] notFilled, int[][] a, int i, int j) {
if (i < 0 || i >= notFilled.length || j < 0 || j >= notFilled[0].length) {
return;
}
if (a[i][j] != 0) {
return;
}
int idx = i * 1000 + j;
int cnt = notFilled[i][j];
toProcess.remove(new WithIdx(cnt, idx));
cnt--;
notFilled[i][j] = cnt;
//if (cnt > 0) {
toProcess.add(new WithIdx(cnt, idx));
//}
}
static int fillAround(int[][] colors, int i, int j, int val) {
if (colors[i - 1][j] == -1) {
colors[i - 1][j] = val;
return (i - 1) * 1000 + j;
}
if (colors[i][j - 1] == -1) {
colors[i][j - 1] = val;
return (i) * 1000 + j - 1;
}
if (colors[i][j + 1] == -1) {
colors[i][j + 1] = val;
return (i) * 1000 + j + 1;
}
if (colors[i + 1][j] == -1) {
colors[i + 1][j] = val;
return (i + 1) * 1000 + j;
}
throw new IllegalArgumentException();
}
static int sumAround(int[][] colors, int i, int j) {
return forSum(colors, i - 1, j) + forSum(colors, i + 1, j) + forSum(colors, i, j + 1)
+ forSum(colors, i, j - 1);
}
static int forSum(int[][] colors, int i, int j) {
return colors[i][j] == -1 || colors[i][j] % 5 == 0 ? 0 : colors[i][j];
}
static int countFree(int[][] m, int i, int j) {
return m[i - 1][j] + m[i + 1][j] + m[i][j - 1] + m[i][j + 1];
}
static class WithIdx implements Comparable<WithIdx> {
static int[] order = {0, 1, 3, 2, 4};
int val, idx;
public WithIdx(int val, int idx) {
this.val = val;
this.idx = idx;
}
@Override
public int compareTo(WithIdx o) {
if (val == o.val) {
return Integer.compare(idx, o.idx);
}
return Integer.compare(order[val], order[o.val]);
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
int[] nextIntArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
long[] nextLongArray(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) res[i] = nextLong();
return res;
}
String[] nextStringArray(int n) {
String[] res = new String[n];
for (int i = 0; i < n; i++) res[i] = nextToken();
return res;
}
}
static class PrefixSums {
long[] sums;
public PrefixSums(long[] sums) {
this.sums = sums;
}
public long sum(int fromInclusive, int toExclusive) {
if (fromInclusive > toExclusive) throw new IllegalArgumentException("Wrong value");
return sums[toExclusive] - sums[fromInclusive];
}
public static PrefixSums of(int[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
public static PrefixSums of(long[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
}
static class ADUtils {
static void sort(int[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
static void reverse(int[] arr) {
int last = arr.length / 2;
for (int i = 0; i < last; i++) {
int tmp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = tmp;
}
}
static void sort(long[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
long a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
}
static class MathUtils {
static long[] FIRST_PRIMES = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013,
1019, 1021, 1031, 1033, 1039, 1049, 1051};
static long[] primes(int to) {
long[] all = new long[to + 1];
long[] primes = new long[to + 1];
all[1] = 1;
int primesLength = 0;
for (int i = 2; i <= to; i++) {
if (all[i] == 0) {
primes[primesLength++] = i;
all[i] = i;
}
for (int j = 0; j < primesLength && i * primes[j] <= to && all[i] >= primes[j];
j++) {
all[(int) (i * primes[j])] = primes[j];
}
}
return Arrays.copyOf(primes, primesLength);
}
static long modpow(long b, long e, long m) {
long result = 1;
while (e > 0) {
if ((e & 1) == 1) {
/* multiply in this bit's contribution while using modulus to keep
* result small */
result = (result * b) % m;
}
b = (b * b) % m;
e >>= 1;
}
return result;
}
static long submod(long x, long y, long m) {
return (x - y + m) % m;
}
static long modInverse(long a, long m) {
long g = gcdF(a, m);
if (g != 1) {
throw new IllegalArgumentException("Inverse doesn't exist");
} else {
// If a and m are relatively prime, then modulo
// inverse is a^(m-2) mode m
return modpow(a, m - 2, m);
}
}
static public long gcdF(long a, long b) {
while (b != 0) {
long na = b;
long nb = a % b;
a = na;
b = nb;
}
return a;
}
}
}
/*
5
3 2 3 8 8
2 8 5 10 1
*/ | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | b8c54414bcb36368a4588dc3934ff086 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.io.*;
public class Equal {
static class pair implements Comparable<pair> {
long level;
long count;
pair(long u, long v) {
this.level = u;
this.count = v;
}
@Override
public int compareTo(pair o) {
if(level>o.level){
return -1;
}
if(level==o.level)
return 0;
return 1;
}
}
public static void main(String[] args) throws IOException {
//BufferedReader br = new BufferedReader(new FileReader("name.in"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
PrintWriter out = new PrintWriter(System.out);
int n=Integer.parseInt(br.readLine());
long[]brackets=new long[n];
long[]presum=new long[n];
st=new StringTokenizer(br.readLine());
long seq=0;
PriorityQueue<pair>pq=new PriorityQueue<>();
for (int i = 0; i <n ; i++) {
brackets[i]=Long.parseLong(st.nextToken());
if(i==0){
presum[i]=brackets[i];
}
else {
if(i%2==0){
presum[i]=presum[i-1]+brackets[i];
}
else {
if(brackets[i]<=presum[i-1]){
presum[i]=presum[i-1]-brackets[i];
while(!pq.isEmpty()&&pq.peek().level>presum[i]){
pair x=pq.poll();
seq+=((x.count)*(x.count+1))/2;
}
if(pq.isEmpty()||pq.peek().level<presum[i]){
pq.add(new pair(presum[i],1));
}
else{
pq.add(new pair(presum[i],pq.poll().count+1));
}
seq+=brackets[i];
}
else {
while(!pq.isEmpty()){
pair x=pq.poll();
seq+=((x.count)*(x.count+1))/2;
}
seq+=presum[i-1];
}
}
}
}
while(!pq.isEmpty()){
pair x=pq.poll();
seq+=((x.count)*(x.count-1))/2;
}
out.println(seq);
out.flush();
out.close();
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 72b97049020f0e181fc8939012f3d618 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes |
import java.util.Scanner;
public class Submit {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
long[] a = new long[n+1];
for(int i=1;i<=n;i++){
a[i] = scanner.nextLong();
}
long ans = 0;
for(int i=1;i<=n;i+=2){
long init = a[i];
long other = 0;
for(int j=i+1;j<=n;j+=2){
if(j!=i+1){
other += a[j-1];
}
long k = Math.min(other,a[j]);
other -= k;
long t = a[j] - k;
if(j!=i+1 && other==0) ans++;
if(t>init){
ans += init;
break;
}else {
ans += t;
init -= t;
}
}
}
System.out.println(ans);
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | ad3c75783beba472076873920412adcf | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static long startTime = System.currentTimeMillis();
// for global initializations and methods starts here
// global initialisations and methods end here
static void run() {
boolean tc = false;
AdityaFastIO r = new AdityaFastIO();
//FastReader r = new FastReader();
try (OutputStream out = new BufferedOutputStream(System.out)) {
//long startTime = System.currentTimeMillis();
int testcases = tc ? r.ni() : 1;
int tcCounter = 1;
// Hold Here Sparky------------------->>>
// Solution Starts Here
start:
while (testcases-- > 0) {
int n = r.ni();
long[] arr = new long[n];
for (int i = 0; i < n; i++) arr[i] = r.nl();
long[] dp = new long[n + 1];
for (int i = 0; i < n; i++) {
dp[i + 1] = dp[i] + ((i & 1) == 0 ? arr[i] : -arr[i]);
}
long count = 0;
for (int i = 0; i < n; i++) {
if ((i & 1) == 0) {
long left = dp[i];
long right = dp[i + 1] - 1;
long cur = dp[i + 1];
for (int j = i + 1; j < n; j++) {
if ((j & 1) == 1) {
long min = Math.min(cur - 1, right);
long max = Math.max(left, cur - arr[j]);
if (cur - arr[j] <= right) count += min - max + 1;
right = Math.min(cur - arr[j], right);
cur -= arr[j];
if (right < left) break;
} else cur += arr[j];
}
}
}
out.write((count + " ").getBytes());
}
// Solution Ends Here
} catch (IOException e) {
e.printStackTrace();
}
}
static class AdityaFastIO {
final private int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public BufferedReader br;
public StringTokenizer st;
public AdityaFastIO() {
br = new BufferedReader(new InputStreamReader(System.in));
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public AdityaFastIO(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String word() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public String line() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public String readLine() throws IOException {
byte[] buf = new byte[100000001]; // 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 ni() 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 nl() 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 nd() 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();
}
}
public static void main(String[] args) throws Exception {
run();
}
static long mod = 998244353;
static long modInv(long base, long e) {
long result = 1;
base %= mod;
while (e > 0) {
if ((e & 1) > 0) result = result * base % mod;
base = base * base % mod;
e >>= 1;
}
return result;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String word() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String line() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int ni() {
return Integer.parseInt(word());
}
long nl() {
return Long.parseLong(word());
}
double nd() {
return Double.parseDouble(word());
}
}
static int MOD = (int) (1e9 + 7);
static long powerLL(long x, long n) {
long result = 1;
while (n > 0) {
if (n % 2 == 1) result = result * x % MOD;
n = n / 2;
x = x * x % MOD;
}
return result;
}
static long powerStrings(int i1, int i2) {
String sa = String.valueOf(i1);
String sb = String.valueOf(i2);
long a = 0, b = 0;
for (int i = 0; i < sa.length(); i++) a = (a * 10 + (sa.charAt(i) - '0')) % MOD;
for (int i = 0; i < sb.length(); i++) b = (b * 10 + (sb.charAt(i) - '0')) % (MOD - 1);
return powerLL(a, b);
}
static long gcd(long a, long b) {
if (a == 0) return b;
else return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
static long lower_bound(List<Long> list, long k) {
int s = 0;
int e = list.size();
while (s != e) {
int mid = (s + e) >> 1;
if (list.get(mid) < k) s = mid + 1;
else e = mid;
}
if (s == list.size()) return -1;
return s;
}
static int upper_bound(List<Long> list, long k) {
int s = 0;
int e = list.size();
while (s != e) {
int mid = (s + e) >> 1;
if (list.get(mid) <= k) s = mid + 1;
else e = mid;
}
if (s == list.size()) return -1;
return s;
}
static void addEdge(ArrayList<ArrayList<Integer>> graph, int edge1, int edge2) {
graph.get(edge1).add(edge2);
graph.get(edge2).add(edge1);
}
public static class Pair implements Comparable<Pair> {
int first;
int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public String toString() {
return "(" + first + "," + second + ")";
}
public int compareTo(Pair o) {
// TODO Auto-generated method stub
if (this.first != o.first)
return (int) (this.first - o.first);
else return (int) (this.second - o.second);
}
}
public static class PairC<X, Y> implements Comparable<PairC> {
X first;
Y second;
public PairC(X first, Y second) {
this.first = first;
this.second = second;
}
public String toString() {
return "(" + first + "," + second + ")";
}
public int compareTo(PairC o) {
// TODO Auto-generated method stub
return o.compareTo((PairC) first);
}
}
static boolean isCollectionsSorted(List<Long> list) {
if (list.size() == 0 || list.size() == 1) return true;
for (int i = 1; i < list.size(); i++) if (list.get(i) <= list.get(i - 1)) return false;
return true;
}
static boolean isCollectionsSortedReverseOrder(List<Long> list) {
if (list.size() == 0 || list.size() == 1) return true;
for (int i = 1; i < list.size(); i++) if (list.get(i) >= list.get(i - 1)) return false;
return true;
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 187bf9c5a7271da3c84a090757474bd6 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.*;
public class C {
public static class F {
static StringBuilder sb;
static long fact[];
static int mod = (int) (1e9 + 7);
static int[] arr = { 0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111, 1111111111 };
static void solve() {
long l = l();
long r = l();
long count = 0;
if (l == r) {
sb.append("0\n");
return;
}
String st = r + "";
int len = st.length();
for (int i = 0; i < st.length(); i++) {
count = count + ((st.charAt(i) - '0') * arr[len]);
len--;
}
String st1 = l + "";
int len1 = st1.length();
for (int i = 0; i < st1.length(); i++) {
count = count - ((st1.charAt(i) - '0') * arr[len1]);
len1--;
}
sb.append(count + "\n");
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
while (test-- > 0) {
solve();
}
System.out.println(sb);
}
/*
* fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++)
* { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; }
*/
// **************NCR%P******************
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % mod;
y--;
}
x = (x * x) % mod;
y = y / 2;
}
return res;
}
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long res = fact[n] % mod;
// System.out.println(res);
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
// System.out.println(res);
return res;
}
// **************END******************
// *************Disjoint set
// union*********//
// ***************PRIME FACTORIZE
// ***********************************//
static TreeMap<Integer, Integer> prime(long n) {
TreeMap<Integer, Integer> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
int nt = 0;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i, nt);
}
}
if (n != 1)
h.put((int) n, 1);
return h;
}
// *****CLASS PAIR
// *************************************************
static class Pair implements Comparable<Pair> {
int x;
long y;
Pair(int x, long y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return (int) (this.y - o.y);
}
}
// *****CLASS PAIR
// ***************************************************
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static 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 print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static long[] sortlong(long[] a2) {
int n = a2.length;
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static int[] sortint(int[] a2) {
int n = a2.length;
ArrayList<Integer> l = new ArrayList<>();
for (int i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static long pow(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 != 0) {
res = (res * x);// % modulus;
y--;
}
x = (x * x);// % modulus;
y = y / 2;
}
return res;
}
// GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
// ******LOWEST COMMON MULTIPLE
// *********************************************
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
// INPUT
// PATTERN********************************************************
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
public static int[] readArray(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
return A;
}
}
public static class Pair {
int idx;
long cnt;
Pair(int _idx, long _cnt) {
idx = _idx;
cnt = _cnt;
}
@Override
public String toString() {
return "( " + idx + " " + cnt + " )";
}
}
public static class BPair {
int oc, cc;
BPair(int _oc, int _cc) {
oc = _oc;
cc = _cc;
}
}
public static void main(String[] args) {
int n = F.i();
if (n % 2 != 0)
n--;
int[] arr = F.readArray(n);
BPair[] bp = new BPair[n / 2];
for (int i = 0; i < n; i += 2) {
bp[i / 2] = new BPair(arr[i], arr[i + 1]);
}
long[] opc = new long[n / 2];
PriorityQueue<Pair> pq = new PriorityQueue<>((a, b) -> {
return b.idx - a.idx;
});
long cnt = 0;
for (int i = 0; i < bp.length; i++) {
// System.out.println("start==== " + cnt + " " + pq);
BPair cp = bp[i];
if (i == 0) {
if (cp.oc >= cp.cc) {
cnt += cp.cc;
if (cp.oc != cp.cc)
pq.add(new Pair(0, cp.oc - cp.cc));
opc[0] = 1;
} else {
cnt += cp.oc;
}
continue;
}
if (cp.oc > cp.cc) {
cnt += cp.cc;
if (cp.oc != cp.cc)
pq.add(new Pair(i, cp.oc - cp.cc));
opc[i] = 1;
} else if (cp.oc < cp.cc) {
cnt += cp.oc;
cnt += opc[i - 1];
int excessCnt = cp.cc - cp.oc;
while (pq.size() != 0 && excessCnt > 0) {
Pair rp = pq.remove();
if (rp.cnt < excessCnt) {
cnt += rp.cnt + (rp.idx > 0 ? opc[rp.idx - 1] : 0);
opc[i] = 0;
} else if (rp.cnt > excessCnt) {
cnt += excessCnt;
opc[i] = 1;
pq.add(new Pair(rp.idx, rp.cnt - excessCnt));
} else {
cnt += rp.cnt + (rp.idx > 0 ? opc[rp.idx - 1] : 0);
opc[i] = 1 + (rp.idx > 0 ? opc[rp.idx - 1] : 0);
}
excessCnt -= rp.cnt;
}
} else {
cnt += cp.cc + opc[i - 1];
opc[i] = opc[i - 1] + 1;
}
// System.out.println("end==== " + cnt + " " + pq);
}
// System.out.println(Arrays.toString(opc));
System.out.println(cnt);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 594e2ca329d37472baa2fa075d75836d | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class c731 {
public static void main(String[] args) throws IOException {
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
PrintWriter pt = new PrintWriter(System.out);
FastReader sc = new FastReader();
// int t = sc.nextInt();
//for(int o = 0; o<t;o++){
//}
int n = sc.nextInt();
long[] arr = new long[n+1];
for(int i = 0 ; i<n;i++) {
arr[i] = sc.nextLong();
}
long ans = 0;
ArrayList<Integer> al1 = new ArrayList<Integer>();
ArrayList<Integer> al2 = new ArrayList<Integer>();
for(int i = 0 ; i<n;i+=2) {
long sum = 0;
long a = arr[i];
for(int j = i+1;j<n;j++) {
if(j%2==0) {
sum += arr[j];
}else {
if(arr[j]<sum) {
sum-=arr[j];
}else {
if(sum!=0) {
ans++;
}
ans += Math.min(a, arr[j]-sum);
a -= arr[j]- sum;
sum = 0;
if(a<0) {
break;
}
}
}
}
}
System.out.println(ans);
}
//------------------------------------------------------------------------------------------------------------------------------------------------
public static int cnt_set(long x) {
long v = 1l;
int c =0;
int f = 0;
while(v<=x) {
if((v&x)!=0) {
c++;
}
v = v<<1;
}
return c;
}
public static void dfs(int s , int[] arr , boolean[] vis,int[] brr) {
}
public static int lis(int[] arr,int[] dp) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
dp[0]= 1;
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>x) {
al.add(arr[i]);
}else {
int v = lower_bound(al, 0, al.size(), arr[i]);
// System.out.println(v);
al.set(v, arr[i]);
}
dp[i] = al.size();
}
//return al.size();
return al.size();
}
public static int lis2(int[] arr,int[] dp) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(-arr[n-1]);
dp[n-1] = 1;
// System.out.println(al);
for(int i = n-2 ; i>=0;i--) {
int x = al.get(al.size()-1);
// System.out.println(-arr[i] + " " + i + " " + x);
if((-arr[i])>x) {
al.add(-arr[i]);
}else {
int v = lower_bound(al, 0, al.size(), -arr[i]);
// System.out.println(v);
al.set(v, -arr[i]);
}
dp[i] = al.size();
}
//return al.size();
return al.size();
}
static int cntDivisors(int n){
int cnt = 0;
for (int i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
if (n/i == i)
cnt++;
else
cnt+=2;
}
}
return cnt;
}
public static long power(long x, long y, long p){
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0){
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static long ncr(long[] fac, int n , int r , long m) {
if(r>n) {
return 0;
}
return fac[n]*(modInverse(fac[r], m))%m *(modInverse(fac[n-r], m))%m;
}
public static int lower_bound(ArrayList<Integer> arr,int lo , int hi, int k)
{
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (arr.get(mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==arr.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> arr,int lo , int hi, int k)
{
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (arr.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==arr.size())
{
return -1;
}
return s;
}
// -----------------------------------------------------------------------------------------------------------------------------------------------
public static int gcd(int a, int b){
if (a == 0)
return b;
return gcd(b % a, a);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
public static long modInverse(long a, long m){
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
long q = a / m;
long t = m;
// m is remainder now, process
// same as Euclid's algo
m = a % m;
a = t;
t = y;
// Update x and y
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
//_________________________________________________________________________________________________________________________________________________________________
// private static int[] parent;
// private static int[] size;
public static int find(int[] parent, int u) {
while(u != parent[u]) {
parent[u] = parent[parent[u]];
u = parent[u];
}
return u;
}
private static void union(int[] parent,int[] size,int u, int v) {
int rootU = find(parent,u);
int rootV = find(parent,v);
if(rootU == rootV) {
return;
}
if(size[rootU] < size[rootV]) {
parent[rootU] = rootV;
size[rootV] += size[rootU];
} else {
parent[rootV] = rootU;
size[rootU] += size[rootV];
}
}
//-----------------------------------------------------------------------------------------------------------------------------------
//segment tree
//for finding minimum in range
public static void build(boolean [] seg,boolean []arr,int idx, int lo , int hi) {
if(lo == hi) {
seg[idx] = arr[lo];
return;
}
int mid = (lo + hi)/2;
build(seg,arr,2*idx+1, lo, mid);
build(seg,arr,idx*2+2, mid +1, hi);
// seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]);
// seg[idx] = seg[idx*2+1]+ seg[idx*2+2];
// seg[idx] = Math.min(seg[idx*2+1], seg[idx*2+2]);
seg[idx] = seg[idx*2+1] && seg[idx*2+2];
}
//for finding minimum in range
public static boolean query(boolean[]seg,int idx , int lo , int hi , int l , int r) {
if(lo>=l && hi<=r) {
return seg[idx];
}
if(hi<l || lo>r) {
return true;
}
int mid = (lo + hi)/2;
boolean left = query(seg,idx*2 +1, lo, mid, l, r);
boolean right = query(seg,idx*2 + 2, mid + 1, hi, l, r);
// return Math.min(left, right);
//return gcd(left, right);
// return Math.min(left, right);
return left && right;
}
// // for sum
//
//public static void update(int[]seg,int idx, int lo , int hi , int node , int val) {
// if(lo == hi) {
// seg[idx] += val;
// }else {
//int mid = (lo + hi )/2;
//if(node<=mid && node>=lo) {
// update(seg, idx * 2 +1, lo, mid, node, val);
//}else {
// update(seg, idx*2 + 2, mid + 1, hi, node, val);
//}
//seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2];
//
//}
//}
//---------------------------------------------------------------------------------------------------------------------------------------
//
//static void shuffleArray(int[] ar)
//{
// // If running on Java 6 or older, use `new Random()` on RHS here
// Random rnd = ThreadLocalRandom.current();
// for (int i = ar.length - 1; i > 0; i--)
// {
// int index = rnd.nextInt(i + 1);
// // Simple swap
// int a = ar[index];
// ar[index] = ar[i];
// ar[i] = a;
// }
//}
// static void shuffleArray(coup[] ar)
// {
// // If running on Java 6 or older, use `new Random()` on RHS here
// Random rnd = ThreadLocalRandom.current();
// for (int i = ar.length - 1; i > 0; i--)
// {
// int index = rnd.nextInt(i + 1);
// // Simple swap
// coup a = ar[index];
// ar[index] = ar[i];
// ar[i] = a;
// }
// }
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
class coup{
int a;
int b;
public coup(int a , int b) {
this.a = a;
this.b = b;
}
}
class trip{
int a , b, c;
public trip(int a , int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 97232e89f600ab95a3f1003f6d59aeb2 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
//--------------------------INPUT READER---------------------------------//
static class fs {
public BufferedReader br;
StringTokenizer st = new StringTokenizer("");
public fs() { this(System.in); }
public fs(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
String next() {
while (!st.hasMoreTokens()) {
try { st = new StringTokenizer(br.readLine()); }
catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String ns() { return next(); }
int[] na(long nn) {
int n = (int) nn;
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
long[] nal(long nn) {
int n = (int) nn;
long[] l = new long[n];
for(int i = 0; i < n; i++) l[i] = nl();
return l;
}
}
//-----------------------------------------------------------------------//
//---------------------------PRINTER-------------------------------------//
static class Printer {
static PrintWriter w;
public Printer() {this(System.out);}
public Printer(OutputStream os) {
w = new PrintWriter(os);
}
public void p(int i) {w.println(i);}
public void p(long l) {w.println(l);}
public void p(double d) {w.println(d);}
public void p(String s) { w.println(s);}
public void pr(int i) {w.print(i);}
public void pr(long l) {w.print(l);}
public void pr(double d) {w.print(d);}
public void pr(String s) { w.print(s);}
public void pl() {w.println();}
public void close() {w.close();}
}
//-----------------------------------------------------------------------//
//--------------------------VARIABLES------------------------------------//
static fs sc = new fs();
static OutputStream outputStream = System.out;
static Printer w = new Printer(outputStream);
static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE;
static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE;
static long mod = 1000000007;
//-----------------------------------------------------------------------//
//--------------------------ADMIN_MODE-----------------------------------//
private static void ADMIN_MODE() throws IOException {
if (System.getProperty("ONLINE_JUDGE") == null) {
w = new Printer(new FileOutputStream("output.txt"));
sc = new fs(new FileInputStream("input.txt"));
}
}
//-----------------------------------------------------------------------//
//----------------------------START--------------------------------------//
public static void main(String[] args)
throws IOException {
ADMIN_MODE();
//int t = sc.ni();while(t-->0)
solve();
w.close();
}
static void solve() throws IOException {
int n = sc.ni();
long[] arr = sc.nal(n);
long ans = 0;
ArrayDeque<Long> open = new ArrayDeque<>(), cont = new ArrayDeque<>();
for(int i = 0; i < n; i++) {
if(i%2==0) open.push(arr[i]);
else {
long curr = arr[i];
while(curr != 0) {
if(!open.isEmpty() && curr < open.peek()) {
long popped = open.pop();
open.push(popped - curr);
ans += curr;
cont.push(1L);
curr = 0;
} else if(!open.isEmpty() && curr == open.peek()) {
long popped = open.pop();
ans += curr;
curr = 0;
if(!cont.isEmpty()) cont.push(cont.pop()+1);
else cont.push(1L);
} else {
if(open.isEmpty()) {
while(!cont.isEmpty()) {
ans += calc(cont.pop());
}
curr = 0;
} else {
long popped = open.pop();
ans += popped;
if(!cont.isEmpty()) cont.push(cont.pop()+1);
if(!cont.isEmpty()) ans += calc(cont.pop());
curr -= popped;
}
}
}
}
}
while(!cont.isEmpty()) {
ans += calc(cont.pop());
}
w.p(ans);
}
static long calc(long n) {
return (n*(n+1))/2-n;
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 1b54cb6c2d5c73f4844822df1b838e93 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[])
{
FastReader input=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int T=1;
while(T-->0)
{
int n=input.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=input.nextInt();
}
Stack<Integer> st=new Stack<>();
long sum=0;
for(int i=0;i<n;i++)
{
if(i%2==0)
{
st.push(a[i]);
}
else
{
int v=a[i];
int f=0;
while(!st.isEmpty())
{
int t=st.pop();
if(t>0)
{
if(t>v)
{
f=1;
sum+=v;
t-=v;
st.push(t);
st.push(0);
v=0;
break;
}
else
{
int m=Math.min(v,t);
sum+=m;
v-=m;
break;
}
}
}
if(f==0)
{
if(v==0)
{
sum+=fun(st,v);
st.push(0);
}
else
{
f=0;
while(!st.isEmpty())
{
int t=st.pop();
if(t==0)
{
sum++;
}
else
{
if(t>v)
{
f=1;
sum+=v;
t-=v;
st.push(t);
st.push(0);
v=0;
break;
}
else if(t==v)
{
int m=Math.min(v,t);
sum+=m;
v-=m;
break;
}
else
{
int m=Math.min(v,t);
sum+=m;
v-=m;
}
}
}
if(f==0)
{
if(v==0)
{
sum+=fun(st,v);
st.push(0);
}
}
}
}
}
}
out.println(sum);
}
out.close();
}
public static int fun(Stack<Integer> st,int v)
{
int sum=0;
Stack<Integer> temp=new Stack<>();
while(!st.isEmpty())
{
int t=st.peek();
if(t==0)
{
sum++;
st.pop();
}
else
{
break;
}
temp.push(t);
}
while(!temp.isEmpty())
{
int t=temp.pop();
st.push(t);
}
return sum;
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | c197ad465ee57aba4476f58d0495597b | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.Writer;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Nipuna Samarasekara
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, FastScanner in, FastPrinter out) {
int n = in.nextInt();
int[] a = in.readIntArray(n);
long[][] min = new long[n + 1][n + 1];
long[][] max = new long[n + 1][n + 1];
long[][] sum = new long[n + 1][n + 1];
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
sum[i][i + 1] = a[i];
max[i][i + 1] = a[i];
} else {
sum[i][i + 1] = -a[i];
min[i][i + 1] = -a[i];
}
}
for (int len = 2; len <= n; len++) {
for (int st = 0; st + len <= n; st++) {
int en = st + len;
sum[st][en] = sum[st][en - 1] + sum[en - 1][en];
min[st][en] = Math.min(min[st][en - 1], sum[st][en - 1] + min[en - 1][en]);
max[st][en] = Math.min(max[st + 1][en], sum[st + 1][en] + max[st][st + 1]);
}
}
long ans = 0;
for (int i = 0; i < n; i += 2) {
for (int j = i + 1; j < n; j += 2) {
long mn = min[i + 1][j];
long mx = max[i + 1][j];
long sm = sum[i + 1][j];
long x1 = a[i];
long x2 = a[j];
if (mn < 0) {
x1 += mn;
sm -= mn;
}
if (mx > 0) {
x2 -= mx;
sm -= mx;
}
if (sm > 0) {
x2 -= sm;
} else {
x1 += sm;
}
if (x1 >= 0 && x2 >= 0) {
ans += Math.min(x1, x2);
if (x1 < a[i] && x2 < a[j]) {
ans++;
}
}
}
}
out.println(ans);
}
}
static class FastPrinter extends PrintWriter {
public FastPrinter(OutputStream out) {
super(out);
}
public FastPrinter(Writer out) {
super(out);
}
}
static class FastScanner extends BufferedReader {
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}
public int read() {
try {
int ret = super.read();
// if (isEOF && ret < 0) {
// throw new InputMismatchException();
// }
// isEOF = ret == -1;
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}
static boolean isWhiteSpace(int c) {
return c >= 0 && c <= 32;
}
public int nextInt() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int ret = 0;
while (c >= 0 && !isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c + " found");
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
}
public String readLine() {
try {
return super.readLine();
} catch (IOException e) {
return null;
}
}
public int[] readIntArray(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = nextInt();
}
return ret;
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | c2d18bd145491ab46d2934063549472b | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Ishu
{
static Scanner scan = new Scanner(System.in);
static BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
static void tc() throws Exception
{
int n = scan.nextInt();
long[] c = new long[n];
int i, j;
for(i=0;i<n;++i)
c[i] = scan.nextLong();
long ans = 0l;
for(i=1;i<n;i+=2)
{
long cur = c[i];
ans += Math.min(c[i - 1], cur);
cur -= c[i - 1];
if(cur < 0)
continue;
long extra = 0;
for(j=i-2;j>=0;--j)
{
if(j % 2 == 0)
{
long cr = c[j] - extra;
extra -= Math.min(extra, c[j]);
if(extra == 0)
{
++ans;
if(cr > cur)
{
ans += cur;
break;
}
else
{
ans += cr;
cur -= cr;
}
}
}
else
extra += c[j];
}
}
output.write(ans + "\n");
output.flush();
}
public static void main(String[] args) throws Exception
{
int t = 1;
//t = scan.nextInt();
while(t-- > 0)
tc();
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | b6fd44b8d7111b7991b46fdd15d4a2d4 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C {
static class RealScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] args) {
RealScanner sc = new RealScanner();
long n;
n = sc.nextLong();
long[] arr = new long[(int) n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
long res = 0;
for (int i = 0; i < n; i += 2) {
long min = arr[i];
long cursor = arr[i];
for (int j = i + 1; j < n; j++) {
if (j % 2 == 0) {
cursor += arr[j];
} else {
long val1 = Math.max(0, cursor - arr[j]);
long val2 = Math.min(min, Math.min(cursor - 1, arr[i] - 1));
if (val1 <= val2) {
res += val2 - val1 + 1;
}
cursor -= arr[j];
min = Math.min(cursor, min);
}
}
}
System.out.println(res);
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 20d697e93e62899c8f0c4b96127ef4ff | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class Practice {
public static long mod = (long) Math.pow(10, 9) + 7;
// public static long mod2 = 998244353;
// public static int tt = 1;
public static ArrayList<Long> one;
public static ArrayList<Long> two;
// public static long[] fac = new long[200005];
// public static long[] invfac = new long[200005];
public static void main(String[] args) throws Exception {
PrintWriter pw = new PrintWriter(System.out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// int t = Integer.parseInt(br.readLine());
// int p = 1;
// while (t-- > 0) {
String[] s2 = br.readLine().split(" ");
int n = Integer.valueOf(s2[0]);
long[] arr = new long[n];
String str = (br.readLine());
String[] s1 = str.split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(s1[i]);
}
long ans = 0;
for (int i = 0; i < n - 1; i += 2) {
long min = 0;
long o = 0;
long curr = 0;
for (int j = i + 1; j < n; j += 2) {
if (j == i + 1) {
long mm1 = 1;
long mm2 = Math.min(arr[i], arr[i + 1]);
ans += mm2 - mm1 + 1;
min += Math.max(0, arr[j]);
curr = curr - arr[j];
} else {
long mm1 = Math.max(1, min);
long cc = Math.min(arr[i], arr[j] - (curr + arr[j - 1]));
if (cc >= mm1) {
ans += cc - mm1 + 1;
}
min += Math.max(0, arr[j] - o - arr[j - 1]);
o = Math.max(0, o + arr[j - 1] - arr[j]);
curr = curr + arr[j - 1] - arr[j];
}
// System.out.println(ans + " " + min);
if (min > arr[i]) {
break;
}
}
}
pw.println(ans);
// }
pw.close();
}
//
// private static long power(long a, long p) {
// // TODO Auto-generated method stub
// long res = 1;
// while (p > 0) {
// if (p % 2 == 1) {
// res = (res * a) % mod;
// }
// p = p / 2;
// a = (a * a) % mod;
// }
// return res;
// }
// private static int kmp(String str) {
// // TODO Auto-generated method stub
// // System.out.println(str);
// int[] pi = new int[str.length()];
// pi[0] = 0;
// for (int i = 1; i < str.length(); i++) {
// int j = pi[i - 1];
// while (j > 0 && str.charAt(i) != str.charAt(j)) {
// j = pi[j - 1];
// }
// if (str.charAt(j) == str.charAt(i)) {
// j++;
// }
// pi[i] = j;
// System.out.print(pi[i]);
// }
// System.out.println();
// return pi[str.length() - 1];
// }
}
// private static void getFac(long n, PrintWriter pw) {
// // TODO Auto-generated method stub
// int a = 0;
// while (n % 2 == 0) {
// a++;
// n = n / 2;
// }
// if (n == 1) {
// a--;
// }
// for (int i = 3; i <= Math.sqrt(n); i += 2) {
// while (n % i == 0) {
// n = n / i;
// a++;
// }
// }
// if (n > 1) {
// a++;
// }
// if (a % 2 == 0) {
// pw.println("Bob");
// } else {
// pw.println("Alice");
// }
// //System.out.println(a);
// return;
// }
// private static long power(long a, long p) {
// // TODO Auto-generated method stub
// long res = 1;
// while (p > 0) {
// if (p % 2 == 1) {
// res = (res * a) % mod;
// }
// p = p / 2;
// a = (a * a) % mod;
// }
// return res;
// }
//
// private static void fac() {
// fac[0] = 1;
// // TODO Auto-generated method stub
// for (int i = 1; i < fac.length; i++) {
// if (i == 1) {
// fac[i] = 1;
// } else {
// fac[i] = i * fac[i - 1];
// }
// if (fac[i] > mod) {
// fac[i] = fac[i] % mod;
// }
// }
// }
//
// private static int getLower(Long long1, Long[] st) {
// // TODO Auto-generated method stub
// int left = 0, right = st.length - 1;
// int ans = -1;
// while (left <= right) {
// int mid = (left + right) / 2;
// if (st[mid] <= long1) {
// ans = mid;
// left = mid + 1;
// } else {
// right = mid - 1;
// }
// }
// return ans;
// }
//private static long getGCD(long l, long m) {
//
// long t1 = Math.min(l, m);
// long t2 = Math.max(l, m);
// if (t1 == 0) {
// return t2;
// }
// while (true) {
// long temp = t2 % t1;
// if (temp == 0) {
// return t1;
// }
// t2 = t1;
// t1 = temp;
// }
//} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | dbb40ce2828b34bc5f405ba974cf8a87 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | //package contest.DeltixRoundSummer2021;
import java.io.*;
import java.util.*;
/***********************
* @oj: codeforces
* @id: hitwanyang
* @email: 296866643@qq.com
* @date: 2021/8/30 11:34
* @url: https://codeforc.es/contest/1556/problem/C
***********************/
public class C1556 {
InputStream is;
FastWriter out;
String INPUT = "";
//提交时注意需要注释掉首行package
//基础类型数组例如long[]使用Arrays排序容易TLE,可以替换成Long[]
//int 最大值2**31-1,2147483647;
//尽量使用long类型,避免int计算的数据溢出
//String尽量不要用+号来,可能会出现TLE,推荐用StringBuffer
void solve() {
// int t=ni();
// for (; t > 0; t--)
go();
}
void go() {
int n = ni();
Integer[] a = na(n);
long ans = 0;
for (int i = 0; i < n; i += 2) {
long left = a[i];
long sum = 0;
for (int j = i + 1; j < n; j += 2) {
if (j == i + 1) {
if (a[j] > a[j - 1]) {
ans += a[j - 1];
break;
} else {
ans += a[j];
left = a[j - 1] - a[j];
}
} else {
if (a[j] >= a[j - 1]) {
if (sum <= a[j] - a[j - 1]) {
long tmp = a[j] - a[j - 1] - sum;
if (left >= tmp) {
ans += (tmp + 1);
left -= tmp;
} else {
ans += (left + 1);
break;
}
sum = 0;
} else {
sum -= (a[j] - a[j - 1]);
}
} else {
sum += (a[j - 1] - a[j]);
}
}
}
}
out.println(ans);
}
void run() throws Exception {
is = System.in;
out = new FastWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
//debug log
//tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new C1556().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private Integer[] na(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private Long[] nal(int n) {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private Integer[][] nmi(int n, int m) {
Integer[][] map = new Integer[n][];
for (int i = 0; i < n; i++)
map[i] = na(m);
return map;
}
private int ni() {
return (int) nl();
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map)
write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
public void trnz(int... o) {
for (int i = 0; i < o.length; i++)
if (o[i] != 0)
System.out.print(i + ":" + o[i] + " ");
System.out.println();
}
// print ids which are 1
public void trt(long... o) {
Queue<Integer> stands = new ArrayDeque<>();
for (int i = 0; i < o.length; i++) {
for (long x = o[i]; x != 0; x &= x - 1)
stands.add(i << 6 | Long.numberOfTrailingZeros(x));
}
System.out.println(stands);
}
public void tf(boolean... r) {
for (boolean x : r)
System.out.print(x ? '#' : '.');
System.out.println();
}
public void tf(boolean[]... b) {
for (boolean[] r : b) {
for (boolean x : r)
System.out.print(x ? '#' : '.');
System.out.println();
}
System.out.println();
}
public void tf(long[]... b) {
if (INPUT.length() != 0) {
for (long[] r : b) {
for (long x : r) {
for (int i = 0; i < 64; i++) {
System.out.print(x << ~i < 0 ? '#' : '.');
}
}
System.out.println();
}
System.out.println();
}
}
public void tf(long... b) {
if (INPUT.length() != 0) {
for (long x : b) {
for (int i = 0; i < 64; i++) {
System.out.print(x << ~i < 0 ? '#' : '.');
}
}
System.out.println();
}
}
private void tr(Object... o) {
System.out.println(Arrays.deepToString(o));
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 34c1d0fbeefc1eeb634b085770f536cd | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Sol {
final static int LINE_SIZE = 128;
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out), false);
int n;
long[] ar;
void solve() throws Exception {
n = in.nextInt();
ar = new long[n];
for (int i = 0; i < n; i++) ar[i] = in.nextLong();
long total = 0;
for (int i = 1; i < n; i += 2) {
long close = ar[i];
long par = 0;
boolean end = false;
for (int j = i - 1; j >= 0; j--) {
//out.println(j + " " + i + " " + close + " " + par + " " + total);
if (j % 2 == 0) par -= ar[j];
else par += ar[j];
if (par < 0) {
if (end) total++;
if (par + close < 0) {
total += close;
break;
} else {
total -= par;
close += par;
par = 0;
end = true;
}
} else if (par == 0) {
total++;
}
}
}
out.println(total);
}
void runCases() throws Exception {
int tt = 1;
//tt = in.nextInt();
while (tt-- > 0) {
solve();
}
in.close();
out.close();
}
public static void main(String[] args) throws Exception {
Sol solver = new Sol();
solver.runCases();
}
static class Pii implements Comparable<Pii> {
public int fi, se;
public Pii(int fi, int se) {
this.fi = fi;
this.se = se;
}
public int compareTo(Pii other) {
if (fi == other.fi) return se - other.se;
return fi - other.fi;
}
}
static class Pll implements Comparable<Pll> {
public long fi, se;
public Pll(long fi, long se) {
this.fi = fi;
this.se = se;
}
public int compareTo(Pll other) {
if (fi == other.fi) return se < other.se ? -1 : 1;
return fi < other.fi ? -1 : 1;
}
}
static class Pair<T extends Comparable<T>, U extends Comparable<U>> implements Comparable<Pair<T, U>> {
T fi;
U se;
public Pair(T fi, U se) {
this.fi = fi;
this.se = se;
}
public int compareTo(Pair<T, U> other) {
if (fi == other.fi) return se.compareTo(other.se);
return fi.compareTo(other.fi);
}
}
static class FastReader {
private final DataInputStream din = new DataInputStream(System.in);
private final byte[] buffer = new byte[65536];
private int bufferPointer = 0, bytesRead = 0;
public String readLine() throws Exception {
byte[] buf = new byte[LINE_SIZE];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) break;
else continue;
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws Exception {
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');
return neg ? -ret : ret;
}
public long nextLong() throws Exception {
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');
return neg ? -ret : ret;
}
public double nextDouble() throws Exception {
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);
return neg ? -ret : ret;
}
private void fillBuffer() throws Exception {
bytesRead = din.read(buffer, bufferPointer = 0, 65536);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws Exception {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws Exception {
din.close();
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 0d75041c02be0dc203e38dcc994d6302 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution {
static class SegmentTree { /* Author: Po-Chun Chiu */
int size;
private long[] max;
private long[] min;
public SegmentTree(long[] array){
size = array.length;
max = new long[array.length*4];
min = new long[array.length*4];
buildTree(array, 1, 0, size - 1);
}
private void buildTree(long[] array, int position, int front, int end){
//Basis case for returning the elements itself
if(front == end){
max[position] = array[front];
min[position] = array[front];
return;
}
//Building trees by dividing into two subtrees
buildTree(array, position * 2, front, (front+end)/2);
buildTree(array, position * 2 + 1, (front+end)/2 + 1, end);
recalc(position, front, end);
}
private void recalc(int position, int front, int end) {
max[position] = Math.max(getMax(position * 2), getMax(position * 2 + 1));
min[position] = Math.min(getMin(position * 2), getMin(position * 2 + 1));
}
private long getMax(int position){
return max[position] ;
}
private long getMin(int position){
return min[position] ;
}
public long queryMax(int queryFront, int queryEnd) {
return queryMax(1, 0, size - 1, queryFront, queryEnd);
}
private long queryMax(int position, int front, int end, int queryFront, int queryEnd){
//Case for entirely inclusive
if(front >= queryFront && queryEnd >= end){
return getMax(position);
}
//Case for entirely exclusive
if(end < queryFront || queryEnd < front){
return Long.MIN_VALUE;
}
long leftAns = queryMax(position*2, front,(front+end)/2,queryFront,queryEnd);
long rightAns = queryMax(position*2+1, (front+end)/2+1,end,queryFront,queryEnd);
return Math.max(leftAns, rightAns);
}
public long queryMin(int queryFront, int queryEnd) {
return queryMin(1,0,size-1,queryFront,queryEnd);
}
private long queryMin(int position,int front,int end, int queryFront, int queryEnd){
//Case for entirely inclusive
if(front>=queryFront&&queryEnd>=end){
return getMin(position);
}
//Case for entirely exclusive
if(end<queryFront||queryEnd<front){
return Long.MAX_VALUE;
}
long leftAns=queryMin(position*2,front,(front+end)/2,queryFront,queryEnd);
long rightAns=queryMin(position*2+1,(front+end)/2+1,end,queryFront,queryEnd);
return Math.min(leftAns, rightAns);
}
}
public static void main(String[] args) throws IOException
{
FastScanner f= new FastScanner();
int ttt=1;
// ttt=f.nextInt();
PrintWriter out=new PrintWriter(System.out);
outer: for(int tt=0;tt<ttt;tt++) {
int n=f.nextInt();
int[] l=f.readArray(n);
long ans=0;
long[] sum=new long[n+1];
for(int i=1;i<=n;i++) {
if(i%2==0) l[i-1]=-l[i-1];
sum[i]=sum[i-1]+l[i-1];
}
// System.out.println(Arrays.toString(sum));
SegmentTree tree=new SegmentTree(sum);
for(int i=1;i<n;i+=2) {
for(int j=i+1;j<n;j+=2) {
long sumf=sum[j+1]-sum[i];
long min=tree.queryMin(i+1, j+1)-sum[i];
// System.out.println(min);
if(sumf>=0) {
if(j+1<n && i-1>-1) {
long left=l[i-1];
long right=-l[j+1];
// System.out.println("left " +left+" right "+right);
right-=sumf;
if(right>=0) {
if(min<0) {
left+=min;
right+=min;
if(left>=0 && right>=0) {
ans+=Math.min(left, right)+1;
}
}
else {
ans+=Math.min(left, right)+1;
}
}
}
}
else {
if(j+1<n && i-1>-1) {
long left=l[i-1];
long right=-l[j+1];
// System.out.println("left " +left+" right "+right);
left+=sumf;
min-=sumf;
if(left>=0) {
if(min<0) {
left+=min;
right+=min;
if(left>=0 && right>=0) {
ans+=Math.min(left, right)+1;
}
}
else {
ans+=Math.min(left, right)+1;
}
}
}
}
// System.out.println("i "+i+" j "+j+" ans "+ans+" sum"+sumf+" min"+min);
}
}
for(int i=0;i<n;i+=2) {
if(i+1<n) ans+=Math.min(l[i], -l[i+1]);
}
System.out.println(ans);
}
out.close();
}
static void sort(int[] p) {
ArrayList<Integer> q = new ArrayList<>();
for (int i: p) q.add( i);
Collections.sort(q);
for (int i = 0; i < p.length; i++) p[i] = q.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long[] readLongArray(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | f60b05a05e35e463496f8f1be3dd44a1 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
public class Test
{
public static void main(String[] args)
{
int tt=1;
//cin >> tt;
while(tt>0)
{
long n;
Scanner sc=new Scanner(System.in);
n=sc.nextLong();
long[] a=new long[(int)n];
for(int i=0;i<n;i++)
a[i]=sc.nextLong();
long ans=0;
for(int i=0;i<n;i+=2)
{
long sum=0,openf=0,closl=0;
for(int j=i+1;j<n;j++)
{
if(j%2==0)
{
sum+=a[j];
}
else
{
if(openf<=a[i] && sum<=a[j])
{
long x=a[i],y=a[j];
x-=openf;y-=sum;
if(x!=a[i] || y!=a[j])
ans++;
ans+=Math.min(x,y);
}
sum-=a[j];
}
if(sum<0)
{openf+=Math.abs(sum);sum=0;}
}
}
System.out.println(ans);
tt--;
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 2b061b6735a41874574563a99030e4bd | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class new1
{
public static void main (String[] args)
{
Scanner s = new Scanner(System.in);
int t = 1;//s.nextInt();
for(int i = 0; i < t; i++) {
int n = s.nextInt();
int[] arr = new int[n + 1];
for(int j = 1; j <= n; j++) {
arr[j] = s.nextInt();
}
int[] count = new int[n + 1];
long total = 0;
int j = 1;
while(j <= n) {
if(j % 2 == 1) {
j++;
continue;
}
else {
int k = j - 1;
while(k >= 1 && arr[j] != 0) {
if(k % 2 == 1 && arr[k] > 0) {
if(arr[k] > arr[j]) {
total = total + arr[j];
arr[k] = arr[k] - arr[j];
arr[j] = 0;
count[j] = 1;
}
else if(arr[k] < arr[j]) {
total = total + arr[k] + count[k - 1];
arr[j] = arr[j] - arr[k];
arr[k] = 0;
}
else {
total = total + arr[j] + count[k - 1];
arr[k] = 0;
arr[j] = 0;
count[j] = count[k - 1] + 1;
}
}
// System.out.println(k + " " + j + " " + count[j] + " " + total);
k--;
}
}
j++;
}
System.out.println(total);
}
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 67cc89c613aeade7aff25613be2950b2 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
//BufferedReader f = new BufferedReader(new FileReader("uva.in"));
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = Integer.parseInt(f.readLine());
StringTokenizer st = new StringTokenizer(f.readLine());
int[] a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
long ans = 0;
for(int i = 0; i < n-1; i += 2) {
long cur = a[i]-a[i+1];
long ext = 0;
ans += Math.min(a[i], a[i+1]);
for(int j = i+3; j < n && cur >= 0; j += 2) {
long left = a[j]-a[j-1];
if(left < 0) {
ext += -left;
} else {
if(left < ext) {
ext -= left;
} else {
left -= ext;
ext = 0;
ans += Math.min(cur, left)+1;
cur -= left;
}
}
}
}
out.println(ans);
f.close();
out.close();
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | f45a8e9eae66e1aa8bf85dc4bec78210 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import javax.swing.plaf.synth.SynthSpinnerUI;
import java.io.*;
import java.net.StandardSocketOptions;
public class tr0 {
static PrintWriter out;
static StringBuilder sb;
static long mod = (long) 1e9 + 7;
static long inf = (long) 1e16;
static int n, k;
static ArrayList<Integer>[] ad;
static int[][] remove, add;
static int[][] memo;
static boolean vis[];
static long[] inv, ncr[];
static HashMap<Integer, Integer> hm;
static int[] pre, suf, Smax[], Smin[];
static int idmax, idmin;
static ArrayList<Integer> av;
static HashMap<Integer, Integer> mm;
static boolean[] msks;
static int[] lazy[], lazyCount;
static int[] dist;
static int[][] P;
static long N;
static ArrayList<Integer> gl;
static int[] a;
static String s;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int n = sc.nextInt();
int[] a = sc.nextArrInt(n);
long ans = 0;
long[][] maxneg = new long[n][n];
for (int i = 0; i < n; i++) {
if (i % 2 == 1)
continue;
long sum = 0;
long min = Long.MAX_VALUE;
for (int j = i + 1; j < n; j++) {
if (j % 2 == 0)
sum += a[j];
else
sum -= a[j];
min = Math.min(min, sum);
maxneg[i][j] = min;
}
}
for (int i = 0; i < n; i++) {
if (i % 2 == 1) {
ans += Math.min(a[i], a[i - 1]);
long sum = -a[i] + a[i - 1];
long min = 0;
for (int j = i - 3; j >= 0; j -= 2) {
if (sum > 0)
break;
sum += -a[j + 1];
min = maxneg[j][i - 1];
ans += Math.max(0, Math.min(a[j], -sum) + min + 1);
sum += a[j];
}
}
}
System.out.println(ans);
out.flush();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextArrInt(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextArrLong(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextArrIntSorted(int n) throws IOException {
int[] a = new int[n];
Integer[] a1 = new Integer[n];
for (int i = 0; i < n; i++)
a1[i] = nextInt();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[i].intValue();
return a;
}
public long[] nextArrLongSorted(int n) throws IOException {
long[] a = new long[n];
Long[] a1 = new Long[n];
for (int i = 0; i < n; i++)
a1[i] = nextLong();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[i].longValue();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | f2b59dc244c5ef038098f08d257ebf25 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
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);
PrintWriter out = new PrintWriter(outputStream);
CSzhatayaSkobochnayaPosledovatelnost solver = new CSzhatayaSkobochnayaPosledovatelnost();
solver.solve(1, in, out);
out.close();
}
static class CSzhatayaSkobochnayaPosledovatelnost {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
long ans = 0;
for (int st = 0; st < n; st += 2) {
long open1 = a[st];
long open2 = 0;
long add = 0;
for (int cur = st + 1; cur < n; cur++) {
if (cur % 2 == 0) {
open2 += a[cur];
} else {
long close = a[cur];
long d = Math.min(open2, close);
open2 -= d;
close -= d;
if (open2 == 0) {
add += Math.min(open1, close) + (add > 0 ? 1 : 0);
}
d = Math.min(open1, close);
open1 -= d;
close -= d;
if (close > 0) {
break;
}
}
}
ans += add;
}
out.println(ans);
}
}
static class InputReader {
private static final int BUFFER_LENGTH = 1 << 10;
private InputStream stream;
private byte[] buf = new byte[BUFFER_LENGTH];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int nextC() {
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 = skipWhileSpace();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextC();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = nextC();
} while (!isSpaceChar(c));
return res * sgn;
}
public int skipWhileSpace() {
int c = nextC();
while (isSpaceChar(c)) {
c = nextC();
}
return c;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 14b93375d96c573e678316543342519f | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes |
import java.io.*;
import java.util.*;
public class DeltixSU21C {
public static void solve(IO io) {
int k = io.getInt();
long [] arr = new long[k];
for (int i = 0; i < k; i++) {
arr[i] = io.getLong();
}
long total = 0;
for (int i = 0; i < k; i+= 2) {
long hl = 1;
long hh = arr[i];
for (int j = i + 1; j < k; j++) {
if (j % 2 == 0) {
hl += arr[j];
hh += arr[j];
} else {
hl -= arr[j];
hh -= arr[j];
if (hh < 0) {
total+= (hh - hl + 1);
break;
} else if (hl <= 0) {
total += 0 - hl + 1;
hl = 0;
}
}
}
}
io.println(total);
}
public static void main(String[] args) {
IO io = new IO();
solve(io);
io.close();
}
static class IO extends PrintWriter {
public IO() {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(System.in));
}
public IO(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public IO(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int getInt() {
return Integer.parseInt(nextToken());
}
public double getDouble() {
return Double.parseDouble(nextToken());
}
public long getLong() {
return Long.parseLong(nextToken());
}
public String getWord() {
return nextToken();
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null)
return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) {
}
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | ec16df0ffd35f1f0d5d2fa0862a9ba45 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
try (Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))))
{
// int T = in.nextInt();
//
// for (int t = 1; t <= T; t++)
// {
// System.out.println();
// }
int n = in.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++)
{
nums[i] = in.nextInt();
}
System.out.println(getCount(nums));
}
}
private static long getCount(int[] nums)
{
long count = 0L;
for (int i = 0; i < nums.length; i = i + 2)
{
long min = 0L;
long max = 0L;
for (int j = i; j < nums.length; j++)
{
int curr = nums[j];
if (j % 2 == 0)
{
if (j == i)
{
max += curr;
}
else
{
min += curr;
max += curr;
}
}
else
{
if (j == i + 1)
{
if (curr > nums[i])
{
count += nums[i];
break;
}
else
{
count += curr;
max -= curr;
}
}
else
{
if (curr < min)
{
min -= curr;
max -= curr;
}
else if (curr >= min && curr <= max)
{
count += curr - min + 1;
min = 0;
max -= curr;
}
else
{
count += max - min + 1;
break;
}
}
}
}
}
return count;
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 50cac10e157772ac32e887e9741cd69d | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes |
import java.io.*;
import java.lang.*;
import java.util.*;
public class C1556 {
public static void main(String[] args) throws IOException{
StringTokenizer st;
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
st = new StringTokenizer(f.readLine());
Stack<point> stack = new Stack<>();
long ans = 0;
int[] dp = new int[n];
for(int i = 0; i < n; i++){
if(i % 2 == 0) stack.add(new point(Integer.parseInt(st.nextToken()), i));
else{
int r = Integer.parseInt(st.nextToken());
while (!stack.isEmpty()){
point ft = stack.pop();
int l = ft.x;
if(l > r){
l-=r;
stack.add(new point(l, ft.y));
dp[i]++;
ans+=r;
break;
}else if(r == l){
dp[i] = 1;
ans+=r;
if(ft.y != 0){
dp[i]+=dp[ft.y-1];
ans+=dp[ft.y-1];
}
break;
}else{
r-=l;
ans+=l;
if(ft.y != 0) ans+=dp[ft.y-1];
}
}
//System.out.println(r + " " + ans);
}
}
System.out.println(ans);
}
public static class point{
int x;
int y;
public point(int x,int y){
this.x = x;
this.y = y;
}
public String toString(){
return(x + " " + y);
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 8fcf704288b5c310d1000e9ae34887ff | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Brackets {
static BufferedReader bf;
static PrintWriter out;
public static void main (String[] args)throws IOException {
bf = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
// int t = Integer.parseInt(bf.readLine());
// while(t-->0){
solve();
// }
}
public static void solve()throws IOException{
int n = nextInt();
int[]arr = nextIntArray(n);
Stack<long[]>st = new Stack<>();
Map<Long,Long>map = new HashMap<>();
long count = 0;
long add = 0;
for(int i =0;i<n;i++){
if(i%2 == 0){
add+=arr[i];
st.push(new long[]{add,arr[i]});
}
else{
long curValue = arr[i];
long localAdd = add;
while(!st.isEmpty() && curValue !=0 ){
long[]cur = st.pop();
if(curValue > cur[1]){
count += cur[1]-1;
curValue -= cur[1];
cur[0] -= cur[1];
localAdd += cur[1];
map.put(localAdd,map.getOrDefault(cur[0],(long)0)+1);
map.put(cur[0],(long)0);
}
else{
long diff = cur[1] - (cur[1] - curValue);
count += diff-1;
cur[1] -= curValue ;
cur[0] -= curValue;
localAdd += diff;
curValue = 0;
map.put(localAdd,map.getOrDefault(cur[0], (long)0)+1);
map.put(cur[0],(long)0);
if(cur[1] != 0){
st.push(cur);
}
}
}
add += arr[i];
}
}
for(long num : map.values()){
count += (num*(num+1))/2;
}
println(count);
}
public static int check(long mid,int []arr,long count){
for(int i = 0;i<arr.length;i++){
if(arr[i] != 0){
count = count - (mid - arr[i]);
}
}
if(count == 0)return 0;
if(count < 0)return -1;
return 1;
}
// code for input
public static void print(String s ){
System.out.print(s);
}
public static void print(int num ){
System.out.print(num);
}
public static void print(long num ){
System.out.print(num);
}
public static void println(String s){
System.out.println(s);
}
public static void println(int num){
System.out.println(num);
}
public static void println(long num){
System.out.println(num);
}
public static void println(){
System.out.println();
}
public static int toInt(String s){
return Integer.parseInt(s);
}
public static long toLong(String s){
return Long.parseLong(s);
}
public static String[] nextStringArray()throws IOException{
return bf.readLine().split(" ");
}
public static int nextInt()throws IOException{
return Integer.parseInt(bf.readLine());
}
public static long nextLong()throws IOException{
return Long.parseLong(bf.readLine());
}
public static String nextString()throws IOException{
return bf.readLine();
}
public static int[] nextIntArray(int n)throws IOException{
String[]str = bf.readLine().split(" ");
int[]arr = new int[n];
for(int i =0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
}
return arr;
}
public static long[] nextLongArray(int n)throws IOException{
String[]str = bf.readLine().split(" ");
long[]arr = new long[n];
for(int i =0;i<n;i++){
arr[i] = Long.parseLong(str[i]);
}
return arr;
}
public static int[][] newIntMatrix(int r,int c)throws IOException{
int[][]arr = new int[r][c];
for(int i =0;i<r;i++){
String[]str = bf.readLine().split(" ");
for(int j =0;j<c;j++){
arr[i][j] = Integer.parseInt(str[j]);
}
}
return arr;
}
public static long[][] newLongMatrix(int r,int c)throws IOException{
long[][]arr = new long[r][c];
for(int i =0;i<r;i++){
String[]str = bf.readLine().split(" ");
for(int j =0;j<c;j++){
arr[i][j] = Long.parseLong(str[j]);
}
}
return arr;
}
static class pair{
int one;
int two;
pair(int one,int two){
this.one = one ;
this.two =two;
}
}
public static long gcd(long a,long b){
if(b == 0)return a;
return gcd(b,a%b);
}
public static long lcm(long a,long b){
return (a*b)/(gcd(a,b));
}
public static boolean isPalindrome(String s){
int i = 0;
int j = s.length()-1;
while(i<=j){
if(s.charAt(i) != s.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
}
// use some math tricks it might help
// sometimes just try to think in straightforward plan in A and B problems don't always complecate the questions with thinking too much differently
// always use long number to do 10^9+7 modulo
// if a problem is related to binary string it could also be related to parenthesis
// *****try to use binary search(it is a very beautiful thing it can work in some of the very unexpected problems ) in the question it might work******
// try sorting
// try to think in opposite direction of question it might work in your way
// if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+d or a,b,c,d in general
// if the question is to much related to left and/or right side of any element in an array then try monotonic stack it could work.
// in range query sums try to do binary search it could work
// analyse the time complexity of program thoroughly
// anylyse the test cases properly
// if we divide any number by 2 till it gets 1 then there will be (number - 1) operation required
// try to do the opposite operation of what is given in the problem
//think about the base cases properly
//If a question is related to numbers try prime factorisation or something related to number theory
// keep in mind unique strings | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 41a577ed326ce93217ff205a993b9c6a | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
import java.util.Map.Entry;
import java.math.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
InputReader in = new InputReader(System.in);
// Scanner in = new Scanner(System.in);
// Scanner in = new Scanner(new BufferedReader(new
// InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(System.out);
// InputReader in = new InputReader(new
// File("ethan_traverses_a_tree.txt"));
// PrintWriter out = new PrintWriter(new
// File("ethan_traverses_a_tree-output.txt"));
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
long ans = 0;
for (int i = 0; i < n; i = i + 2) {
long sum = 0;
long min = 0;
for (int j = i + 1; j < n; j++) {
if (j == i + 1) {
ans = ans + Math.min(a[i], a[j]);
} else if (j % 2 == 1) {
long substract1 = -min;
if (a[i] - substract1 >= 0) {
long k1 = a[i] - substract1;
long nowsum = sum + substract1;
if (a[j] >= nowsum) {
long k2 = a[j] - nowsum;
ans = ans + 1 + Math.min(k1, k2);
}
}
}
if (j % 2 == 0) {
sum = sum + a[j];
} else {
sum = sum - a[j];
}
min = Math.min(min, sum);
}
}
out.printf("%d\n", ans);
out.close();
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public InputReader(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public boolean hasNext() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 041827b9179002f800ac1665ea0bd186 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main{
static void main() throws Exception{
int n=sc.nextInt();
int[]in=sc.intArr(n);
long ans=0;
for(int i=0;i<n;i+=2) {
long bal=in[i];
long minBal=in[i];
for(int j=i+1;j<n;j+=2) {
bal-=in[j];
long cur=in[j];
long balance=bal;
if(balance<0) {
cur+=balance;
balance=0;
}
long me=in[i];
if(balance>0) {
me-=balance;
balance=0;
}
long min=in[i]-minBal;
long can=Math.min(me-min+1, cur);
// System.out.println(i+" "+j+" "+can);
ans+=Math.max(can, 0);
if(bal<0)break;
minBal=Math.min(minBal, bal);
if(j+1<n) {
bal+=in[j+1];
}
}
}
pw.println(ans);
}
public static void main(String[] args) throws Exception{
sc=new MScanner(System.in);
pw = new PrintWriter(System.out);
int tc=1;
// tc=sc.nextInt();
for(int i=1;i<=tc;i++) {
// pw.printf("Case %d:\n", i);
main();
}
pw.flush();
}
static PrintWriter pw;
static MScanner sc;
static class MScanner {
StringTokenizer st;
BufferedReader br;
public MScanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public MScanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int[] intArr(int n) throws IOException {
int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
return in;
}
public long[] longArr(int n) throws IOException {
long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
return in;
}
public int[] intSortedArr(int n) throws IOException {
int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
shuffle(in);
Arrays.sort(in);
return in;
}
public long[] longSortedArr(int n) throws IOException {
long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
shuffle(in);
Arrays.sort(in);
return in;
}
public Integer[] IntegerArr(int n) throws IOException {
Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt();
return in;
}
public Long[] LongArr(int n) throws IOException {
Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong();
return in;
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
static void shuffle(int[]in) {
for(int i=0;i<in.length;i++) {
int idx=(int)(Math.random()*in.length);
int tmp=in[i];
in[i]=in[idx];
in[idx]=tmp;
}
}
static void shuffle(long[]in) {
for(int i=0;i<in.length;i++) {
int idx=(int)(Math.random()*in.length);
long tmp=in[i];
in[i]=in[idx];
in[idx]=tmp;
}
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 1dcc2e6b4c130ab82efc62c89f359050 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.*;
public class Leetcode {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int [] p=new int[n];
for(int i=0;i<n;i++)
p[i]= scanner.nextInt();
long res=0;
for(int i=0;i<n;i+=2){
long sum=0;
long b=p[i];
for(int j=i+1;j<n;j++){
if(j%2==0){
sum+=p[j];
}else{
if(p[j]<sum){
sum-=p[j];
}else{
if(sum!=0)
res++;
res+=Math.min(b,p[j]-sum);
b-=p[j]-sum;
sum=0;
if(b<0)
break;
}
}
}
}
System.out.println(res);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | d0a5e540de14e6f826e04ea08f2bfef5 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.util.Scanner;
public class MyClass {
static Scanner in=new Scanner(System.in);
public static void main(String args[]) {
int n=in.nextInt();
long a[]=new long[n];
long total=0;
for(int i=0;i<n;i++){
a[i]=in.nextLong();
}
for(int i=0;i<n;i+=2){
long h1=1,hh=a[i];
for(int j=i+1;j<n;j++){
if( j%2==0 ){
h1+=a[j];
hh+=a[j];
}else{
h1-=a[j];
hh-=a[j];
if( hh<0 ){
total+=(hh-h1+1);
break;
}else if( h1<=0 ){
total+=0-h1+1;
h1=0;
}
}
}
}
System.out.println(total);
}
} | Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output | |
PASSED | 3ccc9c811816deb0fd69a3dd8ca4e9c6 | train_107.jsonl | 1630247700 | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number.For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$.You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l \le r$$$) of the original bracket sequence, which are regular bracket sequences.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. | 256 megabytes | import java.io.*;
import java.util.*;
public class Test {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
static String readLine() throws IOException {
return br.readLine();
}
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(readLine());
return st.nextToken();
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
static long readLong() throws IOException {
return Long.parseLong(next());
}
static double readDouble() throws IOException {
return Double.parseDouble(next());
}
static char readChar() throws IOException {
return next().charAt(0);
}
static class Pair implements Comparable<Pair> {
int f, s;
Pair(int f, int s) {
this.f = f; this.s = s;
}
public int compareTo(Pair other) {
if (this.f != other.f) return this.f - other.f;
return this.s - other.s;
}
}
final static long inf = (long)1e18;
static void solve() throws IOException {
int n = readInt();
long ans = 0, c[] = new long[n + 1];
for (int i = 1; i <= n; ++i) {
c[i] = readInt();
if ((i&1) == 1) continue;
c[i] *= -1;
long sum = 0, min = inf;
for (int j = i - 1; j >= 1; --j) {
if ((j&1) == 1) {
long lo = Math.max(-sum, -min), hi = Math.min(c[j], -c[i] - sum);
if (sum + lo == 0) ++lo;
if (lo <= hi) ans += hi - lo + 1;
}
sum += c[j];
min = Math.min(min + c[j], c[j]);
}
}
pr.println(ans);
}
public static void main(String[] args) throws IOException {
solve();
//for (int t = readInt(); t > 0; --t) solve();
pr.close();
}
}
| Java | ["5\n4 1 2 3 1", "6\n1 3 2 1 2 4", "6\n1 1 1 1 2 2"] | 1 second | ["5", "6", "7"] | NoteIn the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) Subsequence from the $$$4$$$th to $$$5$$$th character: () Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) Subsequence from the $$$6$$$th to $$$9$$$th character: (()) Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described.In the third example a sequence ()()(()) is described. | Java 8 | standard input | [
"brute force",
"implementation"
] | ca4ae2484800a98b5592ae65cd45b67f | The first line contains a single integer $$$n$$$ $$$(1 \le n \le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, \dots, c_n$$$ $$$(1 \le c_i \le 10^9)$$$, the compressed sequence. | 1,800 | Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.