Blue Bridge Cup - Maximum Sum

Topic Link: Maximum Sum

Problem Description

Xiaolan is playing a treasure hunting game. The game is played on a straight road. The road is divided into nn squares, numbered 1 to nn in sequence. There is a treasure on each square, and the value of the treasure is an integer ( Including positive numbers, negative numbers and zero), when you enter a square, you will get the score of the treasure in the square. The total score that Xiaolan can get is the sum of the points he gets from the squares.

Xiaolan stood on square 1 at the beginning and got the points of the treasure on square 1. He has to go through several steps to reach square n.

When Xiaolan stands on square pp, he can choose to jump to one of these squares from p+1 to p+D(n−p) , where D(1)=1,D(x)(x> 1) Defined as the smallest prime factor of x.

Given the points of the treasures in each square, what is the maximum total points that Xiaolan can get.

input format

The first line of input contains a positive integer n .

The second line contains n integers, which in turn represent the score of the treasure in each square.

output format

The output line contains an integer representing the answer.

sample input


5

1 -2 -1 3 5

sample output


8

sample output

The optimal jumping scheme is: 1→3→4→5.

Evaluation use case scale and agreement

For 40% of the evaluation cases, 1≤n≤100.

For 80% of the evaluation cases, 1≤n≤1000.

For all evaluation cases, 1≤n≤10000, the score of each treasure is an integer whose absolute value does not exceed 10^5.

operating limit

  • Maximum running time: 1s
  • Maximum running memory: 256M

 

Ideas:

First look at the topic clearly : Xiaolan's starting position is 1; and the range of skipping steps: 1-X (X is the smallest prime factor of np)

Idea: Enumerate jumpable ranges

Dynamic programming formula: f[j]=max(f[j],f[i]+a[j])

Judging whether jumping from the current position to the jumpable position can increase the score

Initialize an array: f[n]=INT_MIN

AC code:

#include<bits/stdc++.h>
using namespace std;
int n;
int a[10015],f[10015];
bool is_prime(int k){
    for(int i=2;i*i<=k;i++){
        if(k%i==0) return 0;
    }
    return 1;
}
int funX(int k){
    for(int i=2;i<=k;i++){
        if(is_prime(i) && k%i==0){
            return i;
        }
    }
    return 1;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
        f[i]=-999999;
    }
    f[1]=a[1];
    for(int i=1;i<n;i++){
        int x=funX(n-i);
        //printf("x:%d\n",x);
        for(int j=i+1;j<=x+i;j++){
            if(f[j]==-999999){
                f[j]=f[i]+a[j];
            }
            else{
                if(f[j]<(f[i]+a[j])) f[j]=f[i]+a[j];
            }
        }
        /*for(int j=1;j<=n;j++){
            printf("%d ",f[j]);
        }
        printf("\n");*/
    }
    printf("%d\n",f[n]);
    return 0;
}

Guess you like

Origin blog.csdn.net/abcdefghikk/article/details/129959778