/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 512.0 KiB
#2 Accepted 121ms 692.0 KiB
#3 Accepted 131ms 1008.0 KiB
#4 Accepted 176ms 4.742 MiB
#5 Accepted 226ms 42.301 MiB
#6 Accepted 484ms 417.93 MiB
#7 Accepted 491ms 417.848 MiB
#8 Accepted 489ms 417.938 MiB
#9 Accepted 492ms 417.926 MiB
#10 Accepted 493ms 417.934 MiB
#11 Accepted 489ms 417.934 MiB
#12 Accepted 484ms 417.93 MiB
#13 Accepted 489ms 417.934 MiB
#14 Accepted 486ms 417.926 MiB
#15 Accepted 489ms 417.926 MiB
#16 Accepted 477ms 417.789 MiB
#17 Accepted 149ms 129.52 MiB
#18 Accepted 52ms 17.082 MiB
#19 Accepted 69ms 20.586 MiB
#20 Accepted 491ms 417.93 MiB

Code

/*For today, you happen to be the defeated. But what will you become tomorrow?*/

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define int long long int 
#define dbg if(debugg)
#define F first 
#define S second 

bool debugg = false;

template <typename T>
using order_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;

template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());  
const int N = 200005;
int arr[N], dp[N][4][4][4][4], n, mx = 7;

int magic(int pos, int m1, int m2, int m3, int m4)
{
    if(pos > n) return 0;
    if(m1 >= 3) m1 = 0;
    if(m2 >= 3) m2 = 0;
    if(m3 >= 3) m3 = 0;
    if(m4 >= 3) m4 = 0;
    int &ans = dp[pos][m1][m2][m3][m4];
    if(~ans) return ans;
    ans = magic(pos + 1, m1 + (m1 != 0), m2 + (m2 != 0), m3 + (m3 != 0), m4 + (m4 != 0));
    if(m1 == 0) ans = max(ans, 4 * arr[pos] + magic(pos + 1, 1, m2 + (m2 != 0), m3 + (m3 != 0), m4 + (m4 != 0)));
    if(m2 == 0) ans = max(ans, 3 * arr[pos] + magic(pos + 1, m1 + (m1 != 0), 1, m3 + (m3 != 0), m4 + (m4 != 0)));
    if(m3 == 0) ans = max(ans, 2 * arr[pos] + magic(pos + 1, m1 + (m1 != 0), m2 + (m2 != 0), 1, m4 + (m4 != 0)));
    if(m4 == 0) ans = max(ans, 1 * arr[pos] + magic(pos + 1, m1 + (m1 != 0), m2 + (m2 != 0), m3 + (m3 != 0), 1));
    return ans; 
}

void solve()
{
    cin >> n;
    int sum = 0;
    for(int i = 1; i <= n; i ++){
        cin >> arr[i];
        sum += 4 * arr[i];
    }

    for(int i = 0; i <= n; i ++){
        for(int j = 0; j < 4; j ++){
            for(int k = 0; k < 4; k ++){
                for(int l = 0; l < 4; l ++){
                    for(int m = 0; m < 4; m ++){
                        dp[i][j][k][l][m] = -1;
                    }
                }
            }
        }
    }

    int del = magic(1, 0, 0, 0, 0);
    cout << sum - del << '\n';

}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
        
    int t = 1;
    cin >> t;
    
    while(t--){
        solve();
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1087 Face the monsters
Contest
Brain Booster #5
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-05 17:22:37
Judged At
2024-10-03 13:04:30
Judged By
Score
100
Total Time
493ms
Peak Memory
417.938 MiB