CSES Introductory problem’s algorithm – Increasing Array

I am working around to resolve CSES Introductory problem’s algorithm – Increasing Array via c++. I need help, please.

You are given an array of n integers. You want to modify the array so that it is increasing, i.e., every element is at least as large as the previous element.
On each move, you may increase the value of any element by one. What is the minimum number of moves required?

Input
The first input line contains an integer n: the size of the array.
Then, the second line contains n integers x1, x2 … xN the contents of the array.

Output

Print the minimum number of moves.

Example

Input: 5
3 2 5 1 7

Output: 5

My code is following

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <charconv>
#include <cassert>
#include <iomanip>
#include <optional>
#include <string_view>
using namespace std;
int main()
{
int arrSize = 0;
int number = -1;
vector<int> vect;
string s;
std::string firstLine;
std::getline(std::cin, firstLine);
arrSize = stoi(firstLine);
std::string secondLine;
std::getline(std::cin, secondLine);
std::istringstream strm(secondLine);
while (strm >> s)
{
std::istringstream(s) >> number;
vect.push_back(number);
}
int cnt = 0;
for (int i = 0; i < arrSize - 1; i++)
{
for (int j = i + 1; j < arrSize; j++)
{
int first = vect[i];
int second = vect[j];
if (first > second)
{
std::swap(vect[i], vect[j]);
cnt++;
}
}
}
std::cout << cnt;
return 0;
}
</code>
<code>#include <iostream> #include <vector> #include <string> #include <algorithm> #include <cstdlib> #include <iterator> #include <algorithm> #include <numeric> #include <sstream> #include <charconv> #include <cassert> #include <iomanip> #include <optional> #include <string_view> using namespace std; int main() { int arrSize = 0; int number = -1; vector<int> vect; string s; std::string firstLine; std::getline(std::cin, firstLine); arrSize = stoi(firstLine); std::string secondLine; std::getline(std::cin, secondLine); std::istringstream strm(secondLine); while (strm >> s) { std::istringstream(s) >> number; vect.push_back(number); } int cnt = 0; for (int i = 0; i < arrSize - 1; i++) { for (int j = i + 1; j < arrSize; j++) { int first = vect[i]; int second = vect[j]; if (first > second) { std::swap(vect[i], vect[j]); cnt++; } } } std::cout << cnt; return 0; } </code>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <iterator>  
#include <algorithm>  
#include <numeric>    
#include <sstream>
#include <charconv>
#include <cassert>
#include <iomanip>
#include <optional>
#include <string_view> 
using namespace std;

int main()
{
    int arrSize = 0;
    int number = -1;
    vector<int> vect;
    string s;

    std::string firstLine;
    std::getline(std::cin, firstLine);
    arrSize = stoi(firstLine); 

    std::string secondLine;
    std::getline(std::cin, secondLine);
    std::istringstream strm(secondLine);

    while (strm >> s)
    {
        std::istringstream(s) >> number;
        vect.push_back(number);
    }
    
    int cnt = 0; 
    
    for (int i = 0; i < arrSize - 1; i++)
    {
        for (int j = i + 1; j < arrSize; j++)
        {
            int first = vect[i];

            int second = vect[j];

            if (first > second)
            {
                std::swap(vect[i], vect[j]); 
                cnt++;
            }
        }
    }

    std::cout << cnt;

    return 0;
}

For the one of test cases input data are

10
6 10 4 10 2 8 9 2 7 7

and the correct output is by test

31

but my code returns

18

I understand here tha “move” is in fact a swap. Cannot get the mistake…

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật