Submission details
Task:Card game
Sender:aalto26ch_045
Submission time:2026-09-14 03:42:44 +0300
Language:C++ (C++23)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#40.00 sdetails
#50.00 sdetails
#60.01 sdetails
#70.03 sdetails
#80.32 sdetails
#90.32 sdetails
#100.32 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14--details
#15--details

Code

#include <iostream>
#include <vector>
#include <functional>


void printNewLine(){std::cout << "\n";}
template<typename T, typename... Ts>
std::tuple<Ts...> tail(std::tuple<T,Ts...> as){
    return std::apply([](T &a, Ts... b){return std::make_tuple(b...);},as);
}

template<typename T>
void print(T a){std::cout << a;}

template<typename T, typename Ts>
void print(std::tuple<T,Ts> a){
    print(std::get<0>(a));
    print(" ");
    print(std::get<1>(a));
}

template<typename T, typename... Ts>
void print(std::tuple<T,Ts...> a){
    print(std::get<0>(a));
    print(" ");
    print(tail(a));
}
template<typename T>
void println(T a){print(a);printNewLine();}
template<typename T>
void printVec(std::vector<T> arr, int width = -1){
    if (width == -1) width = arr.size();
    for (int i = 0; i < (int)arr.size(); i++){
        if ((i%width) == width-1){
            print(" ");
            println(arr[i]);
        }else if ((i%width) == 0){        
            print(arr[i]);
        }else {
            print(" ");
            print(arr[i]);
        }
    }    
}

std::tuple<int> readInt1(){
    int a;
    std::cin >> a;
    return {a};
}
std::tuple<int,int> readInt2(){
    int a, b;
    std::cin >> a >> b;
    return {a,b};
}
std::tuple<int,int,int> readInt3(){
    int a, b, c;
    std::cin >> a >> b >> c;
    return {a,b,c};
}

std::vector<int> readVecInt(int n){
    std::vector<int> arr(n);
    for (int i = 0; i < n; i++){
        int a;
        std::cin >> a;
        arr[i] = a;
    }
    return arr;
}

std::vector<std::tuple<int,int>> readVecTup2(int n){
    std::vector<std::tuple<int,int>> arr(n);
    for (int i = 0; i < n; i++){
        int a, b;
        std::cin >> a;
        std::cin >> b;
        arr[i] = {a,b};
    }
    return arr;
}

std::vector<std::tuple<int,int,int>> readVecTup3(int n){
    std::vector<std::tuple<int,int,int>> arr(n);
    for (int i = 0; i < n; i++){
        int a, b, c;
        std::cin >> a;
        std::cin >> b;
        std::cin >> c;
        arr[i] = {a,b,c};
    }
    return arr;
}

template<typename T>
auto getTupleSort(){
    auto tupleSort = [](const std::tuple<T,T> &a, const std::tuple<T,T> &b){ 
        auto [ax, ay] = a;
        auto [bx, by] = b;
        return ay < by; };
    return tupleSort;
}


template<typename T>
void sortBy(std::vector<T> &arr){
    std::sort(arr.begin(),arr.end(),[](const T &a, const T &b){ return a < b; });
}


template<typename T>
T binarySearch(T min, T max, std::function<bool(T)> leq){
    if (min == max) return max;
    T guess = (min+max)/2;
    if (leq(guess)){
        return binarySearch(min,guess,leq);
    }else{
        return binarySearch(guess+1,max,leq);
    }
}

template<typename T>
T binarySearchDecending(T min, T max, std::function<bool(T)> leq){
    std::function<bool(T)> gt = [leq](T x){
        return !leq(x);
    };
    return binarySearch(min,max+1, gt)-1;
}

void carManufacturing(){
    auto [n, t] = readInt2();
    //auto [n, m, k] = readInt3();
    auto arr = readVecInt(n);
    double s = 0;
    for (int x: arr){
        s += 1.0/x;
    }
    std::function<bool(long long)> leq = [&arr,t](long long time){
        long long s = 0;
        for (int x : arr){
            s += time/x;
        }
        return t <= s;
    };
    println(binarySearch((long long)0,(long long)(t/s*(long long)n),leq));
}

template<typename T>
double avg(std::vector<T> &arr){
    double mult = 1.0/arr.size();
    double s = 0;
    for (T x : arr){
        s += mult*(double)x;
    }
    return s;
}
template<typename T>
T sum(std::vector<T> &arr){
    T s = 0;
    for (T x : arr){
        s += x;
    }
    return s;
}

void bottle(){
    auto [n] = readInt1();
    //auto [n, t] = readInt2();
    //auto [n, m, k] = readInt3();
    auto arr = readVecInt(n);
    double s = avg(arr);
    
    std::function<bool(long long)> leq = [&arr](long long t){
        long long s = 0;
        for (long long x : arr){
            if (x-t > 0){
                s += (x-t)/2;
            }else{
                s += (x-t);
            }
        }
        return 0 <= s;
    };
    println(binarySearchDecending((long long)0,(long long)(s),leq));
}

#include <set>
#include <algorithm>
void movie(){
    auto [n] = readInt1();
    auto arr = readVecInt(2*n);
    int* s = &arr[0];
    std::sort((long*)(s),(long*)(s)+n);
    int time = 0;
    int num = 0;
    for (int i = 0; i < n; i++){
        if (time <= arr[2*i]){
            time = arr[2*i+1];
            num += 1;
        }
    }
    println(num);
}

/*

    for (int i = 0; i < n; i++){
        int temp = arr[2*i+1];
        arr[2*i+1] = arr[2*i];
        arr[2*i] = temp;
    }
        */

void cow(){
    auto [n] = readInt1();
    auto dum = readVecInt(n);
    print(1);
    for (int i = 1; i < 30; i++){
        print(" ");
        if ((1<<i) <= 1000*1000*1000){
            print(1<<i);
        } else{
            print(0);
        }
    }    
}
template<typename T>
std::vector<T> cloneVec(std::vector<T> a){
    return a;
}

void castle(){
    auto [n] = readInt1();
    auto arr = readVecTup2(n);
    std::vector<std::tuple<int,int>> arr2 = cloneVec(arr);
    for (int i = 0; i < n; i++){
        auto [u,m] = arr[i];
        arr2[i] = {-(u+m),i};
    }
    sortBy(arr2);
    int us = 0;
    int ms = 0;
    printVec(arr2);
    for (int i = 0; i < n; i++){
        auto [s,idx] = arr2[i];
        auto [u,m] = arr[idx];
        if (i%2 == 0){
            us += u;
        } else {
            ms += m;
        }
    }
    println(us-ms);
}

template<typename T>
void extendedFibonachi(std::vector<T> &initial, int nlast, T upto, T mod ){
    for (int i = nlast; i <= upto; i++){
        initial[i] = 0;
        for (int n = 1; n <= nlast && n <= i; n++){
            initial[i] += initial[i-n];
            initial[i] %=mod;
        }
    }
}

void dice(){
    auto [n] = readInt1();
    std::vector<long long> arr(std::max(7,n+1));
    arr[0] = 1;
    for (int i = 0; i < 6; i++){
        arr[i+1] = 1<<i;
    }
    extendedFibonachi(arr,6,(long long)n,(long long)1000*1000*1000+7);
    println(arr[n]);
}

template<typename T>
std::vector<T> padding(std::vector<T> &initial, T num, int start, int end){
    std::vector<T> arr(start+initial.size()+end);
    for (int i = 0; i < start; i++){
        arr[i] = num;
    }
    for (int i = 0; i < (int)initial.size(); i++){
        arr[start+i] = initial[i];
    }
    for (int i = 0; i < end; i++){
        arr[start+initial.size()+i] = num;
    }
    return arr;
}

template<typename T>
void operateOnMax(int start, int end, std::function<T(int)> &maxHeuristic, std::function<void(int)> &found){
    T initialGuess = maxHeuristic(start);
    int index = start;
    for (int i = start; i < end; i++){
        if (initialGuess < maxHeuristic(i)){
            initialGuess = maxHeuristic(i);
            index = i;
        }
    }
    found(index);
}
template<typename T>
void recursiveSplit(int start, int end, int distl, int distr, std::function<T(int)> &maxHeuristic, std::function<void(int)> &found){
    if (start == end){
        return found(start);
    }else if (end < start){
        return;
    }
    std::function<void(int)> splitter = [start, end, distl, distr,&maxHeuristic,&found](int idx){
        found(idx);
        if (idx-start-distr < end-idx-distl){
            recursiveSplit(idx+distl,   end, distl, distr, maxHeuristic, found);
            recursiveSplit(start, idx-distr, distl, distr, maxHeuristic, found);
        }else {
            recursiveSplit(start, idx-distr, distl, distr, maxHeuristic, found);
            recursiveSplit(idx+distl,   end, distl, distr, maxHeuristic, found);
        }
    };
    operateOnMax(start, end, maxHeuristic,splitter);
}

void cardGame(){
    auto [n] = readInt1();
    auto arr = readVecInt(n);
    arr[0] = 0;
    arr[arr.size()-1] = 0;
    //printVec(arr);
    arr = padding(arr,0,1,1);
    //printVec(arr);
    std::function<int(int)> heuristic = [&arr](int idx){
        return arr[idx]-arr[idx-1]-arr[idx-2]-arr[idx+1]-arr[idx+2];
    };
    long long sum = 0;
    std::function<void(int)> operate = [&arr, &sum](int idx){
        arr[idx-2] = 0;
        arr[idx-1] = 0;
        sum += arr[idx];
        arr[idx+1] = 0;
        arr[idx+2] = 0;
    };
    recursiveSplit(2,(int)arr.size()-2,2,2,heuristic,operate);
    println(sum);
}



int main() {
    //carManufacturing();
    //bottle();
    //movie();
    //cow();
    //castle();
    //dice();
    cardGame();
    exit(0);
}

Test details

Test 1

Verdict: ACCEPTED

input
5
9 4 1 6 6

correct output
6

user output
6

Test 2

Verdict: ACCEPTED

input
6
5 6 2 4 10 1

correct output
16

user output
16

Test 3

Verdict: ACCEPTED

input
10
8 9 10 2 7 1 10 10 1 4

correct output
26

user output
26

Test 4

Verdict:

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

correct output
243

user output
(empty)

Error:
free(): invalid pointer

Test 5

Verdict:

input
1000
10 7 5 6 5 2 5 3 2 2 1 6 8 7 8...

correct output
2230

user output
2176

Feedback: Incorrect character on line 1 col 2: expected "2230", got "2176"

Test 6

Verdict:

input
10000
9 1 8 2 6 5 1 3 3 10 6 3 9 3 1...

correct output
22363

user output
21960

Feedback: Incorrect character on line 1 col 2: expected "22363", got "21960"

Test 7

Verdict:

input
100000
5 5 4 6 8 7 9 6 3 2 5 8 7 3 5 ...

correct output
226636

user output
222073

Feedback: Incorrect character on line 1 col 3: expected "226636", got "222073"

Test 8

Verdict:

input
1000000
5 8 5 7 9 1 9 10 3 6 1 8 3 9 7...

correct output
2259395

user output
2211170

Feedback: Incorrect character on line 1 col 3: expected "2259395", got "2211170"

Test 9

Verdict:

input
1000000
4 5 3 5 4 3 6 7 10 6 3 9 7 9 1...

correct output
2260761

user output
2212121

Feedback: Incorrect character on line 1 col 3: expected "2260761", got "2212121"

Test 10

Verdict:

input
1000000
10 3 6 7 7 10 4 4 5 2 9 4 6 10...

correct output
2260407

user output
2212336

Feedback: Incorrect character on line 1 col 3: expected "2260407", got "2212336"

Test 11

Verdict: ACCEPTED

input
3
87 3 123

correct output
3

user output
3

Test 12

Verdict: ACCEPTED

input
2
175 95

correct output
0

user output
0

Test 13

Verdict: ACCEPTED

input
1
42

correct output
0

user output
0

Test 14

Verdict:

input
1000000
1000 1000 1000 1000 1000 1000 ...

correct output
333333000

user output
(empty)

Test 15

Verdict:

input
1000000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
333333

user output
(empty)