Skip to content

14255: 【原4255】A and B Problem

题目

题目描述

author: Guo Linsong 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4255

Description

There is an integer sequence \(A\) of length \(N\) whose values are unknown.

Given is an integer sequence \(B\) of length \(N - 1\) which is known to satisfy the following:

\(B_i \geq max(A_i,A_{i+1})\)

Find the maximum possible sum of the elements of \(A\).

All values in input are integers.

\(2 \leq N \leq 100\)

\(0 \leq B_i \leq 10^5\)

Input Format

Input is given from Standard Input in the following format:

\(N\)

\(B_1~B_2\ldots B_{N-1}\)

Output Format

Print the maximum possible sum of the elements of \(A\).

Sample Input 1

3

2 5

Sample Output 1

9

Sample Input 2

6

0 153 10 10 23

Sample Output 2

53

ligongzzz's solution

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;
    vector<int> vdata(n - 1);

    for (int i = 0; i < n - 1; ++i)
        cin >> vdata[i];

    int ans = vdata[0] + vdata[n - 2];
    for (int i = 0; i < n - 2; ++i)
        ans += min(vdata[i], vdata[i + 1]);

    cout << ans;

    return 0;
}

zqy2018's solution

#include <bits/stdc++.h>
#define INF 2000000000
using namespace std;
typedef long long ll;
int read(){
    int f = 1, x = 0;
    char c = getchar();
    while(c < '0' || c > '9'){if(c == '-') f = -f; c = getchar();}
    while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
    return f * x; 
}
int n, b[105], a[105];
void init(){
    n = read();
    for (int i = 1; i < n; ++i)
        b[i] = read();
    a[1] = a[2] = b[1];
}
void solve(){
    for (int i = 2; i < n; ++i){
        a[i + 1] = b[i];
        if (b[i] < b[i - 1])
            a[i] = b[i];
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i)
        ans += a[i];
    printf("%d\n", ans);
}
int main(){
    init();
    solve();
    return 0;
}