Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define pi pair<int,int>
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
void setIO() {
ios_base::sync_with_stdio(0);cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
int main()
{
setIO();
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
vi a(n+1);
for(int i=1;i<=n;++i)
cin>>a[i];
for(int i=1;i<=n;++i)
a[i]+=a[i-1];
ll f=a[n];
ll ans=1e18;
for(int i=k;i<=n;++i){
ll x=a[i]-a[i-k];
ll c=(x*(x+1)/2);
ans=min(ans,c+f-x);
}
cout<<ans<<endl;
}
return 0;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
// your code goes here
ll t,m,n,i,j,k,p,q;
cin>>t;
while(t--)
{
cin>>n>>k;
ll a[n];
p=0;
for(i=0;i<n;i++)
{
cin>>a[i];
if(a[i]==1)
p++;
}
ll x=0,y=INT_MAX;
//l=0;
for(i=0;i<n;i++)
{
if(a[i]==1)
x++;
if(i-k>=0&&a[i-k]==1)
{
x--;
}
if(i>=k-1)
y=min(y,x);
}
x=y;
p=p-x;
x=(x*(x+1))/2;
x=x+p;
cout<<x<<endl;
}
return 0;
}
# cook your dish here
# cook your dish here
T = int(input())
for _ in range(T):
n,k = map(int,input().split())
A = list(map(int,input().split()))
m = k
L = [0]*n
for i in range(n):
if A[i] == 1:
if i == 0:
L[i] = 1
else:
L[i] = L[i-1] +1
else:
if i == 0:
L[i] = 0
else:
L[i] = L[i-1]
if i < k-1:
pass
else:
if i == k-1:
m = min(m,L[k-1])
else:
m= min(m,L[i]-L[i-k])
x = L[-1]
x -= m
ans = (m*(m+1))//2 + x
print(ans)
#include <stdio.h>
int summation(int min)
{
int sum=0;
for(int i=min-1 ; i>0 ; i--)
{
sum+=i;
}
return sum;
}
int main()
{
int t;
scanf("%d", &t);
int n, k;
while (t > 0)
{
scanf("%d %d",&n,&k);
int array[n];
for(int i=0 ; i<n ; i++)
{
scanf("%d",&array[i]);
}
int l=0,cost=0,length=1,num_ones=0;
if(array[0]==1)
{
cost=1;
num_ones++;
}
int min=k;
for(int r=1 ; r<n ; r++)
{
length++;
if(length>k)
{
length--;
l++;
cost-=array[r-k];
}
if(array[r]==1)
{
cost++;
num_ones++;
}
if(cost<min && length==k)
min=cost;
}
if(min==0 || min==1)
printf("%d\n",num_ones);
else
printf("%d\n",num_ones+summation(min));
t--;
}
return 0;
}
import java.util.*;
import java.io.*;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
class Hash{
long arr[];
long p = 1 , mod = (long)1e9+7;
public long pow(long a ,long b,long mod) {
if(b==0) return 1l;
if(b==1) return a;
long ans = pow(a,b/2,mod)%mod;
if(b%2==1) {
return (((ans*ans)%mod)*a)%mod;
}
return (ans*ans)%mod;
}
Hash(String s) {
int n = s.length();
arr = new long[n+1];
for(int i =1;i<=n ;i++) {
arr[i] = (arr[i-1] + (s.charAt(i-1)-'A' + 1)*p)%mod;
arr[i] %= mod;
p = (p*31) % mod;
}
}
public long hash(int l , int r) {
System.out.println(l + " " + r);
long ans = (this.arr[r + 1] - this.arr[l] + mod)%mod;
long inverse = this.pow(31 , l , mod);
inverse = this.pow(inverse , mod-2 ,mod);
ans = ans * inverse;
return ans%mod;
}
}
class Node {
int val , low ,high;
Node left , right;
Node(int val,int low ,int high) {
this.val = val;
this.low = low;
this.high = high;
}
public String toString() {
return this.val + " " + this.low + "<- ->" + this.high;
}
}
class Segment {
public Node make(int l,int r ,int arr[]) {
if(l > r) return null;
int sum = arr[r];
System.out.println("At "+ l +" " + r);
if(l-1>=0) sum -= arr[l-1];
int mid = l + (r-l)/2;
Node newnode = new Node(sum , l , r);
if(l==r) return newnode;
newnode.left = make(l , mid,arr);
newnode.right = make(mid + 1, r ,arr);
return newnode;
}
}
public class Main{
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(new FileWriter(problemName + ".out"));
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) { }
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
static Kattio sc = new Kattio();
static long mod = (long)1e9+7;
static PrintWriter out =new PrintWriter(System.out);
public static void buildHeap(int arr[], int n) {
for(int i = n/2-1;i>=0;i--) {
heapify(arr,n,i);
}
}
//Heapify function to maintain heap property.
public static void heapify(int arr[], int n, int i) {
int l = i*2 + 1 , r = i*2 + 2;
int small = l;
if(r<n&& arr[r]<arr[small]) {
small = r;
}
if(l>=n || arr[i]< arr[small]) return;
if(small != i)swap(small, i, arr);
heapify(arr,n,small);
}
public static void swap(int i,int j,int arr[]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void heapSort(int arr[], int n) {
buildHeap(arr,n);
System.out.println(Arrays.toString(arr));
int ans[] = new int[n];
int l = 0 , r = n-1;
while(l<=r) {
swap(l,r,arr);
r--;
heapify(arr,r + 1,0);
}
}
public static long getInversion(String a) {
long zero =0;
for(char ch : a.toCharArray()) {
if(ch=='0') zero++;
}
long ans = 0;
for(char ch : a.toCharArray()) {
if(ch=='1') {
ans += zero;
}
else zero--;
}
return ans;
}
public static List<Integer> getAllParent (int node) {
List<Integer> list = new ArrayList<>();
while(node >= 1) {
list.add(node);
node /= 2;
}
return list;
}
public static boolean isSubsequence(String small, String big) {
// System.out.println( small + " " + big);
int i =0 , j = 0;
while(i< small.length() && j < big.length()) {
if(small.charAt(i) == big.charAt(j)) {
i++;
j++;
}
else {
j++;
}
}
if(i >= small.length()) return true;
else return false;
}
static long arr[][] = new long[1005][1005];
public static void main(String[] args)throws IOException {
int t = sc.nextInt();
while(t-->0) {
solve();
}
out.close();
}
public static void solve()throws IOException {
int n= sc.nextInt() , k = sc.nextInt();
int arr[] = new int[n];
for(int i =0;i<n;i++) {
arr[i] = sc.nextInt();
}
long sum = 0 , min = 0 , total = 0;
for(int i =0;i<k;i++) {
sum += arr[i];
total += arr[i];
}
min = sum;
int l = 0 , r = k;
while(r < n) {
sum = sum -arr[l] + arr[r];
total += arr[r];
l++;
r++;
if(sum < min) {
min = sum;
}
}
long ans = min*(min + 1)/2 + (total - min);
System.out.println(ans);
}
public static long callfun(int x1 , int y1,int x2,int y2,long arr[][] , long memo[][]) {
if(x1 == x2 && y1 == y2) return arr[x1][y1];
if(x1 > x2) return Integer.MIN_VALUE;
if(y1 > y2) return Integer.MIN_VALUE;
if(memo[x1][y1] != -1) return memo[x1][y1];
else {
return memo[x1][y1] = arr[x1][y1] + Math.max(callfun(x1 + 1 ,y1 ,x2,y2,arr , memo ),callfun(x1 ,y1 + 1 ,x2,y2,arr , memo ));
}
}
public static boolean isVowel(char ch) {
if(ch == 'a' || ch == 'e'||ch == 'i' || ch == 'o' || ch == 'u') return true;
return false;
}
public static String getAns(int i,int j,String s) {
if(j>=s.length()) return s.charAt(i) + "";
while(i>=0 && j<s.length() && s.charAt(i) == s.charAt(j)) {
i--;
j++;
}
return s.substring(i +1, j);
}
public static int getFactor(int num) {
if(num==1) return 1;
int ans = 2;
int k = num/2;
for(int i = 2;i<=k;i++) {
if(num%i==0) ans++;
}
return Math.abs(ans);
}
public static int geti(char ch) {
if(ch=='R') return 2;
if(ch=='L') return 3;
if(ch=='U') return 1;
else return 0;
}
public static int[] readarr()throws IOException {
int n = sc.nextInt();
int arr[] = new int[n];
for(int i =0;i<n;i++) {
arr[i] = sc.nextInt();
}
return arr;
}
public static boolean isPowerOfTwo (long x) {
return x!=0 && ((x&(x-1)) == 0);
}
public static boolean isPrime(long num) {
if(num==1) return false;
if(num<=3) return true;
if(num%2==0||num%3==0) return false;
for(long i =5;i*i<=num;i+=6) {
if(num%i==0) return false;
}
return true;
}
public static boolean isPrime(int num) {
if(num==1) return false;
if(num<=3) return true;
if(num%2==0||num%3==0) return false;
for(int i =5;i*i<=num;i+=6) {
if(num%i==0) return false;
}
return true;
}
public static long gcd(long a , long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
public static long get_gcd(long a , long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
public static long fac(long num) {
long ans = 1;
int mod = (int)1e9+7;
for(long i = 2;i<=num;i++) {
ans = (ans*i)%mod;
}
return ans;
}
}
for _ in range(int(input())):
n,k=map(int,input().split())
l=list(map(int,input().split()))
s=sum(l[:k])
y=k
i=0
ans=0
mina=s
w=s
a=[s]
for i in range(n-k):
w=w-l[i]+l[i+k]
a.append(w)
x=min(a)
ans=sum(l)
print(ans-x+(x*(x+1))//2)
t = int(raw_input())
for i in range(t):
st = raw_input().split()
N = int(st[0])
K = int(st[1])
st = raw_input().split()
A = []
tot = 0
for x in st:
n = int(x)
A.append(n)
tot += n
# endfor x
w = 0
for p in range(K):
w += A[p]
# endfor p
bw = w
for p in range(K,N):
w += A[p] - A[p-K]
if w < bw:
bw = w
# endif
# endfor p
tot += bw*(bw-1)/2
print tot
# endfor i
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
using System.Numerics;
public class Test
{
public static int GetInt() => int.Parse(Console.ReadLine());
public static long GetLong() => long.Parse(Console.ReadLine());
public static int[] GetIntArray() => Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
public static long[] GetLongArray() => Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray();
public static string[] GetLines(int n)
{
var ans = new string[n];
for(int i=0; i<n; i++)
{
ans[i] = Console.ReadLine();
}
return ans;
}
public static int Gcd(int a, int b) => b == 0 ? a : Gcd(b, a%b);
public static int Gcd(int[] n) => n.Aggregate((a,b) => Gcd(a,b));
/* public (int,int,int) GcdE(int a, int b)
{
if(b == 0)
return (a,1,0);
(int d, int s, int t) = GcdE(b, a%b);
//returns (g,x,y)
// where g is the Gcd and x,y solve x*a + y*b = g
return (d,t,s - (a/b)*t);
}
*/
public static void Main()
{
var t=GetInt();
for(int i = 0; i<t; i++)
{
Solve();
}
// Solve();
}
public static void Solve()
{
var arr = GetIntArray();
int n = arr[0];
int k = arr[1];
int[] a = GetIntArray();
long ans = F(k, a);
Console.WriteLine(ans);
}
public static long F(int k, int [] a)
{
int n = a.Length;
int[] pref = new int[n+1];
for(int i=0;i<n; i++)
pref[i+1] = pref[i] + a[i];
long min = n;
for(int i=0; i+k<=n;i++)
min = Math.Min(min, pref[i+k]-pref[i]);
long ans = a.Sum() - min + min * (min+1) / 2;
return ans;
}
}
package main
import (
"bufio"
"bytes"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
tc := readNum(reader)
var buf bytes.Buffer
for tc > 0 {
tc--
n, K := readTwoNums(reader)
A := readNNums(reader, n)
res := solve(n, K, A)
buf.WriteString(fmt.Sprintf("%d\n", res))
}
fmt.Print(buf.String())
}
func readInt(bytes []byte, from int, val *int) int {
i := from
sign := 1
if bytes[i] == '-' {
sign = -1
i++
}
tmp := 0
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + int(bytes[i]-'0')
i++
}
*val = tmp * sign
return i
}
func readNum(reader *bufio.Reader) (a int) {
bs, _ := reader.ReadBytes('\n')
readInt(bs, 0, &a)
return
}
func readTwoNums(reader *bufio.Reader) (a int, b int) {
res := readNNums(reader, 2)
a, b = res[0], res[1]
return
}
func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
res := readNNums(reader, 3)
a, b, c = res[0], res[1], res[2]
return
}
func readNNums(reader *bufio.Reader, n int) []int {
res := make([]int, n)
x := 0
bs, _ := reader.ReadBytes('\n')
for i := 0; i < n; i++ {
for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
x++
}
x = readInt(bs, x, &res[i])
}
return res
}
func readUint64(bytes []byte, from int, val *uint64) int {
i := from
var tmp uint64
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + uint64(bytes[i]-'0')
i++
}
*val = tmp
return i
}
func solve(N int, K int, A []int) int64 {
// let's find some ranges [L...R] that can have min cost, to make them all zeros
var L, R int
var best = int64(N) * int64(N)
var sum int
for i := 0; i < N; i++ {
sum += A[i]
if i+1-K >= 0 {
if i-K >= 0 {
sum -= A[i-K]
}
// to set all ones between i - K... i the cost is
x := int64(sum)
// (1 + x) * x / 2
x = (1 + x) * x / 2
if x < best {
best = x
L = i - K + 1
R = i
}
}
}
res := best
for i := L - 1; i >= 0; i-- {
res += int64(A[i])
}
for i := R + 1; i < N; i++ {
res += int64(A[i])
}
return res
}
func max(a, b int) int {
if a >= b {
return a
}
return b
}
func min(a, b int) int {
if a <= b {
return a
}
return b
}
In our experience, we suggest you solve this Consecutive Deletions CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Consecutive Deletions CodeChef Solution.
“I hope this Consecutive Deletions CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.
Keep Learning!