1358D - The Best Vacation - CodeForces Solution


binary search brute force greedy implementation two pointers *1900

Please click on ads to support us..

Python Code:

from sys import stdin
input = stdin.readline





def answer():

    prefix = [0]
    psum = [0]

    for i in range(n):

        prefix.append(prefix[-1] + a[i])
        psum.append(psum[-1] + (a[i] * (a[i] + 1))//2)


    ans = 0
    for i in range(n):

        l , h = i + 1 , 2 * n
        val , sub , upper = 0 , 0 , i
        while(l <= h):

            mid = (l + h) // 2

            if(mid <= n):value = prefix[mid] - prefix[i]
            else:value = prefix[n] - prefix[i] + prefix[mid - n]
            
            if(value <= d):
                
                if(mid <= n):val = psum[mid] - psum[i]
                else:val = psum[n] - psum[i] + psum[mid - n]

                sub = value
                upper = mid
                
                l = mid + 1
            else:
                h = mid - 1


        req = d - sub
        if(req == 0):
            ans = max(ans , val)
            continue

        upper %= n

        buffer = a[upper] - req
        val += (a[upper] * (a[upper] + 1))//2
        val -= (buffer * (buffer + 1))//2

        ans = max(ans , val)
        
    return ans

     
for T in range(1):

    n , d = map(int,input().split())

    a = list(map(int,input().split()))

    print(answer())

    

C++ Code:

#include <bits/stdc++.h>
 
using namespace std;
 
long long triangleNumber(long long x)
{
    return x * (x + 1) / 2;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, l;
    long long x;
    long long maxHugs = 0LL;
    long long currHugs = 0LL;
    cin >> n >> x;
    int d[2 * n];
    long long pd[2 * n + 1];
    pd[0] = 0LL;
    for (int i = 0; i < n; ++i)
    {
        cin >> d[i];
        d[n + i] = d[i];
    }
    for (int i = 0; i < (n << 1); ++i)
    {
        pd[i + 1] = pd[i] + d[i];
    }
    l = 0;
    for (int r = 1; r <= 2 * n; ++r)
    {
        currHugs += triangleNumber(d[r - 1]);
        if (pd[r] < x)
        {
            continue;
        }
        while (l < r && pd[r] - pd[l] > x)
        {
            currHugs -= triangleNumber(d[l]);
            ++l;
        }
        if (pd[r] - pd[l] == x)
        {
            maxHugs = max(maxHugs, currHugs);
        }
        else
        {
            maxHugs = max(maxHugs, currHugs + triangleNumber(d[l - 1])
            - triangleNumber(d[l - 1] - (x - pd[r] + pd[l])));
        }
    }
    cout << maxHugs << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

841A - Generous Kefa
1690B - Array Decrements
1692C - Where's the Bishop
104A - Blackjack
1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum