201C - Fragile Bridges - CodeForces Solution


dp *2000

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
long long l[maxn],r[maxn],odd[maxn];
int n;
long long a[maxn];
int get(int x)
{
    if(x%2==1)return x-1;
    return x;
}
set<long long>s;
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n-1;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<n-1;i++)
        if(a[i-1]>1)
            l[i]=l[i-1]+get(a[i-1]);
    for(int i=n-2;i>=0;i--)
        if(a[i]>1)
            r[i]=r[i+1]+get(a[i]);
    for(int i=0;i<n-1;i++)
    {
        odd[i]+=get(a[i]-1)+1;
        if(i>0)odd[i]+=odd[i-1];
    }
    long long ans = l[0]+r[0];
    long long tmp = ans;
    s.insert(l[0]);
    for(int i=1;i<n;i++)
    {
        s.insert(l[i]-odd[i-1]);
        long long p=*--s.lower_bound(1LL*1e16);
        ans=max(r[i]+odd[i-1]+p,ans);
    }
    cout<<ans<<endl;
}


Comments

Submit
0 Comments
More Questions

734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String
1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ