855B - Marvolo Gaunt's Ring - CodeForces Solution


brute force data structures dp *1500

Please click on ads to support us..

Python Code:

n,p,q,r = [int(x) for x in input().split()]
a= [int(x) for x in input().split()]
m = len(a)
max1= [None]*m
pmax = [None]*m
rmax= [None]*m
j=m-1
pmax[0] = p*a[0]
rmax[m-1] = r*a[m-1]
for i in range(1,m):
    pmax[i] = max(pmax[i-1], (p*a[i]))
    rmax[j-1] = max(rmax[j], (r*a[j-1]))
    j-=1
max1[0] = pmax[0]+q*a[0]+rmax[0]    
for k in range(1,m):
    max1[k] = max(max1[k-1], pmax[k]+q*a[k]+rmax[k])
print(max1[m-1])

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define Ain(A, n)              \
    for (ll i = 0; i < n; i++) \
    {                          \
        cin >> A[i];           \
    }
#define MAX(A) *max_element(A.begin(), A.end())
#define MIN(A) *min_element(A.begin(), A.end())

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll n, p, q, r;
    cin >> n >> p >> q >> r;

    ll valP = INT64_MIN;
    ll valQ = INT64_MIN;
    ll valR = INT64_MIN;

    ll a;

    for (int i = 1; i <= n; i++)
    {
        cin >> a;
        valP = max(valP, p * a);
        valQ = max(valQ, valP + q * a);
        valR = max(valR, valQ + r * a);
    }
    cout << valR << endl;

    return 0;
}


Comments

Submit
0 Comments
More Questions

439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum