Submission #853023

# Submission time Handle Problem Language Result Execution time Memory
853023 2023-09-23T10:35:31 Z Benmath Homecoming (BOI18_homecoming) C++14
31 / 100
1000 ms 21980 KB
#include<homecoming.h>
#include<bits/stdc++.h>
using namespace std;
long long solve(int N, int K, int *A, int *B){
    int n = N;
    int k = K;
    long long int a[n];
    long long int b[n];
    long long int dp[n];
    long long int dp1[n];
    long long int sum1 = 0;
    for(int i = 0; i < n; i++){
        a[i] = A[i];
        b[i] = B[i];
        sum1 = sum1 + a[i] - b[i];
       
        dp[i] = 0;
        dp1[i] = -1e18;
    }
    long long int ans = 0;
    
    ans = max (ans, sum1);
    // ako ne uzimamo prvi element
    for(int i = 0; i < n; i++){
        if (i == 0){
            long long int sum = 0;
            for (int j = i; j < (i + k); j++){
                sum = sum + b[j % n];
            }
            dp1[i] = a[i] - sum;
            
           // dp[i] = max(dp[i], dp1[i]);
           dp[i] = 0;
           dp1[i] = -1e18;
        }else{
            dp[i] = dp[i-1];
            long long int sum = 0;
            for (int j = i; j < (i + k); j++){
                sum = sum + b[j % n];
            }
            sum = a[i] - sum;
            if (i >= k){
                dp1[i] = sum + dp[i-k];
            }else{
                dp1[i] = sum;
            }
         
            dp1[i] = max(dp1[i], dp1[i-1] + a[i] - b[(i + k - 1) % n] );
            dp[i] = max(dp[i], dp1[i]);
        }
    }
    
    ans = max(ans, dp[n-1]);
    
    // ako uzimamo prvi element
    int vis[n];
    for (int i = 0; i < n; i++){
        dp[i] = -1e18;
        dp1[i] = -1e18;
        vis[i] = 0;
    }
    for (int i = 0; i < n; i++){
        if (i == 0){
            long long int sum = 0;
            for (int j = i; j < (i + k); j++){
                sum = sum + b[j % n];
                vis[j] ++;
            }
            dp1[i] = a[i] - sum;
            dp[i] = dp1[i];
        }else{
            if (i >= k){
                dp[i] = dp[i-1];
                long long int sum = 0;
                for (int j = i; j < (i + k); j++){
                    sum = sum + b[j % n];
                }
                sum = a[i] - sum;
               
                dp1[i] = sum + dp[i-k];
                long long int ro = a[i];
                if(vis[(i + k - 1) % n] == 0){
                    ro = ro - b[(i + k - 1) % n];
                }
                dp1[i] = max(dp1[i], dp1[i-1] + ro);
            
                dp[i] = max(dp[i], dp1[i]);
                sum = 0;
                for (int j = i; j <= (n-1); j++){
                    sum = sum + a[j] - b[j];
                }
                ans = max(ans, sum + dp[i-k]);
            }else{
                dp[i] = dp[i-1];
                long long int ro = a[i]; 
                if(vis[(i + k - 1) % n] == 0){
                    ro = ro - b[(i + k - 1) % n];
                }
                dp1[i] =  dp1[i-1] + ro;
                dp[i] = max(dp[i], dp1[i]);
            }
        }
    }
    ans = max(ans, dp[n-1]);
   return ans;
    
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 108 ms 624 KB Output is correct
7 Correct 101 ms 604 KB Output is correct
8 Correct 20 ms 344 KB Output is correct
9 Correct 12 ms 604 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1050 ms 21980 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 108 ms 624 KB Output is correct
7 Correct 101 ms 604 KB Output is correct
8 Correct 20 ms 344 KB Output is correct
9 Correct 12 ms 604 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Execution timed out 1050 ms 21980 KB Time limit exceeded
12 Halted 0 ms 0 KB -