CSES - Aalto Competitive Programming 2024 - wk3 - Mon - Results
Submission details
Task:Particle accelerator
Sender:AleksandrPolitov
Submission time:2024-09-16 17:23:37 +0300
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.00 sdetails
#18ACCEPTED0.00 sdetails
#19ACCEPTED0.00 sdetails
#20ACCEPTED0.00 sdetails
#21ACCEPTED0.00 sdetails
#22ACCEPTED0.00 sdetails
#23ACCEPTED0.00 sdetails
#24ACCEPTED0.00 sdetails
#25ACCEPTED0.00 sdetails
#26ACCEPTED0.00 sdetails
#27ACCEPTED0.00 sdetails
#28ACCEPTED0.00 sdetails
#29ACCEPTED0.00 sdetails
#30ACCEPTED0.00 sdetails
#31ACCEPTED0.00 sdetails
#32ACCEPTED0.00 sdetails
#33ACCEPTED0.00 sdetails
#34ACCEPTED0.00 sdetails
#35ACCEPTED0.00 sdetails
#36ACCEPTED0.00 sdetails
#37ACCEPTED0.00 sdetails
#38ACCEPTED0.00 sdetails
#39ACCEPTED0.00 sdetails
#40ACCEPTED0.00 sdetails
#41ACCEPTED0.00 sdetails
#42ACCEPTED0.01 sdetails
#43ACCEPTED0.01 sdetails
#44ACCEPTED0.01 sdetails
#45ACCEPTED0.01 sdetails
#46ACCEPTED0.01 sdetails
#47ACCEPTED0.01 sdetails
#48ACCEPTED0.01 sdetails
#49ACCEPTED0.01 sdetails
#50ACCEPTED0.01 sdetails
#51ACCEPTED0.01 sdetails
#52ACCEPTED0.01 sdetails
#53ACCEPTED0.01 sdetails
#54ACCEPTED0.01 sdetails
#55ACCEPTED0.01 sdetails
#56ACCEPTED0.01 sdetails
#57ACCEPTED0.01 sdetails
#58ACCEPTED0.01 sdetails
#59ACCEPTED0.01 sdetails
#60ACCEPTED0.01 sdetails
#61ACCEPTED0.01 sdetails
#62ACCEPTED0.01 sdetails
#63ACCEPTED0.01 sdetails
#64ACCEPTED0.01 sdetails
#65ACCEPTED0.01 sdetails
#66ACCEPTED0.01 sdetails
#67ACCEPTED0.01 sdetails
#68ACCEPTED0.01 sdetails
#69ACCEPTED0.01 sdetails
#70ACCEPTED0.01 sdetails
#71ACCEPTED0.01 sdetails
#72ACCEPTED0.06 sdetails
#73ACCEPTED0.06 sdetails
#74ACCEPTED0.06 sdetails
#75ACCEPTED0.06 sdetails
#76ACCEPTED0.06 sdetails
#77ACCEPTED0.06 sdetails
#78ACCEPTED0.06 sdetails
#79ACCEPTED0.06 sdetails
#80ACCEPTED0.06 sdetails
#81ACCEPTED0.06 sdetails
#82ACCEPTED0.06 sdetails
#83ACCEPTED0.06 sdetails
#84ACCEPTED0.06 sdetails
#85ACCEPTED0.06 sdetails
#86ACCEPTED0.06 sdetails
#87ACCEPTED0.06 sdetails
#88ACCEPTED0.06 sdetails
#89ACCEPTED0.06 sdetails
#90ACCEPTED0.06 sdetails
#91ACCEPTED0.06 sdetails

Code

#ifdef ONPC
    #define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>

#define char unsigned char
#define rep(i, a, b) for(int i=a; i< (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back

#define LSOne(S) ((S) & -(S))

using namespace std;
// mt19937 rnd(239);
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

typedef long long C;
typedef complex<C> P;
#define X real()
#define Y imag()

template<class T>
istream& operator>> (istream& is, complex<T>& p) {
    T value;
    is >> value;
    p.real(value);
    is >> value;
    p.imag(value);
    return is;
}

typedef long long ll;
typedef long double ld;

using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef DEBUG
#define dbg(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl;
#else
#define dbg(x...)
#endif

template<typename S, typename T = S> void chmin(S &s, T t) {s = s < t ? s : t;}
template<typename S, typename T = S> void chmax(S &s, T t) {s = s > t ? s : t;}

const int INF = 1e9; // 10^9 = 1B is < 2^31-1
const ll LLINF = 4e18; // 4*10^18 is < 2^63-1
const double EPS = 1e-9;
const ll MOD = 1e9+7;

const int N=550;
ll v[N];
ll dp[N][N];
ll pref[N];

int solve() {
    int n; std::cin >> n;
    for (int i = 1; i <= n; i++) {
        std::cin >> v[i];
    }

    pref[0]=pref[1]=0;
    for (int i = 1; i <= n; i++) {
        pref[i+1]=pref[i]+v[i];
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            dp[i][j]=LLINF;
        }
    }

    for (int i = 1; i <= n; i++) {
        dp[i][i]=0;
    }

    

    for (int i = 1; i <= n-1; i++) {
        dp[i][i+1]=v[i]*v[i+1]-min(v[i], v[i+1])*min(v[i], v[i+1]);
    }



    for (int len = 2; len <= n; len++) {
        for (int st = 1; st <= n-len; st++) {
            //std::cout << st << " " << st+len << std::endl;
            //std::cout << len << ": " << std::endl;
            for (int j = 0; j <= len-1; j++) {
                //std::cout << "lol: " << std::endl;
                //std::cout << "x: " <<st << " " << st+j << std::endl;
                //std::cout << "y: " << st+j+1 << " " << st+len << std::endl;
                ll x=pref[st+j+1]-pref[st];
                //dp[st][st+j];
                ll y=pref[st+len+1]-pref[st+j+1];
                dp[st][st+len]=min(dp[st][st+len], dp[st][st+j]+dp[st+j+1][st+len] + x*y-min(x,y)*min(x,y));
                
                //if(j==0) x=v[st];
                //else x=dp[st][st+j];
                
                //if(j==len-1) y=v[st+j+1];
                //else y=dp[st+j+1][st+len];
                //std::cout << "actual: " << x << " " << y << std::endl;
                //ll y=dp[st+j+1][st+len];
                //std::cout << x << " " << y << std::endl;
                //std::cout << x << " " << y << std::endl;
                

                //dp[st][st+len]=min(dp[st][st+len], dp[st][st+j]+dp[st+j+1][st+len] + x*y-min(x,y)*min(x,y));
                
                //dp[st][st+len]=min(dp[st][st+len], dp[st][st+j]*dp[st+j+1][st+len]-min(dp[st][mid], dp[mid][st+len])*min(dp[st][mid], dp[mid+1][st+len]));
                //std::cout << j << std::endl;
            }
            //for (int mid = st+1; mid <= st+len-1; mid++) {
                //dp[st][st+len]=min(dp[st][st+len], dp[st][mid]*dp[mid+1][st+len]-min(dp[st][mid], dp[mid][st+len])*min(dp[st][mid], dp[mid+1][st+len]));
            //}
            //std::cout << st << " " << st+len << std::endl;
        }
    }
    //std::cout << dp[1][3] << std::endl;

    std::cout << dp[1][n] << std::endl;


    return 0;
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int TET = 1;
    //cin >> TET;
    for (int i = 1; i <= TET; i++) {
        #ifdef ONPC
            cout << "TEST CASE#" << i << endl;
        #endif
        
        if (solve()) {
            break;
        }

        #ifdef ONPC
            cout << "__________________________" << endl;
        #endif
    }
    #ifdef ONPC
        cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
    #endif
}

Test details

Test 1

Verdict: ACCEPTED

input
1
373 

correct output
0

user output
0

Test 2

Verdict: ACCEPTED

input
1
10 

correct output
0

user output
0

Test 3

Verdict: ACCEPTED

input
2
1 7 

correct output
6

user output
6

Test 4

Verdict: ACCEPTED

input
2
798 810 

correct output
9576

user output
9576

Test 5

Verdict: ACCEPTED

input
3
2 7 9 

correct output
10

user output
10

Test 6

Verdict: ACCEPTED

input
3
43371 770816 582162 

correct output
166626155145

user output
166626155145

Test 7

Verdict: ACCEPTED

input
3
255995 678296 258159 

correct output
282656505483

user output
282656505483

Test 8

Verdict: ACCEPTED

input
4
2 4 5 10 

correct output
19

user output
19

Test 9

Verdict: ACCEPTED

input
4
10 1 10 5 

correct output
78

user output
78

Test 10

Verdict: ACCEPTED

input
4
4 10 10 8 

correct output
96

user output
96

Test 11

Verdict: ACCEPTED

input
4
635350 99359 612245 308607 

correct output
283721279422

user output
283721279422

Test 12

Verdict: ACCEPTED

input
5
8 9 7 9 6 

correct output
167

user output
167

Test 13

Verdict: ACCEPTED

input
5
8 10 1 2 4 

correct output
97

user output
97

Test 14

Verdict: ACCEPTED

input
5
25933 931751 549787 947945 435...

correct output
642593396167

user output
642593396167

Test 15

Verdict: ACCEPTED

input
5
709 840 291 122 511 

correct output
731471

user output
731471

Test 16

Verdict: ACCEPTED

input
5
6 2 10 9 8 

correct output
49

user output
49

Test 17

Verdict: ACCEPTED

input
5
870929 831516 206766 363819 91...

correct output
581247680937

user output
581247680937

Test 18

Verdict: ACCEPTED

input
5
4 3 9 1 1 

correct output
45

user output
45

Test 19

Verdict: ACCEPTED

input
5
780095 319045 438508 978444 72...

correct output
492410128596

user output
492410128596

Test 20

Verdict: ACCEPTED

input
5
969 240 870 378 531 

correct output
955854

user output
955854

Test 21

Verdict: ACCEPTED

input
5
501988 499243 495885 7545 1338...

correct output
284384673000

user output
284384673000

Test 22

Verdict: ACCEPTED

input
10
8 9 7 9 6 9 5 7 7 4 

correct output
347

user output
347

Test 23

Verdict: ACCEPTED

input
10
8 10 1 2 4 10 2 3 1 4 

correct output
194

user output
194

Test 24

Verdict: ACCEPTED

input
10
25933 931751 549787 947945 435...

correct output
1909421677160

user output
1909421677160

Test 25

Verdict: ACCEPTED

input
10
709 840 291 122 511 570 893 43...

correct output
2139925

user output
2139925

Test 26

Verdict: ACCEPTED

input
10
6 2 10 9 8 7 7 6 3 2 

correct output
339

user output
339

Test 27

Verdict: ACCEPTED

input
10
870929 831516 206766 363819 91...

correct output
2999634317613

user output
2999634317613

Test 28

Verdict: ACCEPTED

input
10
4 3 9 1 1 4 2 10 6 1 

correct output
194

user output
194

Test 29

Verdict: ACCEPTED

input
10
780095 319045 438508 978444 72...

correct output
3083316036349

user output
3083316036349

Test 30

Verdict: ACCEPTED

input
10
969 240 870 378 531 817 233 42...

correct output
1139241

user output
1139241

Test 31

Verdict: ACCEPTED

input
10
501988 499243 495885 7545 1338...

correct output
554211450134

user output
554211450134

Test 32

Verdict: ACCEPTED

input
100
8 9 7 9 6 9 5 7 7 4 5 3 9 1 10...

correct output
6396

user output
6396

Test 33

Verdict: ACCEPTED

input
100
8 10 1 2 4 10 2 3 1 4 2 4 4 7 ...

correct output
3542

user output
3542

Test 34

Verdict: ACCEPTED

input
100
25933 931751 549787 947945 435...

correct output
45440863225859

user output
45440863225859

Test 35

Verdict: ACCEPTED

input
100
709 840 291 122 511 570 893 43...

correct output
45958697

user output
45958697

Test 36

Verdict: ACCEPTED

input
100
6 2 10 9 8 7 7 6 3 2 10 3 1 7 ...

correct output
3828

user output
3828

Test 37

Verdict: ACCEPTED

input
100
870929 831516 206766 363819 91...

correct output
38360827705542

user output
38360827705542

Test 38

Verdict: ACCEPTED

input
100
4 3 9 1 1 4 2 10 6 1 6 7 5 6 4...

correct output
5967

user output
5967

Test 39

Verdict: ACCEPTED

input
100
780095 319045 438508 978444 72...

correct output
42394120248428

user output
42394120248428

Test 40

Verdict: ACCEPTED

input
100
969 240 870 378 531 817 233 42...

correct output
43869305

user output
43869305

Test 41

Verdict: ACCEPTED

input
100
501988 499243 495885 7545 1338...

correct output
51929619520205

user output
51929619520205

Test 42

Verdict: ACCEPTED

input
200
21 495 634 444 749 832 499 584...

correct output
94150203

user output
94150203

Test 43

Verdict: ACCEPTED

input
200
19480 664871 463323 194160 725...

correct output
109172696765966

user output
109172696765966

Test 44

Verdict: ACCEPTED

input
200
740217 873584 263375 86454 533...

correct output
91609970771011

user output
91609970771011

Test 45

Verdict: ACCEPTED

input
200
3 9 9 9 10 2 10 8 5 8 7 7 8 8 ...

correct output
12594

user output
12594

Test 46

Verdict: ACCEPTED

input
200
8 7 9 4 1 2 4 3 10 3 6 4 4 4 6...

correct output
11434

user output
11434

Test 47

Verdict: ACCEPTED

input
200
2 6 1 2 4 9 3 10 6 1 4 9 4 8 2...

correct output
10973

user output
10973

Test 48

Verdict: ACCEPTED

input
200
523282 55700 550826 308687 456...

correct output
92752119769026

user output
92752119769026

Test 49

Verdict: ACCEPTED

input
200
530707 844425 191564 889066 67...

correct output
99915887908116

user output
99915887908116

Test 50

Verdict: ACCEPTED

input
200
506 433 879 245 182 909 853 20...

correct output
100894725

user output
100894725

Test 51

Verdict: ACCEPTED

input
200
761422 412836 246994 715471 13...

correct output
91724968917854

user output
91724968917854

Test 52

Verdict: ACCEPTED

input
200
9 10 9 10 9 7 1 7 7 10 4 7 6 1...

correct output
9260

user output
9260

Test 53

Verdict: ACCEPTED

input
200
3 7 8 1 1 10 3 7 1 2 4 9 7 4 4...

correct output
13944

user output
13944

Test 54

Verdict: ACCEPTED

input
200
5 8 5 7 9 8 2 9 4 9 3 3 7 1 3 ...

correct output
15692

user output
15692

Test 55

Verdict: ACCEPTED

input
200
10 2 8 8 3 6 3 5 7 2 2 1 4 3 7...

correct output
12255

user output
12255

Test 56

Verdict: ACCEPTED

input
200
700 53 1000 318 221 984 362 51...

correct output
126087673

user output
126087673

Test 57

Verdict: ACCEPTED

input
200
6 5 3 1 2 2 5 2 2 3 7 7 5 2 6 ...

correct output
11199

user output
11199

Test 58

Verdict: ACCEPTED

input
200
6 3 8 6 8 1 9 2 2 3 3 8 5 9 8 ...

correct output
13071

user output
13071

Test 59

Verdict: ACCEPTED

input
200
814768 915127 735563 468064 86...

correct output
98792356012408

user output
98792356012408

Test 60

Verdict: ACCEPTED

input
200
562 289 125 165 398 398 782 31...

correct output
94162582

user output
94162582

Test 61

Verdict: ACCEPTED

input
200
3 4 1 10 8 5 5 4 6 4 8 9 9 9 8...

correct output
11090

user output
11090

Test 62

Verdict: ACCEPTED

input
200
4 3 7 5 2 7 10 8 4 7 10 10 3 7...

correct output
11655

user output
11655

Test 63

Verdict: ACCEPTED

input
200
958322 322406 770487 611574 98...

correct output
81490639470393

user output
81490639470393

Test 64

Verdict: ACCEPTED

input
200
373 422 556 331 956 754 737 73...

correct output
88581726

user output
88581726

Test 65

Verdict: ACCEPTED

input
200
5 7 5 10 3 2 9 8 2 7 1 9 10 9 ...

correct output
13948

user output
13948

Test 66

Verdict: ACCEPTED

input
200
780277 423471 92725 418110 633...

correct output
136105358146422

user output
136105358146422

Test 67

Verdict: ACCEPTED

input
200
308420 676676 231540 246163 27...

correct output
104365913260031

user output
104365913260031

Test 68

Verdict: ACCEPTED

input
200
602 717 953 825 144 864 808 92...

correct output
82761699

user output
82761699

Test 69

Verdict: ACCEPTED

input
200
465 525 193 757 582 310 621 85...

correct output
101145827

user output
101145827

Test 70

Verdict: ACCEPTED

input
200
9 10 10 9 8 5 7 10 7 6 3 2 4 2...

correct output
9822

user output
9822

Test 71

Verdict: ACCEPTED

input
200
798 810 821 367 123 829 603 60...

correct output
111853493

user output
111853493

Test 72

Verdict: ACCEPTED

input
500
21 495 634 444 749 832 499 584...

correct output
320652082

user output
320652082

Test 73

Verdict: ACCEPTED

input
500
19480 664871 463323 194160 725...

correct output
255800779186420

user output
255800779186420

Test 74

Verdict: ACCEPTED

input
500
740217 873584 263375 86454 533...

correct output
282774432682654

user output
282774432682654

Test 75

Verdict: ACCEPTED

input
500
3 9 9 9 10 2 10 8 5 8 7 7 8 8 ...

correct output
30727

user output
30727

Test 76

Verdict: ACCEPTED

input
500
8 7 9 4 1 2 4 3 10 3 6 4 4 4 6...

correct output
28929

user output
28929

Test 77

Verdict: ACCEPTED

input
500
2 6 1 2 4 9 3 10 6 1 4 9 4 8 2...

correct output
39800

user output
39800

Test 78

Verdict: ACCEPTED

input
500
523282 55700 550826 308687 456...

correct output
367458083034844

user output
367458083034844

Test 79

Verdict: ACCEPTED

input
500
530707 844425 191564 889066 67...

correct output
276567810216590

user output
276567810216590

Test 80

Verdict: ACCEPTED

input
500
506 433 879 245 182 909 853 20...

correct output
357098617

user output
357098617

Test 81

Verdict: ACCEPTED

input
500
761422 412836 246994 715471 13...

correct output
345118298392748

user output
345118298392748

Test 82

Verdict: ACCEPTED

input
500
9 10 9 10 9 7 1 7 7 10 4 7 6 1...

correct output
37385

user output
37385

Test 83

Verdict: ACCEPTED

input
500
3 7 8 1 1 10 3 7 1 2 4 9 7 4 4...

correct output
45054

user output
45054

Test 84

Verdict: ACCEPTED

input
500
5 8 5 7 9 8 2 9 4 9 3 3 7 1 3 ...

correct output
48480

user output
48480

Test 85

Verdict: ACCEPTED

input
500
10 2 8 8 3 6 3 5 7 2 2 1 4 3 7...

correct output
35867

user output
35867

Test 86

Verdict: ACCEPTED

input
500
700 53 1000 318 221 984 362 51...

correct output
344007358

user output
344007358

Test 87

Verdict: ACCEPTED

input
500
6 5 3 1 2 2 5 2 2 3 7 7 5 2 6 ...

correct output
32937

user output
32937

Test 88

Verdict: ACCEPTED

input
500
6 3 8 6 8 1 9 2 2 3 3 8 5 9 8 ...

correct output
33083

user output
33083

Test 89

Verdict: ACCEPTED

input
500
814768 915127 735563 468064 86...

correct output
352570895811941

user output
352570895811941

Test 90

Verdict: ACCEPTED

input
500
562 289 125 165 398 398 782 31...

correct output
349500395

user output
349500395

Test 91

Verdict: ACCEPTED

input
500
3 4 1 10 8 5 5 4 6 4 8 9 9 9 8...

correct output
35153

user output
35153