From 67b90577c53565783baa39dfca813714ab0e4709 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 31 May 2022 23:23:19 +0800 Subject: [PATCH] C - Sum of Substrings https://codeforces.com/contest/1691/submission/159034603 --- Codeforces/1691/C/C.cpp | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Codeforces/1691/C/C.cpp diff --git a/Codeforces/1691/C/C.cpp b/Codeforces/1691/C/C.cpp new file mode 100644 index 00000000..347f9295 --- /dev/null +++ b/Codeforces/1691/C/C.cpp @@ -0,0 +1,43 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +int t, n, k; +std::string s; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> t; + + while (t--) { + cin >> n >> k >> s; + + auto i = s.find_last_of('1'); + if (i != std::string::npos && n - i - 1 <= k) { + k -= n - i - 1; + std::swap(s[n - 1], s[i]); + } + + auto j = s.find_first_of('1'); + if (j != n - 1) { + if (j != std::string::npos && j <= k) { + std::swap(s[0], s[j]); + k -= j; + } + } + + int ans = 0; + for (int i = 1; i < s.size(); i++) { + ans += std::stoi(s.substr(i - 1, 2)); + } + + cout << ans << endl; + } + + return 0; +}