Submission #794914

# Submission time Handle Problem Language Result Execution time Memory
794914 2023-07-27T03:55:06 Z 79brue Line Town (CCO23_day1problem3) C++17
25 / 25
409 ms 35436 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll INF = 1e18;

struct Fenwick{
    int n;
    int tree[500002];

    void init(int _n){
        n = _n;
        for(int i=1; i<=n; i++) tree[i] = 0;
    }

    void add(int x, int y){
        while(x<=n){
            tree[x] += y;
            x+=x&-x;
        }
    }

    int sum(int x){
        int ret = 0;
        while(x){
            ret += tree[x];
            x-=x&-x;
        }
        return ret;
    }

    int sum(int l, int r){
        if(l>r) return 0;
        return sum(r) - sum(l-1);
    }
} tree, treeL, treeR;

int n;
ll arr[500002];
ll DP[2][2];
ll ld[500002], rd[500002];

void solve();

int main(){
    scanf("%d", &n);
    for(int i=1; i<=n; i++){
        scanf("%lld", &arr[i]);
        if(i%2==0) arr[i] = -arr[i];
    }
    solve();
}

bool impossible(int start0, int start1, int len0, int len1, int cnt0, int cnt1){
    int c0 = 0, c1 = 0;
    c0 += (len0 % 2 && start0 == 0) ? (len0 + 1) / 2 : len0 / 2;
    c1 += (len0 % 2 && start0 == 1) ? (len0 + 1) / 2 : len0 / 2;
    c0 += (len1 % 2 && start1 == 0) ? (len1 + 1) / 2 : len1 / 2;
    c1 += (len1 % 2 && start1 == 1) ? (len1 + 1) / 2 : len1 / 2;
    if(c0 == cnt0 && c1 == cnt1) return false;
    return true;
}

ll inversion = 0;

void addSetLeft(int x){
    inversion += ld[x];
    inversion += treeL.sum(x+1, n);
    inversion += treeR.sum(1, x-1);
    treeL.add(x, 1);
}

void addSetRight(int x){
    inversion += rd[x];
    inversion += treeR.sum(1, x-1);
    inversion += treeL.sum(x+1, n);
    treeR.add(x, 1);
}

void delSetLeft(int x){
    treeL.add(x, -1);
    inversion -= treeR.sum(1, x-1);
    inversion -= treeL.sum(x+1, n);
    inversion -= ld[x];
}

void delSetRight(int x){
    treeR.add(x, -1);
    inversion -= treeL.sum(x+1, n);
    inversion -= treeR.sum(1, x-1);
    inversion -= rd[x];
}

void solve(){
    vector<pair<ll, int> > vec;
    for(int i=1; i<=n; i++) vec.push_back(make_pair(abs(arr[i]), i));
    sort(vec.rbegin(), vec.rend());

    tree.init(n);
    treeL.init(n);
    treeR.init(n);
    for(int i=1; i<=n; i++) tree.add(i, 1);

    /// 부호 관련
    /// 부호는 -가 0, +가 1
    for(int a=0; a<4; a++) DP[(a>>1)&1][a&1] = INF;
    DP[0][0] = 0;

    int usedCnt = 0; /// 지금까지 양쪽으로 보낸 개수
    for(int s=0; s<n; s++){
        if(vec[s].first == 0) break;
        int e = s;
        while(e+1<n && vec[s].first == vec[e+1].first) e++;
        reverse(vec.begin()+s, vec.begin()+e+1);
        vector<ll> pos[2];
        vector<ll> rpos[2];

        for(int i=s; i<=e; i++) tree.add(vec[i].second, -1);

        for(int i=s; i<=e; i++){
            int x = vec[i].second;
            ld[x] = tree.sum(1, x), rd[x] = tree.sum(x, n);
            if(arr[x] < 0) pos[0].push_back(x); /// 일단 인덱스를 입력함
            else           pos[1].push_back(x); /// 여기도 인덱스를 입력함
        }

        rpos[0] = pos[0];
        rpos[1] = pos[1];
        reverse(rpos[0].begin(), rpos[0].end());
        reverse(rpos[1].begin(), rpos[1].end());

        int L = tree.sum(1, n); /// 현재 수를 제외한 켜져 있는 남은 수의 개수
        int K = (int)pos[0].size() + (int)pos[1].size(); /// 현재 보고 있는 수의 개수

        for(int js=0; js<2; js++){ /// 왼쪽 요구사항 -> 왼쪽은 무엇으로 시작해야 하는가?
            int je = (n%2) ^ ((usedCnt - js) & 1); /// 오른쪽 요구사항 -> 오른쪽은 무엇으로 시작해야 하는가?
            for(int ta=0; ta<2; ta++){ /// 왼쪽에 붙은 개수의 홀짝성을 고정
                int tb = (K+ta)%2; /// 이건 오른쪽에 붙은 개수의 홀짝성
                if(impossible(js, je, ta, K-ta, (int)pos[0].size(), (int)pos[1].size())) continue; /// 홀짝성이 안 맞음

                int l = ta, r = K-ta;
                inversion = 0;
                for(int i=1; i<=l; i++){
                    if(i%2 == 1) addSetLeft(pos[js][i/2]);
                    else         addSetLeft(pos[!js][(i-1)/2]);
                }
                for(int i=1; i<=r; i++){
                    if(i%2 == 1) addSetRight(rpos[je][i/2]);
                    else         addSetRight(rpos[!je][(i-1)/2]);
                }
                DP[1][(js+ta)&1] = min(DP[1][(js+ta)&1], DP[0][js] + inversion);

                while(r>=2){
                    for(int c=0; c<2; c++){
                        if(r%2==1) delSetRight(rpos[je][r/2]);
                        else       delSetRight(rpos[!je][(r-1)/2]);
                        r--, ++l;
                        if(l%2==1) addSetLeft(pos[js][l/2]);
                        else       addSetLeft(pos[!js][(l-1)/2]);
                    }
                    DP[1][(js+ta)&1] = min(DP[1][(js+ta)&1], DP[0][js] + inversion);
                }

                while(l){
                    if(l%2==1) delSetLeft(pos[js][l/2]);
                    else       delSetLeft(pos[!js][(l-1)/2]);
                    l--;
                }
                while(r){
                    if(r%2==1) delSetRight(rpos[je][r/2]);
                    else       delSetRight(rpos[!je][(r-1)/2]);
                    r--;
                }
            }
        }

        s = e;
        for(int a=0; a<2; a++) DP[0][a] = DP[1][a], DP[1][a] = INF;
        usedCnt += K;
    }

    ll ans = min(DP[0][0], DP[0][1]);
    printf("%lld", ans == INF ? -1 : ans);
}

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:139:21: warning: unused variable 'tb' [-Wunused-variable]
  139 |                 int tb = (K+ta)%2; /// 이건 오른쪽에 붙은 개수의 홀짝성
      |                     ^~
Main.cpp:133:13: warning: unused variable 'L' [-Wunused-variable]
  133 |         int L = tree.sum(1, n); /// 현재 수를 제외한 켜져 있는 남은 수의 개수
      |             ^
Main.cpp: In function 'int main()':
Main.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
Main.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         scanf("%lld", &arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 171 ms 35092 KB Output is correct
16 Correct 163 ms 35436 KB Output is correct
17 Correct 166 ms 35128 KB Output is correct
18 Correct 79 ms 34468 KB Output is correct
19 Correct 254 ms 34480 KB Output is correct
20 Correct 221 ms 33724 KB Output is correct
21 Correct 232 ms 33672 KB Output is correct
22 Correct 262 ms 33680 KB Output is correct
23 Correct 229 ms 33792 KB Output is correct
24 Correct 165 ms 33572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 171 ms 35092 KB Output is correct
16 Correct 163 ms 35436 KB Output is correct
17 Correct 166 ms 35128 KB Output is correct
18 Correct 79 ms 34468 KB Output is correct
19 Correct 254 ms 34480 KB Output is correct
20 Correct 221 ms 33724 KB Output is correct
21 Correct 232 ms 33672 KB Output is correct
22 Correct 262 ms 33680 KB Output is correct
23 Correct 229 ms 33792 KB Output is correct
24 Correct 165 ms 33572 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 190 ms 30396 KB Output is correct
45 Correct 184 ms 30024 KB Output is correct
46 Correct 139 ms 30400 KB Output is correct
47 Correct 95 ms 30000 KB Output is correct
48 Correct 328 ms 32116 KB Output is correct
49 Correct 229 ms 33828 KB Output is correct
50 Correct 161 ms 25812 KB Output is correct
51 Correct 166 ms 25860 KB Output is correct
52 Correct 163 ms 25840 KB Output is correct
53 Correct 95 ms 25864 KB Output is correct
54 Correct 299 ms 29112 KB Output is correct
55 Correct 199 ms 29144 KB Output is correct
56 Correct 205 ms 29044 KB Output is correct
57 Correct 58 ms 18080 KB Output is correct
58 Correct 69 ms 17988 KB Output is correct
59 Correct 70 ms 18020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 198 ms 25764 KB Output is correct
15 Correct 312 ms 25756 KB Output is correct
16 Correct 319 ms 25776 KB Output is correct
17 Correct 302 ms 25856 KB Output is correct
18 Correct 304 ms 25764 KB Output is correct
19 Correct 337 ms 25816 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 244 ms 25848 KB Output is correct
23 Correct 244 ms 25832 KB Output is correct
24 Correct 198 ms 25772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 1 ms 468 KB Output is correct
48 Correct 1 ms 468 KB Output is correct
49 Correct 1 ms 468 KB Output is correct
50 Correct 2 ms 468 KB Output is correct
51 Correct 1 ms 468 KB Output is correct
52 Correct 1 ms 468 KB Output is correct
53 Correct 1 ms 468 KB Output is correct
54 Correct 1 ms 468 KB Output is correct
55 Correct 1 ms 468 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
57 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 171 ms 35092 KB Output is correct
16 Correct 163 ms 35436 KB Output is correct
17 Correct 166 ms 35128 KB Output is correct
18 Correct 79 ms 34468 KB Output is correct
19 Correct 254 ms 34480 KB Output is correct
20 Correct 221 ms 33724 KB Output is correct
21 Correct 232 ms 33672 KB Output is correct
22 Correct 262 ms 33680 KB Output is correct
23 Correct 229 ms 33792 KB Output is correct
24 Correct 165 ms 33572 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 190 ms 30396 KB Output is correct
45 Correct 184 ms 30024 KB Output is correct
46 Correct 139 ms 30400 KB Output is correct
47 Correct 95 ms 30000 KB Output is correct
48 Correct 328 ms 32116 KB Output is correct
49 Correct 229 ms 33828 KB Output is correct
50 Correct 161 ms 25812 KB Output is correct
51 Correct 166 ms 25860 KB Output is correct
52 Correct 163 ms 25840 KB Output is correct
53 Correct 95 ms 25864 KB Output is correct
54 Correct 299 ms 29112 KB Output is correct
55 Correct 199 ms 29144 KB Output is correct
56 Correct 205 ms 29044 KB Output is correct
57 Correct 58 ms 18080 KB Output is correct
58 Correct 69 ms 17988 KB Output is correct
59 Correct 70 ms 18020 KB Output is correct
60 Correct 0 ms 340 KB Output is correct
61 Correct 0 ms 340 KB Output is correct
62 Correct 1 ms 468 KB Output is correct
63 Correct 2 ms 468 KB Output is correct
64 Correct 1 ms 468 KB Output is correct
65 Correct 1 ms 468 KB Output is correct
66 Correct 1 ms 468 KB Output is correct
67 Correct 1 ms 468 KB Output is correct
68 Correct 0 ms 340 KB Output is correct
69 Correct 0 ms 340 KB Output is correct
70 Correct 1 ms 468 KB Output is correct
71 Correct 1 ms 468 KB Output is correct
72 Correct 1 ms 468 KB Output is correct
73 Correct 198 ms 25764 KB Output is correct
74 Correct 312 ms 25756 KB Output is correct
75 Correct 319 ms 25776 KB Output is correct
76 Correct 302 ms 25856 KB Output is correct
77 Correct 304 ms 25764 KB Output is correct
78 Correct 337 ms 25816 KB Output is correct
79 Correct 1 ms 340 KB Output is correct
80 Correct 0 ms 340 KB Output is correct
81 Correct 244 ms 25848 KB Output is correct
82 Correct 244 ms 25832 KB Output is correct
83 Correct 198 ms 25772 KB Output is correct
84 Correct 2 ms 468 KB Output is correct
85 Correct 1 ms 468 KB Output is correct
86 Correct 1 ms 468 KB Output is correct
87 Correct 1 ms 468 KB Output is correct
88 Correct 2 ms 468 KB Output is correct
89 Correct 1 ms 468 KB Output is correct
90 Correct 1 ms 468 KB Output is correct
91 Correct 1 ms 468 KB Output is correct
92 Correct 1 ms 468 KB Output is correct
93 Correct 1 ms 468 KB Output is correct
94 Correct 0 ms 340 KB Output is correct
95 Correct 0 ms 340 KB Output is correct
96 Correct 218 ms 28416 KB Output is correct
97 Correct 271 ms 28940 KB Output is correct
98 Correct 222 ms 28836 KB Output is correct
99 Correct 230 ms 25968 KB Output is correct
100 Correct 203 ms 29352 KB Output is correct
101 Correct 325 ms 26100 KB Output is correct
102 Correct 318 ms 26108 KB Output is correct
103 Correct 409 ms 25900 KB Output is correct
104 Correct 409 ms 25904 KB Output is correct
105 Correct 402 ms 25916 KB Output is correct
106 Correct 358 ms 25976 KB Output is correct