Can someone explain this please?

This is Permutator, final question of Bronze module “Introduction to Sorting”.

This is my code:

#include <bits/stdc++.h>
using namespace std;

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

  int n; cin >> n;
  vector<long long> a(n), b(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < n; ++i) cin >> b[i];

  vector<long long> scaled_a(n);
  for (int i = 0; i < n; ++i) {
    long long count = (1LL * i + 1) * (n - i);
    scaled_a[i] = a[i] * count;
  }

  sort(scaled_a.begin(), scaled_a.end());
  sort(b.rbegin(), b.rend());

  long long t = 0;
  for (int i = 0; i < n; ++i) {
    t += scaled_a[i] * b[i];
  }

  cout << t << "\n";

  return 0;
}

This is editorial one:

#include <algorithm>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

int main() {
        int len;
        std::cin >> len;
        vector<int> a(len);
        for (int &i : a) { std::cin >> i; }
        vector<int> b(len);
        for (int &i : b) { std::cin >> i; }

        vector<long long> subarr_num(len);
        for (int i = 0; i < len; i++) { subarr_num[i] = (long long)(i + 1) * (len - i); }

        vector<long long> actual_a(len);
        for (int i = 0; i < len; i++) { actual_a[i] = subarr_num[i] * a[i]; }

        std::sort(actual_a.begin(), actual_a.end());
        std::sort(b.begin(), b.end(), std::greater<int>());

        long long total = 0;
        for (int i = 0; i < len; i++) { total += actual_a[i] * b[i]; }
        cout << total << endl;
}

My solution fails the test cases but the editorial one passes every test case. But both logics are same? Can someone please explain this to me.

Hello,

I submitted your exact code same code on Codeforces Gym Problem 104520H, and it seems like your code passes perfectly:

Is it the case that you have made an error submitting that caused the failure(eg submitted the wrong code or forgot to save file or under the wrong language)?

Hope that helps,
Eric Liu

1 Like

It passes? Very weird. Let me look at it. Oh, I see the problem, my Neovim was showing me latest version of my file but somehow was saving a older swap file to the OS and mismatch was persistent. My neovim previously crashed.

When I directly copy pasted the code it worked. Thank you anyways :slight_smile: